-1

I need the for loop to execute 4 times in the program and then to exit at the fourth time and give the total of the results.

It should not count X as a spoilt vote

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.

Here is my code

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 of INTEREST START
    //loop over the voting stations
    for ( int i = 1; i <= NR_VOTING_STATIONS; i++)
    {

        //loop over voters
        while (vote != 'X')
        {
            cout << "Vote for candidate A, B or C : " << endl;
            cin  >> vote;

            switch(vote)
            {
                  case 'A':
                       votesForA++;
                       break;
                  case 'B':
                       votesForB++;
                       break;
                  case 'C':
                       votesForC++;
                       break;
                  default:
                       spoiltVotes++;
            }                     
        }
    }
    // LOOP of INTEREST END

    //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; 

    system("pause");
    return 0;
}

Thanks

7
  • 2
    The for loop looks fine to me and will in fact execute four times. So what exactly is your problem? Commented Mar 25, 2012 at 10:09
  • why put ** around for loop construct? Commented Mar 25, 2012 at 10:11
  • make your question more clear.... Commented Mar 25, 2012 at 10:12
  • What does 'not working correctly' mean? It is of great assistance to the experienced developers here to know exactly what is wrong - what does not happen that should happen, what happens that should not happen, any error messages. Also, you should say what you have done to debug this yourself and what the results of this debugging were. Commented Mar 25, 2012 at 10:12
  • @Shiplu I guess this is attempt to make part of code bold but SO render engine doesn't support this. I've seen such here multiple times. Commented Mar 25, 2012 at 10:13

5 Answers 5

1

just add in the switch:

case 'X':
    break;

because the while condition will not be executed until the next round.

Sign up to request clarification or add additional context in comments.

Comments

1

If the question is

It should not count X as a spoilt vote

The answer would be to add a case:

case 'X':
     break;

Comments

1

Also, be careful to initialize vote first, or use do { } while() rather than while { }

Comments

0

If you need the for loop to execute four times why not just surround it in another for loop?

Comments

0

In the while loop you should check whether the Voters in that polling sttion are still available. Since you are neutral at the input 'X', you should add it as one of the cases in the switch.

Your new while loop should look like:

Voters_Still_Present = 1;
while (Voters_Still_Present)
    {
        cout << "Vote for candidate A, B or C : " << endl;
        cin  >> vote;

        switch(vote)
        {
              case 'A':
                   votesForA++;
                   break;
              case 'B':
                   votesForB++;
                   break;
              case 'C':
                   votesForC++;
                   break;
              case 'X':
                   //do nothing
                   break;
              default:
                   spoiltVotes++;
        }  
       /* Here, find out if voters are still available in the present station.
          If yes, Voters_Still_Present = 1; else  Voters_Still_Present = 0;
        */            
    }

4 Comments

I need the program to run the loop 4 times and each time store the results and at the end of the fourth time is will then display the totals of each candidate. Thanks
Goodday Ahmed, I have declared Voters_Still_Present as an int created a if statement. I am new at this so please excuse me. if (Voters_Still_Present = 1) { cout << ""; } else { Voters_Still_Present = 0; }
Please join the chatroom chat.stackoverflow.com/rooms/9327/c. we could discuss the question further.
Hi Ahmed, I am in the chatroom now

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.