Need help with Array+Subroutines - Strings

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.
Last edited on
I'm getting this error because I'm comparing my pointer, array of chars(the word) with the letter which is not a pointer?

The problem is that the word argument in your function is not an array of chars. It is an array of strings.
string and char are two different things.
Your letter argument is incorrect as well, it should be a char instead of a string.


See here what char is: http://www.cplusplus.com/doc/tutorial/variables/
See here what string is: http://www.cplusplus.com/reference/string/string/
I thought a string=an array of chars, I'm wrong then?
I tried to use my argument "letter" as a char, but then I can't compare a string with a char on line 8. And the problem says specific "For this, create a function that receives an string and a caracter.
I thought a string=an array of chars, I'm wrong then?

Not by a long shot, but an array of chars and an std::string are two different things.

And the problem says specific "For this, create a function that receives an string and a caracter.

Exactly. A string and a char, meaning "int calcrepeats (string word, char letter, int length)". Note how word is a string, and letter is a char.

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
#include <iostream>

using namespace std;

int calcrepeats (string word, char 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;
    char 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).";
}
Ohhhh, I got it now, dang, at least I was close, thanks a lot for your help.

Also, is there a way to make it work with case sensitive?
Like, if I put the word "Lemonkiss" and the letter "l" it does not count the "L".
Topic archived. No new replies allowed.