Hello. I'm stuck on a problem, and would appreciate some help to resolve it.
I need to create this:
"Create a C++ program that counts the number of times that a char appears on a string."
I need to use subroutines to resolve it, and basic stuff. I think I'm almost there, I just need to figure out why my program it's not working.
Here's what I got so far:
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
|
#include <iostream>
using namespace std;
int calcrepeats (string word[], string letter, int length){
int repeats=0;
for (int cont=0; cont<length; cont++){
if (word[cont]==letter)
repeats=repeats+1;
}
cout << repeats;
return repeats;
}
int main()
{
string word, letter;
cout << "Inform a word: ";
cin >> word;
cout << "Inform the char to be analyzed: ";
cin >> letter;
int length=word.length();
int repeats=calcrepeats(word, letter, length);
cout << "The char appeared " << repeats << " time(es).";
}
| |
The problem is, I get an error on line 23.
"cannot convert 'std::string {aka std::basic_string<char>}' to 'std::string* {aka std::basic_string<char>*}' for argument '1' to 'int calcrepeats(std::string*, std::string, int)'"
I've tried other things, none of them worked, I'm getting this error because I'm comparing my pointer, array of chars(the word) with the letter which is not a pointer? Any ideas on how to fix that? Thanks.