switch statement inside a do while

This is a discussion on switch statement inside a do while within the C++ Programming forums, part of the General Programming Boards category; The exit condition of my do while loop is while the char variable choice is not equal to q or ...

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    153

    switch statement inside a do while

    The exit condition of my do while loop is while the char variable choice is not equal to q or to Q...however when i enter q it displays the default case in the switch and doesnt exit the loop...here's the 1st half of the loop...
    Code:
        {
            cout << "Benevolent Order of Programmers Report\n"
                 << "a. Display by name\t     b. Display by title\n"
                 << "c. Display by bopname\t  d. Display by Preference\n"
                 << "q. Quit\n";
            cout << "Your choice? ";
            cin  >> choice;
            cout << "\n";
            switch(choice)
            {
                case 'A':
                case 'a':  cout << s_known[0].fullname << endl
                                << s_known[1].fullname << endl
                                << s_known[2].fullname << endl;
                           cout << "\n";
                                break;
                case 'B':
                case 'b':  cout << s_known[0].title << endl
                                << s_known[1].title << endl
                                << s_known[2].title << endl;
                           cout << "\n";
                                break;

  2. #2
    Registered User
    Join Date
    Sep 2004
    Posts
    153

    the 2nd part...

    Code:
     default:   cout << "You entered an invalid option!\n";
                           break;
            }
    
        } while (choice != 'q' || choice != 'Q');
        cout << "BYE!\n";
    And here's the very end of it right after my last case in the switch...the "BYE" is never reached, any idea what's going wrong? Thanks for all the help everyone -Chap

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    Code:
    bool Continue = true;
    while(Continue)
    {
       //menu
       switch (choice)
       {
    	  ....
    	  case 'Q':
    	  case 'q':
    		cout << "BYE" << endl;
    		Continue = false;
    		break;
    	  default:
    		cout << "invalid option" << endl;
       }
    }

  4. #4
    Registered User
    Join Date
    Sep 2004
    Posts
    153
    thanks for the help....I'm just curious why it isn't exiting out the way I did it...is it stuck inside the switch statement? That wouldn't make sense though because if the user inputs q right off the bat it should skip the entire loop body, right?

  5. #5
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    try && instead of || in the while conditional. Since choice can't be 'q' and 'Q' at the same time either choice != 'q' or choice != 'Q' has to be true, so the || will always be true. Whereas the && version will only be true if choice not equal to either 'q' or 'Q'.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting the switch statement to work.
    By mtymightymike in forum C Programming
    Replies: 7
    Last Post: 10-15-2008, 07:32 PM
  2. Stack operations from switch statement elements
    By mlsrar in forum C Programming
    Replies: 15
    Last Post: 10-02-2008, 02:12 PM
  3. switch case statement
    By stanlvw in forum C++ Programming
    Replies: 3
    Last Post: 02-26-2008, 05:06 AM
  4. Switch statement
    By beene in forum C++ Programming
    Replies: 21
    Last Post: 07-01-2007, 09:13 AM
  5. Switch
    By cogeek in forum C Programming
    Replies: 4
    Last Post: 12-23-2004, 06:40 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21