Thread: Looping a Switch Case

  1. #1
    Registered User
    Join Date
    Jan 2007
    Posts
    2

    Looping a Switch Case

    Good day everyone, I'm new to C++ programming and was following the tutorials and came across a doozy in the switch ~ case tutorial. It said a loop function could be added to make the program wait for valid input, but with this code it just finishes the program no matter what the user inputs.

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        int input;
        
        cout<<"1. Play game\n";
        cout<<"2. Load game\n";
        cout<<"3. Multiplayer\n";
        cout<<"4. Exit\n";
        cout<<"Selection: ";
        cin>> input;
        
    
    do {  // start loop
      
        switch ( input ) {  //start switch
        case 1:
             cout<<"Playing game...\n";
             break;
        case 2:
             cout<<"Loading game...\n";
             break;
        case 3:
             cout<<"Loading multiplayer server...\n";
             break;
        case 4:
             cout<<"Thanks for playing.\n";
             break;
        default:
                input = 0;                   // ==, !=, 
               }  // end switch
      
      }  while ( input = 0); //end loop, ==, !=,
        
        cin.get();  // a pause function to stop window from closing fast
        cin.get();
    }
    Is the loop code in the wrong place, or is the default part of the switch ~ case wrong?

    My goal is to become a fairly decent programmer in the months (years, decades!) to come, and I feel pretty dumb not being able to figure this out.

  2. #2
    Registered User
    Join Date
    May 2006
    Posts
    903
    = is different of ==. = means assign. == means check for equality.

  3. #3
    Registered User
    Join Date
    Jan 2007
    Posts
    2
    Excellent! Well, as first impressions always last, I am now the greater of all idiots. What I did to fix the code was move the cin>> input into the loop command:

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        int input;
        
        cout<<"1. Play game\n";
        cout<<"2. Load game\n";
        cout<<"3. Multiplayer\n";
        cout<<"4. Exit\n";
        cout<<"Selection: ";
        
        
    
    do {  // start loop
      
      cin>> input;
        switch ( input ) {  //start switch
        case 1:
             cout<<"Playing game...\n";
             break;
        case 2:
             cout<<"Loading game...\n";
             break;
        case 3:
             cout<<"Loading multiplayer server...\n";
             break;
        case 4:
             cout<<"Thanks for playing.\n";
             break;
        default:
                input = 0;                   // ==, !=, 
               }  // end switch
      
      }  while ( input == 0); //end loop, ==, !=,
        
        cin.get();  // a pause function to stop window from closing fast
        cin.get();
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  2. Problems with switch()
    By duvernais28 in forum C Programming
    Replies: 13
    Last Post: 01-28-2005, 10:42 AM
  3. enumeration with switch case?
    By Shadow12345 in forum C++ Programming
    Replies: 17
    Last Post: 09-26-2002, 04:57 PM
  4. rand()
    By serious in forum C Programming
    Replies: 8
    Last Post: 02-15-2002, 02:07 AM