hi guys I am trying to figure out a way to check for the correct input for example if I type steve in for id it will not work as id is an int,
I thought my code would work but it clearly doesn't
what I thought was happening while input equals false we get trapped in a while loop so we enter steve in so i expect cin.bad() to be set to true so the first if statement shouldn't run yet it seems to for some reason
and I expected the second if statement to run I used cin.clear to clear the state of the stream then ignore to ignore the input
isn't reading a string to an int an unrecoverable error?
Not really. It is possible to recover from this error by clearing the stream error flags and completely cleaning out the input buffer.
Your code is only removing one character from the input buffer, you need to tell ignore() how many characters to remove(usually using numeric limits max) as the numeric value and the end of line character as the optional second parameter.
By the way you can check the status of all the flags by just using the stream name if(std::cin).
It is canonical to check for the success or failure of attempted input by using the returned (reference to the) stream as the condition in if/loop statements.
This operator makes it possible to use streams and functions that return references to streams as loop conditions, resulting in the idiomatic C++ input loops such as while(stream >> value) {...} or while(getline(stream, string)){...}.
Such loops execute the loop's body only if the input operation succeeded.
#include <iostream>
int get_int()
{
int value ;
if( std::cin >> value )return value ; // success: an int was read
// badly formed input: failed to read an int
std::cout << "please enter en integer: " ; // inform the user
std::cin.clear() ; // clear the failed state of the stream
std::cin.ignore( 1000000, '\n' ) ; // extract and discard the bad input
return get_int() ; // try again
}
int get_int( int minv, int maxv )
{
int value = get_int() ;
if( value >= minv && value <= maxv ) return value ;
// out of range
std::cout << "please enter a value in [" << minv << ", " << maxv << "]: " ;
return get_int(minv,maxv) ;
}
int main()
{
std::cout << "id [0,999]?: " ;
constint id = get_int(0,999) ;
std::cout << "id is " << id << '\n' ;
}
I tried ignoring a large number followed by a newline as the params to ignore but it doesn't seem to work the way I expected it pretty much just goes to a new line and waits for input
also another snippet of code to explain my question below
another question relating to this is what would cause a stream to go into an unrecoverable error or the cin.bad() to be true?
and for some reason when I print out the failbit in both cases it prints 4,why 4? and I thought that it would be 0 if the read is successful yet it still prints 4