Good day. There was a problem with C ++. In short - in one line does not occur reading using the input stream cin. Code below. The place where the error occurs is indicated by a comment. Tried to clear the stream before reading user_num_to_sum with cin.clear () - don't help.

#include <iostream> //cin, cout #include <conio.h> //_getch() #include <vector> //vector using std::cout; using std::cin; using std::vector; int main () { vector<int> numbers; // Π²Π΅ΠΊΡ‚ΠΎΡ€ ΠΈΠ· Ρ†Π΅Π»Ρ‹Ρ… чисСл int num = 0; // число ΠΊΠΎΡ‚ΠΎΡ€ΠΎΠ΅ заносим Π² ΠΊΠΎΠ½Π΅Ρ† Π²Π΅ΠΊΡ‚ΠΎΡ€Π° int user_num_to_sum = 0; // число элСмСнтов Π²Π΅ΠΊΡ‚ΠΎΡ€Π° для суммирования(ΠΏΡ€ΠΈΠΌΠ΅Ρ€: Ссли число 3, Ρ‚ΠΎ суммируСм ΠΏΠ΅Ρ€Π²Ρ‹Π΅ 3 элСмСнта) cout << "Write a few numbers: \n"; while(cin >> num) { numbers.push_back(num);// заносим числа Π² Π²Π΅ΠΊΡ‚ΠΎΡ€ } cout << "How much elements of vector do you want to sum?\n"; cin >> user_num_to_sum; // ERROR: ΠΏΡ€ΠΎΠ³Ρ€Π°ΠΌΠΌΠ° пропускаСт Π΄Π°Π½Π½ΡƒΡŽ строчку ΠΈ выполняСт ΡΠ»Π΅Π΄ΡƒΡŽΡ‰ΡƒΡŽ подставляя Π² user_num_to_sum Π·Π½Π°Ρ‡Π΅Π½ΠΈΠ΅ 0, ΠΊΠΎΡ‚ΠΎΡ€ΠΎΠ΅ Π±Ρ‹Π»ΠΎ Π·Π°Π΄Π°Π½ΠΎ ΠΏΡ€ΠΈ ΠΈΠ½ΠΈΡ†ΠΈΠ°Π»ΠΈΠ·Π°Ρ†ΠΈΠΈ. cout << "The sum of first " << user_num_to_sum << " numbers -> "; //ΠΊΠΎΠ΄ Π΅Ρ‰Π΅ Π½Π΅ Π³ΠΎΡ‚ΠΎΠ² ΠΏΠΎΠ»Π½ΠΎΡΡ‚ΡŒΡŽ, ΠΏΡ€ΠΎΠ³Ρ€Π°ΠΌΠΌΠ° смысла Π½Π΅ ΠΈΠΌΠ΅Π΅Ρ‚ _getch(); return 0; } 

I stop the while loop (cin >> num) by entering the error character '|'. This technique is used in his book Stroustrup. But then for some reason, after stopping the cycle, the reading of the whole number user_num_to_sum does not work.

PS The variant with cin.clear () worked, but only in case of completion of data input into the vector by pressing Ctrl + Z. If you complete it by entering incorrect data (for example, '|' when reading data of type int), then cin.clear () does not work. Question: why?

  • And how do you stop filling the vector? - andy.37 pm
  • PS I cin.clear() helped. - andy.37

