Thread: default switch statement not working

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    76

    default switch statement not working

    how come... my default in my switch statement doesn't execute if the user doesn't input anything?

    Code:
    void Menu()
    {
         int answer;
         cout<<"\n<>Phone Book Entry Menu:";
         cout<<"\n  <1>Create New File";
         cout<<"\n  <2>Open File";
         cout<<"\n  <3>Write to File";
         cout<<"\n  <4>Delete File";
         cout<<"\n  <5>Quit";
         cout<<"\n  ->: ";
         cin>>answer;
         switch(answer)
         {
                       case 1:
                            MCreateFile();
                            break;
                       case 2:
                            cout<<"\nMOpenFile();"; // <+>MOpenFile();
                            break;
                       case 3:
                            cout<<"\nMWriteToFile();"; // <+>MWriteToFile();
                            break;
                       case 4:
                            cout<<"\nMDeleteFile();";  // <+>MDeleteFile
                            break;
                       case 5:
                            return;
                            break;
                       default:
                               cout<<"\nNot a valid entry!";
                               cout<<"\n  ->: ";
         }
    }

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Well the cin statement you have is pretty smart. If you kept pressing Enter or just put in a bunch of spaces, cin will wait until the user types something that has meaning.

  3. #3
    Registered User
    Join Date
    Apr 2005
    Posts
    76
    o... right... the switch statement doesn't check if enter is being pressed...thats cin's job...
    ok... how could i put cin<<answer; in a while loop...
    ive already tried and failed...
    something like
    Code:
    while(cin<<answer)
    {
         cout<<"\n->: ";
    }

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    cin uses the insertion operator >>. When you want to loop input, you must make an initial attempt to read something in, then if that fails, you can enter a while loop. Basically you want to:
    - check the state of cin
    - cin.clear(); reset all of cin's flags to make it okay for reuse
    - ignore as many characters as you can
    - try to get input again.
    - lather, rinse, repeat
    Last edited by whiteflags; 05-13-2006 at 10:19 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Error proofing my switch statement.
    By Shamino in forum C++ Programming
    Replies: 10
    Last Post: 01-04-2008, 04:38 PM
  2. switch statement
    By crash88 in forum C Programming
    Replies: 4
    Last Post: 06-29-2006, 12:49 AM
  3. Equivalent of less than in a switch statement?
    By Diamonds in forum C++ Programming
    Replies: 5
    Last Post: 10-14-2002, 07:14 PM
  4. switch not working
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 01-22-2002, 07:25 AM
  5. switch statement / command line
    By Unregistered in forum C Programming
    Replies: 5
    Last Post: 12-20-2001, 04:21 PM