recursion functions

this program wont run


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
/// user inputs a arrya of numbers and a number to look for 
// and the computer outputs the amount of times the number shows up



#include <iostream>
using namespace std;

int count(int number, char array[], int length, int counter);
int main()
{
    // inputs number and array
    char Sentence[20];
    int number;
    cout << "type in a array: " << endl;
    cin.getline(Sentence, 20);
    
    cout << " Type in the number you would like to look for " << endl;
    cin  >> number;
    
    count( number, Sentence[20], 20);
    
    system("pause");
    return 0;
    
    
}
/* counts the amount of a certian number in a string */

int count(int number, char array[], int length, int counter)
{
    int length = 0;
    if (number == array[length])
               return count(number, array[], length--, counter++);
    else
        {
                         cout << " there are " << counter << " " << number << "(s) in your array" << endl;
                         return;
        }         
}



and here are the errors
-------------------------------


In function `int main()':

20 invalid conversion from `char' to `char*'

9 too few arguments to function `int count(int, char*, int, int)'

20 at this point in file

In function `int count(int, char*, int, int)':

31 declaration of 'int length' shadows a parameter

33 expected primary-expression before ']' token

37 return-statement with no value, in function returning 'int'
Last edited on
When you pass an array to a function, you pass it as if it was another normal variable. You don't need the array subscript like "Sentence[20]". You only need "Sentence". ^_^

For the "int length" error, you declare a variable named 'length' in the function when you have a parameter name 'length'.

The next error is the same as the first. Treat it the same way.

For the last error, you declare a function returning an int, but in the else{} block inside the count() function, you use "return;". That means that you aren't returning a value, where the program expects you to return an int. This is an issue with the way you designed the function. My recommendation is to return 0, return 1 or change the function so that it only returns a value rather than returning a value AND outputting the result.
Last edited on
thanks but there is still a problem


when i run the program the number of the input number is always 0


code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/// user inputs a arrya of numbers and a number to look for 
// and the computer outputs the amount of times the number shows up



#include <iostream>
using namespace std;

void count(int number, char array[], int length, int counter);
int main()
{
    // inputs number and array
    char Sentence[20];
    int number;
    cout << "type in a array: " << endl;
    cin  >> Sentence;
    
    cout << " Type in the number you would like to look for " << endl;
    cin  >> number;
    
    count( number, Sentence, 20, 0);
    
    system("pause");
    return 0;
    
    
}
/* counts the amount of a certian number in a string */

void count(int number, char array[], int length, int counter)
{
     bool Run;
     while (Run)
     {
   cout << " array " << array << endl;//*    out puts $ instead of array
 length--;
   cout << "length " << length << endl;
   cout << "array[length] " << array[length];
    if (number == array[length])
               return count(number, array, length--, counter++);
    else if (length == 0)
        {
                         cout << " there are " << counter << " " << number << "(s) in your array" << endl;
                         Run = false;
                         return;
        }
                 }              
}
Topic archived. No new replies allowed.