This question already has answers here:
Infinite loop with cin when typing string while a number is expected
(4 answers)
Closed 6 years ago.
Please read the following code:
#include <iostream>
#include <cstdlib>
int main() {
std::cout << "Please input one integer." << std::endl;
int i;
while (true) {
std::cin >> i;
if (std::cin) {
std::cout << "i = " << i << std::endl;
break;
} else {
std::cout << "Error. Please try again."<< std::endl;
std::cin.ignore();
std::cin.clear();
}
}
std::cout << "Thank you very much." << std::endl;
std::system("pause");
return 0;
}
When I give std::cin an invalid input, such as w, then Error. Please try again. is outputed infinitely.
I thought std::cin.ignore would blank the input stream, and std::cin.clear would resume it to normal state. So why does the infinite loop happen?
By default, std::basic_istream::ignore() ignores only 1 character:
basic_istream& ignore( std::streamsize count = 1, int_type delim = Traits::eof() );
(From http://en.cppreference.com/w/cpp/io/basic_istream/ignore)
More idiomatic use for your case seems to be the one from the example there:
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
Also, note that you want to call clear() before the ignore() call, so the input extraction of ignore() will succeed instead of returning immediately.
Related
I am having trouble with the following code. It is meant to keep asking for a valid input until an integer or double is inputted. It works as intended for characters, however when I input a string of length more than 1, it will run the loop multiple times. For example, the input "hello" with cause "Please enter a valid number" to be printed 5 times. Interestingly "h llo" will only print the sentence 4 times.
int gamenumber;
while(true)
{
cin >> gamenumber;
if(cin.fail())
{
cout << "Please enter a valid number" << endl;
cin.clear();
cin.ignore();
} else
break;
I did manage to fix this issue by replacing "cin.ignore()" with "cin.ignore(1000, '\n')".
But regardless, it's bugging me that I don't understand why "cin.ignore()" alone doesn't fix this? Is there a way to fix the above code without using "cin.ignore(1000, '\n')"? (This is part of a homework assignment, and we may not be allowed to use "cin.ignore(1000, '\n')")
Thank you!
You need use ignore with the overloaded one, see this anser here.
Or you can just need to run getline to drain the contents, but this way is slower and unnecessary.
#include <iostream>
#include <string>
int main()
{
double n;
while( std::cout << "Please, enter a number\n"
&& ! (std::cin >> n) )
{
std::cin.clear();
std::string line;
std::getline(std::cin, line);
std::cout << "I am sorry, but '" << line << "' is not a number\n";
}
std::cout << "Thank you for entering the number " << n << '\n';
}
So I figure I'll put this here since I had to traverse a lot of docs and forums to find the definitive answer. I was trying to get input from the user and check if the input was an integer using isdigit() in an if statement. If the if statement failed the program would output an error message. Although, when a nondigit character was entered the program would loop through the error message endlessly. Here's that code:
int guess = -1;
while (game.getCurQuestion() <= 4) {
std::cout << "Guess: " << game.getCurQuestion() + 1 << std::endl;
std::cin >> guess;
if(isdigit(guess))
{
game.guess(guess);
else
{
std::cout << "Error\n"; //this would be looped endlessly
}
}
std::cout << "You got " << game.getCorrect() << " correct" << std::endl;
return 0;
}
NOTE: Solved, only posted to include my solution. Feel free to correct if I stated anything incorrectly.
The posted way will fail sometimes and will cast the doubles to integers if any doubles are input.
Use something like the following
int getIntInput() {
try {
std::string input;
std::cout << "\nPlease Enter a valid Integer:\t";
std::cin >> input;
size_t takenChars;
int num = std::stoi(input, &takenChars);
if (takenChars == input.size()) return num;
} catch (...) {}
return getIntInput();
}
Problem: The program kept hold of the non-integer value stored in the cin buffer. This leads to the program never leaving the error message.
Solution:
Use std::cin.fail() to check if the input matches the variable data type. I.E. int was the expected input but the user entered a char. In this case std::cin.fail() would be true.
In the case of std::cin.fail(), use std::cin.clear() and std::cin.ignore(std::numeric_limits<int>::max(), 'n') std::cin.clear() will clear the error flag. The std::cin.ignore(std::numeric_limits<int>::max(), 'n') will ignore any other input that is not an integer and will skip to the new line. Effectively progressing the program.
The solution implemented in my code looks like this:
int guess = -1;
while (game.getCurQuestion() <= 4) {
std::cout << "Guess: " << game.getCurQuestion() + 1 << std::endl;
std::cin >> guess;
if (std::cin.fail())
{
std::cout << "Please enter a valid number\n";
std::cin.clear();
std::cin.ignore(std::numeric_limits<int>::max(), '\n');
}
game.guess(guess);
}
Hope this helps and that it saves some people the tedious research because of never learning std::cin error handling! Note: I'm aware my implementation skips the current move, call it punishment ;)
I am reading the book 'Accelerated C++', and I am unable to reproduce the results for their read homework problem even after exactly copying their code on my machine. The basic problem is about using cin.clear() to change the failure state of the inpute stream after the use of EOF to indicate that all the grades have been entered. Authors suggest Ctrl+D on linux systems for EOF. I already saw this and this but they couldn't solve my problem.
Here is my minimal working example:
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main()
{
string name, city;
vector<double> homework;
cout << "Enter your name : " ;
cin >> name;
cout << "Hello " + name + "!" << endl;
cout << endl;
cout << "Enter your grades" << endl;
if (cin)
{
homework.clear();
double x;
while (cin >> x)
{
homework.push_back(x);
}
cin.clear();
}
cout << endl;
cout << "Enter your city : " ;
cin >> city ;
cout << "You live in " + city << endl;;
return 0;
}
After entering all the homework, I hit Ctrl+D and then I expect that I would now be given chance to enter city name. But the program just ends after printing the two strings at the end of my code. What is wrong with my understanding of cin.clear()? I would also like to point out that using cin.ignore() after cin.clear() doesn't help either.
On fail you need to clear the flags and ignore all the bad input.
Include #include <limits> for std::numeric_limits.
if ( std::cin.fail( ) )
{
std::cin.clear( );
std::cin.ignore( std::numeric_limits<std::streamsize>::max( ), '\n');
}
If you want a way to exit the loop use a value that you can test for (like -1). When you receive that value, exit the loop.
double value{ 0 };
while( std::cin >> value && value != -1 )
homework.push_back( value );
if ( std::cin.fail( ) )
{
std::cin.clear( );
std::cin.ignore( std::numeric_limits<std::streamsize>::max( ), '\n');
}
Edit:
I didn't see that you were trying to end the stream with EOF.
Read this answer to figure out how to accomplish this.
How to resume input stream after stopped by EOF in C++?
I read the example code from https://en.cppreference.com/w/cpp/io/basic_ios/clear and tried this:
command line input is: w
#include <iostream>
#include <string>
int main()
{
double n = 5.0;
std::cin >> n;
std::cout << std::cin.bad() << "|" << std::cin.fail() << "\n";
std::cout << "hello" << "\n";
std::cin.clear();
std::cout << std::cin.bad() << "|" << std::cin.fail() << "\n";
std::cin >> n; //Why can't I input again here?
std::cout << "world" << "\n";
std::cout << n << "\n";
}
I don't understand why the program finishes without allowing me to input again, I think I've use std::cin.clear() to reset all the "fail" states.
clear just resets the error-flags, but it leaves the previous input, which had led to the failure, in the buffer. Hence, the second cin >> n will again read the same input and will again fail. So you will not get the chance to enter new input.
You need to take errorneous characters from the buffer (in addition to calling cin.clear()); Use, for example, cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'), which ignores every character until the first occurence of a \n. You could also use fgets, but - in contrast to ignore - fgets requires a buffer to store characters in which you are actually not interested.
Say I have a program that takes in integers. How do I stop the program from falling apart if the user enters an out of range number, or a letter or something?
The cin's base class is std::basic_istream. The input stream indicates a recoverable error in case it cannot extract the requested data from the stream. In order to check for that error bit, std::basic_istream::fail() method must be used — it returns true if there was a failure or false if everything is alright. It is important to remember that if there is an error, the data is left in the stream and, of course, the error bit(s) must also be cleared using std::basic_istream::clear(). Also, a programmer must ignore incorrect data, or otherwise an attempt to read something else will fail again. For that purpose, std::basic_istream::ignore() method can be used. As for the valid range of values, it must be checked manually. Okay, enough theory, here is a simple example:
#include <limits>
#include <iostream>
int main()
{
int n = 0;
for (;;) {
std::cout << "Please enter a number from 1 to 10: " << std::flush;
std::cin >> n;
if (std::cin.fail()) {
std::cerr << "Sorry, I cannot read that. Please try again." << std::endl;
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
continue;
}
if (n < 1 || n > 10) {
std::cerr << "Sorry, the number is out of range." << std::endl;
continue;
}
std::cout << "You have entered " << n << ". Thank you!" << std::endl;
break;
}
}
Hope it helps. Good Luck!
I prefer reading the input as strings, and then sanitizing them with boost::lexical_cast<>:
#include <boost/lexical_cast.hpp>
#include <iostream>
#include <string>
int main () {
std::string s;
while( std::cin >> s) {
try {
int i = boost::lexical_cast<int>(s);
std::cout << "You entered: " << i << "\n";
} catch(const std::bad_cast&) {
std::cout << "Ignoring non-number: " << s << "\n";
}
}
}
Postscript: If you are allergic to Boost, you can use this implementation of lexical_cast:
template <class T, class U>
T lexical_cast(const U& u) {
T t;
std::stringstream s;
s << u;
s >> t;
if( !s )
throw std::bad_cast();
if( s.get() != std::stringstream::traits_type::eof() )
throw std::bad_cast();
return t;
}
something like this should do you need to clear the buffer after checking aswell if i remember right
if (cin.fail())
{
cout<<"need to put a number"<<endl;
cin.clear();
cin.ignore();
}
If You dont want to add libraries to your code you could also use do..while() statements.
in your do while you will ask for user input and then receive it to your variable then in the while part you will be able to check that this is the data you are expecting if not continue to ask for the data.
just another option ....even though the answer already mentioned should work more than adequately
You can use the following code for simplest and fast checking of valid input in int :
#include "stdafx.h"
#include <iostream>
using namespace std;
int main()
{
int intb;
while( !( cin>>intb ) ){
cin.clear ();
cin.ignore (1000, '\n');
cout<<"Invalid input enter again: "<<endl;
}
cout<<"The value of integer entered is "<<b<<endl;
return 0;
}
The while loop keeps on iterating until it gets the right input.
cin.clear() changes the error control state.
cin.ignore() removes clear the input stream so that new input can be taken again. If not done thw while loop will be in infinite state.