switch() statement. What's the smartest way to repeat it?

This is a discussion on switch() statement. What's the smartest way to repeat it? within the C++ Programming forums, part of the General Programming Boards category; Hi everyone. I thought of a few ideas, on how to make the switch statement repeat, if the person chose ...

  1. #1
    Novice programmer newn's Avatar
    Join Date
    Aug 2010
    Posts
    59

    switch() statement. What's the smartest way to repeat it?

    Hi everyone.
    I thought of a few ideas, on how to make the switch statement repeat, if the person chose the wrong number, which isn't in the switch case. Like so:

    Code:
    cout << "what will you choose, 1, 2 or 3?" << endl;
    
    switch(choice)
    {
    case 1:
    dosomething();
    break;
    case 2:
    dosomethingelse();
    break;
    case 3:
    quit();
    breal;
    default:
    cout << "Wrong Choice, Pal! Choose again.";
    break;
    }
    So at the point, where case isn't 1, 2 or 3, it should repeat the cout question and the switch statement.

    So anyway, all my ways has some downsides. Like repeating of code, double code, long code, or unnecesary code.
    So anyway, I'd like to know, how it's supposed to be done with no mistakes.
    Thanks.

  2. #2
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,641
    Repeating == loop.

  3. #3
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,855
    Code:
    bool bExit = false;
    do
    {
         switch(choice)
         {
              case 1:
                   dosomething();
              break;
              case 2:
                   dosomethingelse();
              break;
              case 3:
                   bExit = true;
              breal;
              default:
                   cout << "Wrong Choice, Pal! Choose again.";
              break;
         }
    } while (!bExit);
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  4. #4
    Novice programmer newn's Avatar
    Join Date
    Aug 2010
    Posts
    59
    Thanks. I made it so myself, but it wasn't working. I found out, that there was some other code left... Note to self: always check the WHOLE code, before asking stupid questions...

  5. #5
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,586
    case 3:
    bExit = true;
    breal;
    I got a chuckle out of this one. I think that is supposed to be break.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. issue with switch statement
    By bluetxxth in forum C Programming
    Replies: 14
    Last Post: 02-24-2010, 09:01 AM
  2. Switch Statement Errors
    By melodia in forum C Programming
    Replies: 7
    Last Post: 10-25-2009, 03:51 PM
  3. Mutli Switch statement help
    By elwad in forum C Programming
    Replies: 9
    Last Post: 05-09-2009, 03:19 AM
  4. Switch statement / default:
    By kcpilot in forum C Programming
    Replies: 4
    Last Post: 12-02-2008, 02:14 PM
  5. begginner and I need help, switch statement
    By trkpony in forum C Programming
    Replies: 3
    Last Post: 04-24-2003, 08:03 AM

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