-
March 23rd, 2009, 03:00 PM
#1
Nested switch statement
Yet another question from a student. For a assignment I was asked to create a program that will calculate the amount of votes in a election. There are four voting stations and three candidates.
The program compiles ok but the totals I get are not correct. Somehow it only counts some of the characters. I am stumped, any help would be much appreciated.
Code:
#include <iostream>
using namespace std;
int main ()
{
const int NR_VOTING_STATIONS = 4;
int votesForA, votesForB, votesForC, spoiltVotes;
char vote;
//Initialize 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
cout << "Who are you voting for (A,B or C)? " << endl;
cin >> vote;
while ( vote != 'X' && vote != 'x')
{
cin >> vote;
switch (vote)
{
case 'A':
case 'a':
votesForA++;
break;
case 'B':
case 'b':
votesForB++;
break;
case 'C':
case 'c':
votesForC++;
break;
default:
spoiltVotes++;
break;
}
}
}
// display results
cout << 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;
}
Last edited by Brad Jones; March 24th, 2009 at 01:49 PM.
Reason: added code tag
-
March 23rd, 2009, 03:07 PM
#2
Re: Nested switch statement
Code:
cout << "Who are you voting for (A,B or C)? " << endl;
cin >> vote; // This vote will never be counted
Kurt
-
March 23rd, 2009, 03:10 PM
#3
Re: Nested switch statement
I don't see a nested switch statement.
-
March 24th, 2009, 12:01 AM
#4
Re: Nested switch statement
Even if I exclude the vote in:
cout << "Who are you voting for (A,B or C)? " << endl;
cin >> vote; // This vote will never be counted
The votes still dont add up also how would i include the firts vote?
-
March 24th, 2009, 04:15 AM
#5
Re: Nested switch statement
I think you could include the first vote by putting the second
cin >> vote
(the one in the while loop) below the switch statement. That way you wouldnt overvrite the first vote before evaluating it.
Have you tried debugging? You wrote that it counts only some of the characters. There must be a pattern...
Also if you use the [code] [/code] tags you get formated code in your post.
-
March 24th, 2009, 04:29 AM
#6
Re: Nested switch statement
Maybe this reads better:
Code:
#include <iostream>
using namespace std;
int main ()
{
const int NR_VOTING_STATIONS = 4;
int votesForA, votesForB, votesForC, spoiltVotes;
char vote;
//Initialize 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
cout << "Who are you voting for (A,B or C)? " << endl;
cin >> vote;
while ( vote != 'X' && vote != 'x')
{
cin >> vote;
switch (vote)
{
case 'A':
case 'a':
votesForA++;
break;
case 'B':
case 'b':
votesForB++;
break;
case 'C':
case 'c':
votesForC++;
break;
default:
spoiltVotes++;
break;
}
}
}
// display results
cout << 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;
}
-
March 24th, 2009, 04:35 AM
#7
Re: Nested switch statement
Originally Posted by TheStorm
Maybe this reads better:
Code:
#include <iostream>
using namespace std;
int main ()
{
....
}
Sure using Code tags is much better!
However, I agree with GCDEF:
Originally Posted by GCDEF
I don't see a nested switch statement.
Victor Nijegorodov
-
March 24th, 2009, 04:47 AM
#8
Re: Nested switch statement
You are right, what I meant to say was nested while statement. As I assume you know by now this is all pretty new to me and all the terms are at times confusing.
-
March 24th, 2009, 04:56 AM
#9
Re: Nested switch statement
It is pretty new to me too
So have you tried debuging?
How off is the result you get from what you expect?
-
March 24th, 2009, 05:46 AM
#10
Re: Nested switch statement
Hi,
This is actually quite a common situation, you want to use a while() loop, but need to take care about how you run it the first time. You could use
Code:
do
{// read input and deal with it
} while (input != X);
or you could use
Code:
// read input
while ( input != X)
{
// deal with input
// read input
}
So, the simplest way to fix your code is to move the 2nd "cin >> vote" to the end of the while loop (like w3rd says.) That way the vote you read before starting the while loop won't be ignored.
Alan
-
March 24th, 2009, 06:57 AM
#11
Re: Nested switch statement
May be you can write your code this way... its somewhat clear about the station the votes are entered....
Code:
ForLoop(....)
{
cout << "At station " << i << endl;
do
{
cout << "Who are you voting for (A,B or C)? " << endl;
cin >> vote;
if(vote == 'x' || vote == 'X')
break;
switch (vote)
{
case 'A':
case 'a':
votesForA++;
break;
case 'B':
case 'b':
votesForB++;
break;
case 'C':
case 'c':
votesForC++;
break;
default:
spoiltVotes++;
break;
}
}while(1);
}
Last edited by LOOSER_007; March 24th, 2009 at 07:29 AM.
-
March 24th, 2009, 07:02 AM
#12
Re: Nested switch statement
Changing the code to below worked. Thank you for your help.
Code:
//Loop over voters
cout << "Who are you voting for (A,B or C)? " << endl;
cin >> vote;
while ( vote != 'X' && vote != 'x')
{
switch (vote)
{
case 'A':
case 'a':
votesForA++;
break;
case 'B':
case 'b':
votesForB++;
break;
case 'C':
case 'c':
votesForC++;
break;
default:
spoiltVotes++;
break;
}
cin >> vote;
}
}
-
March 24th, 2009, 07:13 AM
#13
Re: Nested switch statement
One last thing I need the code to see any character other than A B C as a spoilt vote. This should include X, as it stands now it reads every character except A B C and X as a spoilt vote.
-
March 24th, 2009, 07:27 AM
#14
Re: Nested switch statement
Use do-While loop to include 'X' in Spoilt votes....
Code:
for()
{
do
{
cout<<"enter vote:"<<endl;
cin>>vote;
switch(vote)
{
case 'A':
.....
.....
default:
spoiltVotes++;
break;
}
}while(vote != 'X' && vote != 'x');
}
-
March 24th, 2009, 07:34 AM
#15
Re: Nested switch statement
Thank you but the original assignment states that I can not use do..while. Part of the construct is given and it does not include do...while statemts.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|