Thread: Stuck on tutorial number 5 (basics): switch cases

  1. #1
    Registered User
    Join Date
    Jun 2011
    Posts
    13

    Stuck on tutorial number 5 (basics): switch cases

    http://www.cprogramming.com/tutorial/lesson5.html

    Right at the bottom, it says
    ...you can make a loop around the whole thing to have it wait for valid input.
    How would I go about doing this? I suppose the do..while loop would be best, so I tried that and here's my best shot (it doesn't work):

    Code:
    #include <iostream>
    
    using namespace std;
    
    void playgame()
    {
        cout << "Play game called";
    }
    void loadgame()
    {
        cout << "Load game called";
    }
    void playmultiplayer()
    {
        cout << "Play multiplayer game called";
    }
    
    int main()
    {
      int input;
    do {
      cout<<"1. Play game\n";
      cout<<"2. Load game\n";
      cout<<"3. Play multiplayer\n";
      cout<<"4. Exit\n";
      cout<<"Selection: ";
      cin>> input;
      switch ( input ) {
      case 1:            // Note the colon, not a semicolon
        playgame();
        break;
      case 2:            // Note the colon, not a semicolon
        loadgame();
        break;
      case 3:            // Note the colon, not a semicolon
        playmultiplayer();
        break;
      case 4:            // Note the colon, not a semicolon
        cout<<"Thank you for playing!\n";
        break;
      }
    } while (input=0);
      cin.get();
    }
    Thanks for all responses... I'm very new and learning the basics so please be kind!
    Last edited by rwebb2305; 06-12-2011 at 07:29 PM.

  2. #2
    Registered User jdragyn's Avatar
    Join Date
    Sep 2009
    Posts
    96
    Code:
    while (input=0);
    = assigns. == compares.
    C+/- programmer extraordinaire

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    13
    OK, that helps a lot, but the program still doesn't function as I want it to.

    It's now set to:
    Code:
    } while (input==0);
    But when I enter a value that comes to 0, the loop isn't initiated and the program ends. Can you help me understand why that is?

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    But when I enter a value that comes to 0, the loop isn't initiated and the program ends. Can you help me understand why that is?
    You are in fact in the loop. Look at your switch code, though. There isn't a case for input being 0, so it is like nothing happens, and the loop is terminated, as expected.

  5. #5
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    It's your loop condition. Better make it:
    Code:
    } while (input != 4);
    EDIT: And get a "cin.ignore();" before "cin.get();" if you want the program to wait for enter. ( "cin >> input;" has left the newline character behind )
    Last edited by GReaper; 06-13-2011 at 05:45 AM.
    Devoted my life to programming...

  6. #6
    Registered User
    Join Date
    Jun 2011
    Posts
    13
    I want it to loop if there is invalid input, so that if something irrelevant is entered, nothing happens and it just waits for a correct response.

    The
    Code:
    input == 0
    must be wrong, but I'm not sure how to put
    Code:
    input = no valid input
    If I put
    Code:
    input != 1 || 2 || 3 || 4
    it allows me to change my input and it doesn't close after I enter an invalid input, so I'm nearly there. However, it doesn't close if I enter 1,2,3 or 4, which I want it to. Any further help?

  7. #7
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Then:
    Code:
    } while (input >= 1 && input <= 4);
    Devoted my life to programming...

  8. #8
    Registered User
    Join Date
    Jun 2011
    Posts
    13
    Quote Originally Posted by Sipher View Post
    Then:
    Code:
    } while (input >= 1 && input <= 4);
    That makes it so I do the loop when I input a correct value. My aim is for the program to shut down if it's correct, but ask again if an incorrect value is given, until a correct value is entered.

    I tried
    Code:
    } while (input < 1 && input > 4);
    but it still doesn't work properly, it shuts down whatever value I enter.

    EDIT:
    Code:
    } while (input < 1 || input > 4);
    has done the trick, thanks for everyone's help!
    Last edited by rwebb2305; 06-13-2011 at 06:03 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using Functions/Switch Cases? (in my code)
    By smogsy in forum C Programming
    Replies: 1
    Last Post: 03-02-2011, 03:04 AM
  2. Question about switch cases
    By cashmerelc in forum C Programming
    Replies: 5
    Last Post: 09-21-2007, 11:57 PM
  3. switch cases and timers
    By soranz in forum C++ Programming
    Replies: 5
    Last Post: 10-02-2005, 06:43 PM
  4. switch cases and do while
    By exoillusion in forum C Programming
    Replies: 7
    Last Post: 07-28-2003, 07:18 AM
  5. could someone please explain switch cases?
    By .exe in forum C++ Programming
    Replies: 7
    Last Post: 07-01-2003, 01:42 PM

Tags for this Thread