Friday, 23 August 2013

cin for an int inputing a char causes Loop that is supposed to check input to go wild

cin for an int inputing a char causes Loop that is supposed to check input
to go wild

This is a function of my game it will ask for input and cin into
"iAuswahl"! Then the while loop is checks if it is one of the Values i
want 1-9 if not it activates and is supposed to ask for new input. Witch
it does for int. But if i input a char like r it will go crazy and just
keep giving me back my cout and skip the cin! My questions are why does it
do it and how do i stop it?
void zug(string sSpieler, int iDran){
int iAuswahl;
char cXO = 'O';
if (iDran == 1)
{
cXO = 'X';
}
cout << sSpieler << ", Sie sind am Zug. Bitte waehlen sie eins der
Felder.\n" << endl;
grafik();
cout << "Sie sind >> " << cXO << " <<." << endl;
cin >> iAuswahl;
cout << endl;
while (iAuswahl != 1 && iAuswahl != 2 && iAuswahl != 3 && iAuswahl != 4 &&
iAuswahl != 5 && iAuswahl != 6 && iAuswahl != 7 && iAuswahl != 8 &&
iAuswahl != 9)
{
cout << "Kein gültiges Feld bitte wählen sie noch einmal!\n" << endl;
cin >> iAuswahl;
}
feldfuellen(iAuswahl, cXO);
}

No comments:

Post a Comment