Voting machine

I made a voting machine, but it doesn't work right. Can you help me find whats wrong? Thanks.

Here is how it works: You type in 10 letters in a row that are all abc s, and it counts how many a s, b s, c s.

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

using namespace std;

int main()
{
    string str;
    int a=0,b=0,c=0;
    cin>>str;
    for(int num=10;num>0;num--)
    {
        if(str[num]=='a')
        {
            a++;
        }
        else if(str[num]=='b')
        {
            b++;
        }
        else if(str[num]=='c')
        {
            c++;
        }
    }
    cout<<"There are: "<<a<<" votes on a, "<<b<<" votes on b, "<<c<<" votes on c.";
    return 0;
}
Your code only looks at the 2nd through 11th characters of the input string (even if that string doesn't even have 11 characters).

Change your for loop to this:
for (int num = 0; num < str.size(); ++num)
Since you gave no description of the error, I am making an educated guess here...

You are taking a string that is 10 bytes long and indexing (in your for loop) positions 11-2. Remember that computers count starting with 0, where humans start with 1. So str[10] = position 11. Try changing your for loop as follows:
for (int num=9; num>=0; num--)
Thank you, there is no error, it doesn't count right. Now it does! Thanks!
Topic archived. No new replies allowed.