A while loop with a switch statement inside a for loop.

I've just started learning C++!! Any help with this would be great!

This program should simulate voting and display the result. There are 4 voting stations and 3 candidates (A, B, C) and spoilt votes (default). The For loop is supposed to iterate 4 times (as there are 4 voting stations) and the while loop that's nested inside the for loop should input the votes. 'X' should end the votes for each voting station, commencing the next iteration of the For (in other words - cycling back into the while loop)

Should display the results as:
Total candidate A: 5
Total candidate B: 3
Total candidate C: 1
Total Spoilt votes: 1.

The problem i have is that the while loop does not iterate again after X is entered, in other words the For loop only iterates once and not 4 times as set (supposedly) in the condition. In essence only 1 voting station's results are displayed.


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
36
37
 #include <iostream>
using namespace std;

int main()
{
    int votesForA = 0, votesForB = 0, votesForC = 0, spoiltVotes = 0;
    int count;
    char vote = 0;
    for ( int count = 1; count <= 4; ++count)
    {
        //cout << "Iteration " << count << "  ";
        while( vote != 'X')
        {
            cout << "Enter the vote (A, B or C. X to end):";
            cin >> vote;
                switch (vote)
                {
                    case 'A':
                    votesForA++;
                    break;
                    case 'B':
                    votesForB++;
                    break;
                    case 'C':
                    votesForC++;
                    break;
                    default:
                    spoiltVotes++;
                }
        }
    }
    cout << "Total candidate A: " << votesForA << endl;
    cout << "Total candidate B: " << votesForB << endl;
    cout << "Total candidate C: " << votesForC << endl;
    cout << "Total spoilt votes: " << spoiltVotes << endl;
return 0;
}
The problem i have is that the while loop does not iterate again after X is entered

When you reach line 12 during the second and subsequent iterations of your for loop, vote is still set to 'X so your while loop does not execute on iterations after the first.

Move line 8 to line 10. This will reinitialize vote on each iteration of the for loop.
"Move line 8 to line 10. This will reinitialize vote on each iteration of the for loop. "

This worked!!

AbstractionAnon, just like to say that I'm amazed at how well you managed to solve that problem. Not because its a difficult problem but because I didn't think it would make sense and that i just wrote a bunch of garbage!! Started C++ about a month ago so as you can imagine its all a bit overwhelming.

Of course, I would also like to thank you for your help, its much appreciated.
I was wondering if you could help me with another problem, regarding the same program. The output actually needs to print something like:

Candidate A: xxxxxx
Candidate B: xxxx
Cadidate C: xxxxxxxx
Total spoilt votes: xxxx .... depending on the amount of each candidate's votes.

So the numerical output should be converting to x's somehow. I'm not even sure if this is possible. I'm indeed hoping that the current code could be modified slightly, and that not a complete overhaul is required?
I'm assuming what you mean by x's is a histogram.

If you're dealing with small numbers (< screen width), histograms are pretty easy. Just loop for the specified number of times displaying an x.

If you're dealing with numbers larger than the screen width, you're going to want to scale each output based on the largest number of votes and the number of available columns.
By x's i mean exactly that... x's. Not a histogram!
So just print the required number of x's:
1
2
3
  for (int i=0; i<votesForA; i++)
    cout << 'x';
  cout << endl;

Rinse and repeat for B, C and spoilt.

Since you're doing this 4 times, it's a good opportunity for a function.
1
2
3
4
5
6
7
8
9
void print_xs (int cnt)
{   for (int i=0; i<cnt; i++)
      cout << 'x';
    cout << endl;
}
...
  cout << "Candidate A: ";
  print_xs (votesForA);
//  Repeat for B, C and spoilt 

Last edited on
I think I'll do it the rinse and repeat method as functions are beyond the scope of the assignment I'm busy with. Functions happen to be the very next chapter!

Now just to figure out how to concatenate those loops with the rest of the text. But don't tell me how, I'll grapple with it.

Thanks again AbstractionAnon

... I've managed to do it! Much easier than I thought!
Last edited on
Topic archived. No new replies allowed.