It seems like my program is not running as it should. It skips the for loop and I can't see why
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 my coding:
#include <iostream>
using namespace std;
int main( )
{
const int NR_VOTING_STATIONS = 4;
int votesForA, votesForB, votesForC, spoiltVotes;
char vote;
// initialise totals
votesForA = 0;
votesForB = 0;
votesForC = 0;
spoiltVotes = 0;
// loop over the voting stations
for (int i = 1 ; i <= NR_VOTING_STATIONS ; i++ )
{
// loop over voters
while (vote != 'x')
{
cout <<"Enter your voting Choice A,B,C: ";
cin >> vote;
//if (vote = 'X')
if (vote = 'A')
votesForA = votesForA + 1;
if (vote = 'B')
votesForB = votesForB + 1;
if (vote = 'C')
votesForC = votesForC + 1;
}
}
// 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;
}
it seems like the for loop is not working if I step through the program
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 my coding:
#include <iostream>
using namespace std;
int main( )
{
const int NR_VOTING_STATIONS = 4;
int votesForA, votesForB, votesForC, spoiltVotes;
char vote;
// initialise totals
votesForA = 0;
votesForB = 0;
votesForC = 0;
spoiltVotes = 0;
// loop over the voting stations
for (int i = 1 ; i <= NR_VOTING_STATIONS ; i++ )
{
// loop over voters
while (vote != 'x')
{
cout <<"Enter your voting Choice A,B,C: ";
cin >> vote;
//if (vote = 'X')
if (vote = 'A')
votesForA = votesForA + 1;
if (vote = 'B')
votesForB = votesForB + 1;
if (vote = 'C')
votesForC = votesForC + 1;
}
}
// 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;
}
it seems like the for loop is not working if I step through the program