C ++ newbie is trying to create a simple text game on a 2D array.
When I use the switch as shown. It will always print the default value, no matter what else happens.
From other threads and forums, I found that it probably has something to getch() with getch() and that it returns char , as well as \n .
I tried for an hour, but I canβt solve it. The reason I use getch() is this: C ++ changing the canonical mode in windows (for reference).
Part of my code:
//Set up game field generateField(); setPlayerStart(); //printGameInfo(); TO BE MADE //Start game while loop int userInput; do{ //system("cls"); DISABLED FOR TESTING PURPOSES printField(); userInput = getch(); switch(userInput){ case 72:{ //ARROW UP cout << "1What" << endl; //ALSO FOR TESTING PURPOSES break; } case 80:{ //ARROW DOWN cout << "2What" << endl; break; } case 75:{ //ARROW LEFT cout << "3What" << endl; break; } case 77:{ //ARROW RIGHT cout << "4What" << endl; break; } case 113:{ //"q" return false; //Quit game } default:{ cout << "Default..." << endl; } } } while(userInput != 5);
c ++ windows switch-statement
LuukV
source share