0

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;
}
Ken White
  • 123,280
  • 14
  • 225
  • 444
Mackas
  • 1
  • 1
  • What is not working specifically? Two things stand out to me but I could be wrong on both. 1. What is the point of having two sentinel variables that have the same value? Shouldn't one of them be a lowercase 'x'? 2. I do not see how case 'x' or case 'X' would be reached if the whole point of the user entering an 'x' is to break out of the loop. – The_Redhawk Apr 22 '21 at 01:36
  • Hi The_Redhawk. It should be a Cap and lower case, thanks. I changed too many things and somehow changed that too the same thing. The while loop is a sentinel driven loop and so the purpose of the condition is to allow a user to exit the loop by entering a specific character in this case. It turns out the change I needed was to replace || with &&. I tried this yesterday but unfortunately I may have also tried too many other changes around the same time. Thanks for your reply. – Mackas Apr 22 '21 at 05:11

0 Answers0