nested loops, with switch statement

hi there, ive got the following problem;

there are 4 voting stations.
3 candidates to vote for.
three totals and the number of spoilt votes are initialised to 0.
for loop follows, going from 1 to the number of voting stations.
inside while loop a message appear on the screen asking voters which candidate to vote for.
inside while loop is also a switch statement to increment the correct total.
while loop is exited when x is entered.

after the loops, all votes are displayed for each candidate.
ive written the following so far:

#inlcude <iostream>
using namespace std;
int main ()
{
const int NR_VOTING_STATIONS = 4;
int votesForA, votesForB, votesForC, spoiltVotes;
char vote;

// initialize totals
votesForA = 0;
votesForB = 0;
votesForC = 0;
spoiltVotes = 0;

//loop over the voting stations
for (int i = 1; i < NR_VOTING_STATIONS; i++)
{
// while loop over voters
while ()
{
cout << "Which candidate would you like to vote for (A, B, C): ";
cin >> votesForA >> votesForB >> votesForC >> spoiltVotes;

switch ()
{
}
}
}

//display the results
cout << endl << endl;
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;
}

now i need help with the for loop, while loop and switch statement. is my for loop right? i am struggeling how to write my while condition and need tips on how to use a switch statement.
Last edited on
1
2
cout << "Which candidate would you like to vote for (A, B, C): ";
cin >> votesForA >> votesForB >> votesForC >> spoiltVotes;


This looks like each voter will enter the number of votes they'd like for each candidate, and also how many votes they'd like to spoil. That doesn't seem right.

1
2
3
4
5
6
7
8
9
cout << "Which candidate would you like to vote for (A, B, C): ";
cin >> vote;

switch (vote)
{
  case 'A': ++votesForA;
                break;
  etc. etc. 
}
thanx man, i appreciate it, i will go check it out.
and about my for loop? is the condition right? or should i change it, and what do i have to put my while loop for the condition? maybe an example would be nice, my 1st time using c++>
The problem is that you ask the question, "Which candidate would you like to vote for (A, B, C)?"; and try to read into four values.

You need to collect the answer one variable (not four), lets call it response. You then check if the variable holds A, B, C or neither and increment votesForA, votesForB, votesForC or spoiltVotes respectively,
You need to stop coding and start thinking. The problem should be solved (at least inside your head) before you start coding it. You tell us what the while loop should do; if you struggle with the syntax, we can help, but there's no point in us just telling you what to write.
ok ive got everything right, eccept now my while loop has to end when i press X. Is that part of the switch statement or on its own?
Topic archived. No new replies allowed.