Another C++ Problem

deetlej1

Active Member
Joined
Dec 7, 2008
Messages
38
Reaction score
0
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
 
should be == not =

== equality comparison operator
= is the equality assignment operator.

(x = 'A') evaluates to true ALWAYS, provided that the assignment is successful (which in most situations, it would be.
 
if I change '=' to '==' it is still just keeps on asking to enter you're voting choice. it not counting the votes to display it
 
Fixed and tested working code
Code:
#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;
///////////////////////////////////////////
//Remember to initialise this too!/////////
//////////////////////////////////////////
vote = '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;
////////////
//NOTE!///
///////////
		if (vote == 'X')
			break;
////////////
//NOTE!///
///////////
		else if (vote == 'A')
			votesForA += 1;
////////////
//NOTE!///
///////////
		else if (vote == 'B')
			votesForB += 1;
////////////
//NOTE!///
///////////
		else if (vote == 'C')
			votesForC += 1;
////////////
//NOTE!///
///////////
		else
			spoiltVotes += 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;
}
 
Thx Sn3rd, now this makes sense. do you have time for more questions :-)
 
it seems like it is the basic stuff that bits me.... the next one is also suppose to be easy.

Suppose we want to input and validate three integers in a do..while loop. The variable names are n1, n2 and n3. The requirement is that the sum of the three integers is less than 1000 or that n3 is greater than 100. If this condition does not hold, the loop has to be executed again. Write down a correct condition for the do..while loop.

My Coding:

#include <iostream>
using namespace std;

int main()
{
int n1, n2, n3, sum;

do
{
cout<<"Enter a number:";
cin>>n1;
cout<<"Enter a second Number:";
cin>>n2;
cout<<"Enter a third Number:";
cin>>n3;

sum = n1 + n2 + n3;

//loop if sum is less than 1000 OR n3 is greater than 100
} while ((n3 <= 100) && (sum >= 1000));

return 0;

}
 
Replace && with ||

&& means "AND" which means that both conditions must be satisfied.
|| means "OR" which means that at least one condition must be satisfied.
 
as I thought it is just some basic stuff... I tested the program but it still seems like the while is not working. I changed it to

while ((n3 >= 100) || (sum <= 1000));

and still it keep on asking to enter a number

the question states..
the sum of the three integers is less than 1000 or that n3 is greater than 100.
 
Sorry, I was wrong... it SHOULD be &&, because you want n3 to be less than 100 AND you want the sum to be less than 1000.

And hence:
Code:
while ((n3 >= 100)&&(sum <= 1000))
 
Actually I'm confused as to what exactly you want.

As per your first post:
The requirement is that the sum of the three integers is less than 1000 or that n3 is greater than 100. If this condition does not hold, the loop has to be executed again.
Code:
do
{
} while ((n3 < 100)||(sum > 1000));

In words:
If EITHER n3 is less than 100 OR the sum of n1, n2 and n3 is greater than 1000, go through the loop again.
 
are universities not teaching there students properly anymore that they need to ask other developers to do the work for them:P
 
So if we've got question we don't ask anyone and then just suffer in silence?
 
Actually, since he is writing most of the code, and just making small mistakes, why not help?

IF someone says, i have this problem adn the programme needs to do this, help...then screw em, but if they are trying and get stuck, why not?
 
Top
Sign up to the MyBroadband newsletter
X