Programme that calculates votes

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 have to be asked one after the other for which candidate (indicated by A or B or C) he or she wants to vote. X is entered when all the voters at the specific voting station have voted.

The program has the following structure:
· The three totals and the number of spoilt votes are initialised to 0.
· Now a for loop follows, 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.

This is what i have done so far and i cant get it rightpls hel :

#include <iostream>
using namespace std;
int main( )
{
const int NR_VOTING_STATIONS = 4;
int votesForA, votesForB, votesForC, spoiltVotes;
char vote;
// initialise totals
// loop over the voting stations
for (votesForA = 0; votesForA = 'A'; spoiltVotes++)
{
for (votesForB = 0; votesForB = 'B'; spoiltVotes++)
{
for (votesForC = 0; votesForB = 'C'; spoiltVotes++)
{
cout << "Which candidate do you want to choose: ";
cin >> vote "\n";
{

}

}
}
// loop over voters
while (votesForA != 'A') && (votesForB != 'B') && (votesForC != 'C')
{
cout <<"Enter which candidate you want :";
cin >> vote;
//complete the loop
}
switch (vote)
{
case 'A' :
cout << "A";
case 'B' :
cout << "B";
case 'C' :
cout << "C";
cout<< endl;
default:
case 'X' :
cout << "Spoilt Votes" << endl;
}
// display 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;
}
Last edited on
This is the lounge. You should post in Beginners if you want coding help. That said:


1
2
3
4
5
6
7
// loop over the voting stations
for (votesForA = 0; votesForA = 'A'; spoiltVotes++)
{
for (votesForB = 0; votesForB = 'B'; spoiltVotes++)
{
for (votesForC = 0; votesForB = 'C'; spoiltVotes++)
{


This doesn't make a lot of sense. It seems to be looping over voteForA from zero to 'A' (which is a character, and will be interpreted as its numerical representation, which is 65) and looping over votesForB from zero to 66, and looping over votesForC from zero to (voteForB =, which I assume is a typo) 67, but in each case you're never changing the variable you're claiming to loop over... it's just all a bit of a mess and you need to scrub all that and start again. This time, you need more planning and thinking up front.

Last edited on
Topic archived. No new replies allowed.