c++ - What type of loop would I wrap around this statement? -
for reason i'm getting extremely confused type of loop should wrapping around switch statement. if have switch statement below , want user able keep inputting keystroke until hits 1 of cases type of loop best use this?
thanks,
int input; cin >> input; switch( input ) { case 1: playgame(); break; case 2: loadgame(); break; case 3: multiplayer(); break; case 4: cout << "thanks, exitting \n"; break; default: cout << "error \n"; cin >> input; break; }
there missing stop condition.
int input = 0; // make habit of initialising variables bool again = true; while(again) { // use input != 4 (stop value) cin >> input; ... case 4: // magic numbers bad, maybe use enum cases. cout << "thanks, exiting \n"; again = false; // <----- else wont out. break; ... }
Comments
Post a Comment