Cin for int injecting char calls a loop that should check input,

This is a feature of my game, it will ask for input and entry in "iAuswahl"! Then the while loop checks if this is one of the values, I want 1-9 if it is not activated and needs to request a new input. Witch is for int. But if I enter a char, for example r, it will go crazy and just continue to give me back my cout and skip cin! My questions are, why is he doing this and how can I stop him?

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); } 
+5
source share
3 answers

If an error occurs while reading from the stream, the error flag is set, and until you clear the error flags, there will be no more reading. That is why you get an infinite loop.

 cin.clear(); // clears the error flags // this line discards all the input waiting in the stream cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); 

In addition, it is incorrect to use the results of an input operation if you do not know if the reading was the first. You cannot make any assumptions about the meaning of iAuswahl . This is one of the most common mistakes made by newcomers using threads. Always check if the input operation was ok. This is easiest to do using operator>> in a boolean context:

 if (cin >> some_obj) { // evaluates to true if it succeeded } else { // something went wrong } 

And my mine, this line

 while (iAuswahl != 1 && iAuswahl != 2 && iAuswahl != 3 && iAuswahl != 4 && iAuswahl != 5 && iAuswahl != 6 && iAuswahl != 7 && iAuswahl != 8 && iAuswahl != 9) 

may be as follows:

 while (iAuswahl < 1 || iAuswahl > 9) 

The correct loop might look something like this:

 while (true) { if ((cin >> iAuswahl) && (iAuswahl >= 1) && (iAuswahl <= 9)) break; std::cout << "error, try again\n"; cin.clear(); cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); } 
+5
source

You need to clear the error flag after reading the wrong type, otherwise cin will refuse to read anything, since it will be in an invalid state. In addition, you need to ignore the character that was not read by cin , because it would be stuck in an infinite loop, because it would always try to read from that character.

 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.clear(); // #include <limits> cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); cin >> iAuswahl; } 

Besides,

 while (iAuswahl != 1 && iAuswahl != 2 && iAuswahl != 3 && iAuswahl != 4 && iAuswahl != 5 && iAuswahl != 6 && iAuswahl != 7 && iAuswahl != 8 && iAuswahl != 9) 

can be written as

 if(iAuswahl < 1 || iAushwahl > 9) 

Do not forget to initialize iAushwahl to 0 or some other value, because if your cin >> iAushwahl does not work, you will read the uninitialized variable.

+3
source

If you do not use your iAuswahl variable to perform any mathematical operations that are not in this function, just make a variable of type char, it makes no sense to make an int variable if it does not do anything that might require it to be int.

0
source

Source: https://habr.com/ru/post/1498570/


All Articles