The cin.clear()
clears the error flag on cin
(so that future I/O operations will work correctly), and then cin.ignore(10000, '\n')
skips to the next newline (to ignore anything else on the same line as the non-number so that it does not cause another parse failure). It will only skip up to 10000 characters, so the code is assuming the user will not put in a very long, invalid line.
Related Posts:
- Using cin to input a single letter into a char
- Multiple inputs on one line
- outputting ascii table in C++
- Why use conio.h?
- How to read a file line by line or a whole text file at once?
- What are the rules of the std::cin object in C++?
- std::cin.getline( ) vs. std::cin
- ‘cout’ was not declared in this scope
- C++ round a double up to 2 decimal places
- When and why do I need to use cin.ignore() in C++?
- streambuf::xsgetn and state flags
- ‘cout’ was not declared in this scope
- cin >> “no operator matches these operands”
- Press Enter to Continue
- Reading from text file until EOF repeats last line
- cin >> “no operator matches these operands”
- Correct way to use cin.fail()
- Checking cin input stream produces an integer
- Reading getline from cin into a stringstream (C++)
- Reading from .txt file into two dimensional array in c++
- std::cin input with spaces?
- C++ – pointer being freed was not allocated error
- How do I flush the cin buffer?
- cin.eof() functionality
- cin.ignore(numeric_limits
::max(), ‘\n’) - The difference between cin.ignore and cin.sync
- How to print (using cout) a number in binary form?
- Xcode 11.1: iostream’ file not found
- What’s the difference between while(cin) and while(cin >> num)
- operator << must take exactly one argument
- Using cin for char array
- istream and ostream problem – C++
- How to solve “Unresolved inclusion:
” in a C++ file in Eclipse CDT? - How many spaces for tab character(\t)?
- How do I build a graphical user interface in C++? [closed]
- What are the differences between a pointer variable and a reference variable in C++?
- What is the effect of extern “C” in C++?
- finding dll for “The specified module could not be found”
- no matching function for call to ‘ ‘
- Read file line by line using ifstream in C++
- Convert an int to ASCII character
- When should I write the keyword ‘inline’ for a function/method?
- When does a process get SIGABRT (signal 6)?
- Logical XOR operator in C++?
- C++ – Decimal to binary converting
- Alternative to itoa() for converting integer to string C++?
- 1e-9 or -1e9, which one is correct?
- Returning an empty string : efficient way in c++
- “&” meaning after variable type
- How can I create objects while adding them into a vector?
- What is the printf format specifier for bool?
- libpng warning: iCCP: known incorrect sRGB profile
- In CLion, header only library: file “does not belong to any project target, code insight features might not work properly”
- Is a function definition not allowed here before a ‘{‘ token?
- terminate called after throwing an instance of ‘std::out_of_range’
- What does “missing template argument” mean?
- Is there ‘byte’ data type in C++?
- How to return a string from a C++ function?
- C++, random number w/ range of 1-6
- What does “missing template argument” mean?
- How do conversion operators work in C++?
- What is the difference between isdigit() and isnumber()?
- Convert char* to string C++
- C++ delete vector, objects, free memory
- Copy a file in a sane, safe and efficient way
- c++ convert string to hex
- LINK : fatal error LNK1561: entry point must be defined ERROR IN VC++
- Multiple OR or AND conditions in IF statement
- What does it mean double free detected in tcache 2 while using mpz?
- Generate SHA256 in c++
- A good and basic implementation of BigInt class in C++
- What is the C equivalent to the C++ cin statement?
- c++ Read from .csv file
- Check if C++ Array is Null
- Random not declared in scope
- What does the “Expected ‘(‘ for function-style cast or type construction” error mean?
- LINK : fatal error LNK1561: entry point must be defined ERROR IN VC++
- Comparing the values of char arrays in C++
- Creating an instance of class
- Destructor for a doubly-linked list that points to its value
- LLVM Compiler 2.0: Warning with “using namespace std;”
- C++ operator+ and operator+= overloading
- In c++ what does a tilde “~” before a function name signify?
- ERROR: Control may reach end of non-void function /
- Function stoi not declared
- Too many arguments to function
- not declared in this scope’ when using strlen()
- Converting a Cubemap into Equirectangular Panorama
- Why is this vector iterator not incrementable?
- glibc detected : double free or corruption
- What is the best way to develop a C++ web application?
- How to install Visual Studio Build Tools 2010 on Visual Studio 2015 Community?
- Throwing out of range exception in C++
- Converting string to unsigned int returns the wrong result
- Visual c++ can’t open include file ‘iostream’
- c++ vector bubble sort
- compare and swap vs test and set
- Weighted random numbers
- compare and swap vs test and set
- Link error “undefined reference to `__gxx_personality_v0′” and g++ [duplicate]