Hi

Any reason why my votes aren't adding up once the program has run?

#include <iostream>
using namespace std;

int main ( )
{
	const int NR_SESSIONS = 3;
	int nrAttend;
	int votesForS, votesForR, votesForO;
	char vote;
    
	// Initialise totals
	votesForS = 0;
	votesForR = 0;
	votesForO = 0;

	// Loop over sessions
	for (int i = 1; i <= NR_SESSIONS; i++)
	{
		cout << "SESSION " << i << endl;
		cout << "How may pupils attended this session? " << endl;
		cin >> nrAttend;
		cout << endl;
        
	// Loop over attendants  
	    cout << "Vote 'S' for Scarlett, 'R' for Rhett and 'O' for any other character! " << endl << endl;
        cout << "Which character would you like to vote for? " << endl;
		cin >> vote;

		for (int j = 1; j < nrAttend; j++)
		{
			switch (vote)
			{
			case 'S':
				votesForS++; 
				break;
			case 'R':
				votesForR++; 
				break;
			default:
				votesForO++;
			}
   
			cout << "Next: For whom do you vote? ";
			cin >> vote;
			cout << endl;
		}
	}

	// Displays results
	cout << endl << endl;
	cout << "Votes for Scarlet:  " << votesForS << endl;
	cout << "Votes for Rhett:  " << votesForR << endl;
	cout << "Votes for Other:  " << votesForO << endl;
    
	return 0;
}

You need to put the retrtieval of the vote (cin) before the incrementing of the vote. As it stands it will add one to "Other" (Default) before it takes the first vote, and then it will drop the last vote.

Pseudocode as it stands now:
Loop through votes
Add 1 vote to their choice
Ask for their choice
End Loop

What you want:
Loop through votes
Ask for their choice
Add 1 vote to their choice
End Loop

Ah yes, I should've spotted that howler when I helped you yesterday!

For the last people you ask, their votes are not getting counted..
What you need to do is ask for input before the switch, so perhaps try something like this:

#include <iostream>
using namespace std;

int main()
{
	const int NR_SESSIONS = 3;
	int nrAttend;
	int votesForS, votesForR, votesForO;
	char vote;
    
	// Initialise totals
	votesForS = 0;
	votesForR = 0;
	votesForO = 0;

	// Loop over sessions
	for (int i = 0; i < NR_SESSIONS; i++)
	{
		cout << "SESSION " << i+1 << endl;
		cout << "How may pupils attended this session? " << endl;
		cin >> nrAttend;
		cout << endl;
        
	// Loop over attendants  
	    cout << "Vote 'S' for Scarlett, 'R' for Rhett and 'O' for any other character! " << endl << endl;
		for (int j = 0; j < nrAttend; j++)
		{
			cout << "For whom does pupil " << j+1 << " vote? ";
			cin >> vote;
			cout << endl;
			switch (vote)
			{
			case 'S':
				votesForS++; 
				break;
			case 'R':
				votesForR++; 
				break;
			default:
				votesForO++;
			}
		}
	}

	// Displays results
	cout << endl << endl;
	cout << "Votes for Scarlet:  " << votesForS << endl;
	cout << "Votes for Rhett:  " << votesForR << endl;
	cout << "Votes for Other:  " << votesForO << endl;
    
	return 0;
}

My bad, I should've spotted that yesterday!

Cheers for now,
Jas.

Edited 7 Years Ago by JasonHippy: pytos and smelling pistakes

Hey Jason, No problem. Thanks alot to you both....

Have another one if you guys are keen look under my thread - VALUE RETURNING FUNCTIONS WITH ONE OR MORE VALUE PARAMETERS.

Have a great weekend....

Btw, You should also note that the Votes are CASE-SENSITIVE too. So you will need to check if CAPS-LOCK is on or else the votes would all go to VoteTo0.

This article has been dead for over six months. Start a new discussion instead.