2 answers 2

 while(cin >> num) { // Пока ΠΌΡ‹ ΠΌΠΎΠΆΠ΅ΠΌ ΠΏΡ€ΠΎΡ‡ΠΈΡ‚Π°Ρ‚ΡŒ int, Ρ‡ΠΈΡ‚Π°Π΅ΠΌ Π΅Π³ΠΎ ... // ΠΈ Ρ‡Ρ‚ΠΎ-Ρ‚ΠΎ Π΄Π΅Π»Π°Π΅ΠΌ } // Π’Ρ‹ΡˆΠ»ΠΈ ΠΈΠ· Ρ†ΠΈΠΊΠ»Π°, Ρ‚. ΠΊ. int ΠΏΡ€ΠΎΡ‡ΠΈΡ‚Π°Ρ‚ΡŒ нСльзя ... // Π’ΡƒΡ‚ ΠΌΡ‹ Π½ΠΈΡ‡Π΅Π³ΠΎ Π½Π΅ Ρ‡ΠΈΡ‚Π°Π»ΠΈ cin >> user_num_to_sum; // Опа! Π§ΠΈΠ°Ρ‚Π΅ΠΌ Π΅Ρ‰Ρ‘ int 

But the state of the stream has not changed - int as it was impossible to read, and now it is impossible.

If the input of a sequence of numbers ends with some non-numeric input, you should read this something as a string and then read the next number. If the input was completed by pressing ctrl + z, then there seems to be eof in general.

Also, to reset the error marker, you must call cin.clear() before reading.

  • By the way, if the input is completed by pressing ctrl + Z, then cin.clear () perfectly solves the problem (as I did) and the last (after the cycle) number is remarkably read. - andy.37
  • Yes, it really works. Do not think it is stupid, I'm just learning, but why cin.clear () does not clear the last erroneous input for example the symbol '|'? - Taras Sousse
  • @ andy.37, thanks, I will know. In general, I somehow prefer scanf - it has a lot of tricks with the format. For example: stackoverflow.com/a/439205/178988 - Qwertiy ♦
  • 2
    @ TarasSus, please add your last comment to the question itself, or ask a new one, I would love to know the answer myself ) - andy.37
 while(cin >> num) { numbers.push_back(num);// заносим числа Π² Π²Π΅ΠΊΡ‚ΠΎΡ€ } std::string s; cin.clear(); // сбрасываСм ΠΎΡˆΠΈΠ±ΠΊΡƒ Π² cin cin >> s; // ΡƒΠ±ΠΈΡ€Π°Π΅ΠΌ ΠΈΠ· cin нСчисловыС символы cout << "How much elements of vector do you want to sum?\n"; cin >> user_num_to_sum; // ERROR: ΠΏΡ€ΠΎΠ³Ρ€Π°ΠΌΠΌΠ° пропускаСт Π΄Π°Π½Π½ΡƒΡŽ строчку ΠΈ выполняСт ΡΠ»Π΅Π΄ΡƒΡŽΡ‰ΡƒΡŽ подставляя Π² user_num_to_sum Π·Π½Π°Ρ‡Π΅Π½ΠΈΠ΅ 0, ΠΊΠΎΡ‚ΠΎΡ€ΠΎΠ΅ Π±Ρ‹Π»ΠΎ Π·Π°Π΄Π°Π½ΠΎ ΠΏΡ€ΠΈ ΠΈΠ½ΠΈΡ†ΠΈΠ°Π»ΠΈΠ·Π°Ρ†ΠΈΠΈ. 

This is how your code will work correctly when filling in a vector with any non-numeric character.

Actually, the answer to the last question: cin.clear () clears the flags (state) of the error in the stream, but the last character entered (group of characters) remains in it, so they need to be removed from there, for example, into some unnecessary string.

P.S. The question turned out unexpectedly interesting and deserves encouragement in the form of a plus.

P.P.S. Found a variant without an extra variable (checked, you need to #include <limits> )

 cout << "Write a few numbers (end with any non-number input): \n"; while(cin >> num) { numbers.push_back(num);// заносим числа Π² Π²Π΅ΠΊΡ‚ΠΎΡ€ } cin.clear(); // сбрасываСм Ρ„Π»Π°Π³ ошибки cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // пропускаСм всС Π΄ΠΎ символа Π½ΠΎΠ²ΠΎΠΉ строки cout << "How much elements of vector do you want to sum?\n"; cin >> user_num_to_sum;