-
cin.get() being ignored
Hi
Im using a switch case for menu options.In case 1 im just printing the values of a couple varibles.I put in the getline() but it seems to be by passed all together.
the varible flashs up on the screen as it should with out the getline().
if i use system("pause") within the switch case it stops the screen so i can make sure the vars are returning the correct values.Also if i put the getline() outside the switch case...
..i know the problem aint exactly the end of the world just curious whats not adding up,
regards C
Code:
switch(x){
case 1:
playerStats( playerName, gold, playerTea ,playerFood,playerSpices, playerLoc);//just cout's stats
cin.get();
break;
case 2:
//sell function
break;
case 3:
//change location
break;
case 4:
return 0;
break;
default:
cout<<"YoU bRoKe ThE CoMpUtEr!!!"<<endl;
cin.get();
return 0;
break;
}
-
If getline doesn't wait for you to type something that means it's consuming stray input.
If you handle your input like this
std::cin >> foo;
std::cin.ignore();
it should fix the immediate problem. It's a better idea not to mix the getline function with stream extraction though, since they behave differently (getline reads anything, extraction skips over white space by default). If you can avoid that, do so.
-
thanks,I get you,its working now