Nested switch statement
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 26

Thread: Nested switch statement

  1. #1
    Join Date
    Mar 2009
    Posts
    15

    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

  2. #2
    Join Date
    Oct 2002
    Location
    Austria
    Posts
    1,284

    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

  3. #3
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,047

    Re: Nested switch statement

    I don't see a nested switch statement.

  4. #4
    Join Date
    Mar 2009
    Posts
    15

    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?

  5. #5
    Join Date
    Mar 2009
    Posts
    94

    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.

  6. #6
    Join Date
    Mar 2009
    Posts
    15

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

  7. #7
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,269

    Re: Nested switch statement

    Quote Originally Posted by TheStorm View Post
    Maybe this reads better:

    Code:
    #include <iostream>
    using namespace std;
    
    int main ()
    {
    ....
    }
    Sure using Code tags is much better!

    However, I agree with GCDEF:
    Quote Originally Posted by GCDEF View Post
    I don't see a nested switch statement.
    Victor Nijegorodov

  8. #8
    Join Date
    Mar 2009
    Posts
    15

    Red face 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.

  9. #9
    Join Date
    Mar 2009
    Posts
    94

    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?

  10. #10
    Join Date
    Aug 2008
    Location
    Scotland
    Posts
    379

    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

  11. #11
    Join Date
    Aug 2008
    Location
    India
    Posts
    186

    Lightbulb 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.

  12. #12
    Join Date
    Mar 2009
    Posts
    15

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

  13. #13
    Join Date
    Mar 2009
    Posts
    15

    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.

  14. #14
    Join Date
    Aug 2008
    Location
    India
    Posts
    186

    Thumbs up 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');
    }

  15. #15
    Join Date
    Mar 2009
    Posts
    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.

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center