My code is below. I've used this layout and coding before with no problems but unfortunately this
won't work and after hours of trying to spot the logic error, I'm still stuck.
This is my question: Suppose a new member of the city council has to be chosen from three candidates and suppose there are 4 voting stations. We need a C++ program that will count the votes for every candidate and display the result.
At every voting station the voters vote by choosing A, B or C on a ballot paper. The voting officer must enter the votes into the program so that they can be counted. X is entered when all the votes at a specific voting station have been entered. The program has the following structure:
The three totals and the number of spoilt votes are initialised to 0. Use the following integer variables votesForA, votesForB, votesForC, spoiltVotes
Use a for loop, going from 1 to the number of voting stations.
Inside this loop is a while loop. A prompting message appears on the screen, asking the voter for which candidate he or she wants to vote. The choice of the voter is then input.
Inside the while loop is a switch statement to increment the correct total. The default option is used to count the number of spoilt votes.
The while loop is exited when X is entered for the choice.
When the for loop is exited, the three totals and the number of spoilt votes are displayed.
Display the results as follows:
Total candidate A: xxx Total candidate B: xxx Total candidate C: xxx Total spoilt votes: xxx
Run your program with the input given below and submit printouts of the program and output. (We write the data of each voting station in one line, but you will possibly enter the values on separate lines.)
B B B A C X)
I would appreciate any help. Thanks
#include <iostream>
using namespace std;
int main()
{
int votesForA=0,votesForB=0, votesForC=0, spoiltVotes=0, xTotal=0;
const int STATIONS=4;
const char SENT='X';
const char SENTINEL='X';
char vote;
for(int v=1;v<=STATIONS;v++)
{
cout << "For Governor A press 'A'| For Governor B press"
<< "'B'| For Governor C press 'C'|Press 'X' to Exit"<<endl;
cout << "Which candidate would you like to vote for?"<<endl;
cin >> vote;
while((vote!= SENT) || (vote!=SENTINEL))
{
switch (vote)
{
case 'a':
case 'A':
votesForA = votesForA+1;
break;
case 'b':
case 'B':
votesForB = votesForB+1;
break;
case 'c':
case 'C':
votesForC = votesForC+1;
break;
case 'x':
case 'X':
xTotal++;
break;
default:
spoiltVotes +=1;
}
{
cout << "For Governor A press 'A'| For Governor B press"
<< "'B'| For Governor C press 'C'|Press 'X' to Exit"<<endl;
cout << "Which candidate would you like to vote for?"<<endl;
cin >> vote;
}
}
}
cout <<"Total candidate A : " << votesForA << endl;
cout <<"Total candidate B : " << votesForB << endl;
cout <<"Total candidate C : " << votesForC << endl;
spoiltVotes = spoiltVotes - xTotal;
cout <<"Total spoilt votes: " <<spoiltVotes<< endl;
return 0;
}