/// 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>
usingnamespace 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'
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.
/// 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>
usingnamespace 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++);
elseif (length == 0)
{
cout << " there are " << counter << " " << number << "(s) in your array" << endl;
Run = false;
return;
}
}
}