Counting characters

Hello guys, I'm working on program to count letter b,r and w in a string. I keep getting errors please help
THANKS






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
#include <iostream> 
#include <string> 
using namespace std;
int count(string s, char a);
int count(string s, char b);
int count(string s, char c);

{
    int bcount= 0; 
    int rcount=0;
    int wcount=0;
  
    for (int i=0;i<s.length();i++) 
        // checking character in string 
        if (s[i] == a) 
            bcount++; 
        else if (s[i] == b)
            rcount++;
         else
             wcount++;
}
int main() 
{ 

    string str;
    char a = 'b'; 
    char b = 'r';
    char c = 'w';
    cout<< "Enter string:";
    cin >> str;
    cout << "blue:"<< count(str, a) << endl; 
    cout << "red:"<< count(str, b) << endl; 
    cout << "white:"<< count(str, c) << endl; 
    return 0; 
}
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
#include <iostream>
#include <string>

using namespace std;

int count(string s, char ch)
{
    int count = 0;
    
    for (int i=0;i<s.length();i++)
        
        if (s[i] == ch)
            count++;
    
    return count;
}

int main()
{
    
    string str;
    char aa = 'b';
    char bb = 'r';
    char cc = 'w';
    cout<< "Enter string: ";
    cin >> str;
    cout << " blue: " << count(str, aa) << ' ' << aa << endl;
    cout << "  red: " << count(str, bb) << ' ' << bb << endl;
    cout << "white: " << count(str, cc) << ' ' << cc << endl;
    return 0;
}


Enter string:bratwurst
 blue: 1 b
  red: 2 r
white: 1 w
Program ended with exit code: 0
Hello guys, I'm working on program to count letter b,r and w in a string. I keep getting errors please help
What compiler are you using? Your compiler should tell you, when you compile, what your errors are and what line you can find them at.
1_ You're missing your function name before you open your brackets on line 8
2_ on line 15, if you want to check for the character a : a character is written between inverted commas, in such manner 'a'.
3_ your function isn't actually returning acount, rcount, or wcount. AND wcount will be incremented for EVERY character that ISN'T 'a' or 'r', is that what you intend?
Last edited on
I think I need to update my compiler but thanks
No worries
I think I need to update my compiler but thanks


Your compiler, whatever it is, won't help you much with the things wrong with your code.

Your main is OK, but your 3 functions show that you probably don't quite get it how functions work.

You only need 1 function header which defines the general case where you specify the string and the character to count (whatever you decide on) and then return with how many. Despite the bloopers I would imagine that you want to specify other characters, not just 'b', 'r' and 'w'. and that's what the general case allows you to do.

Read my code along with H00G0's comments and you'll get more out of it than from a compiler.

Topic archived. No new replies allowed.