I am writting a menu, where a user will use a number to choose their option. How do i stop the user from entering a char, or check to make sure that it is an int. I have tried using isdigit and isalpha and noting is working
You can't; the user can enter anything. In your program you need to check what the user has entered, and if it is something invalid, throw it away and seek another input from the user. For instance:
#include <iostream>
int read_int( int lower_bound, int upper_bound )
{
std::cout << "enter an integer in the interval [ "
<< lower_bound << ", " << upper_bound << " ): " ;
int value ;
if( std::cin >> value ) // if the user has entered a number
{
if( value < lower_bound ) std::cerr << "error: the number is too small\n" ;
elseif( value >= upper_bound ) std::cerr << "error: the number is too big\n" ;
elsereturn value ;
}
else
{
std::cerr << "error: not an integer\n" ;
std::cin.clear() ; // clear the error state
std::cin.ignore( 1024, '\n' ) ; // and discard cruft in the input buffer
}
return read_int( lower_bound, upper_bound ) ; // try again
}
#include <iostream>
int read_int( int lower_bound, int upper_bound )
{
std::cout << "enter an integer in the interval [ "
<< lower_bound << ", " << upper_bound << " ): " ;
int value ;
if( std::cin >> value ) // if the user has entered a number
{
if( value < lower_bound ) std::cerr << "error: the number is too small\n" ;
elseif( value >= upper_bound ) std::cerr << "error: the number is too big\n" ;
elsereturn value ;
}
else
{
std::cerr << "error: not an integer\n" ;
std::cin.clear() ; // clear the error state
std::cin.ignore( 1024, '\n' ) ; // and discard cruft in the input buffer
}
return read_int( lower_bound, upper_bound ) ; // try again
}
int main()
{
// display menu of, say, 8 opions 1 .. 8
int choice = read_int( 1, 9 ) ;
std::cout << "you chose " << choice << '\n' ;
}
if( std::cin >> value ) // if the user has entered a number
essentially tests if the stream is in a good state after the attempt to read an integer.
std::cin is put into a failed state - the failbit is set - if the expected data was not successfully read. This would be the case if we tried to read an integer and the user typed in - the input buffer contains - "abc\n".
Once an error state is set, it remains set; and any operation on the stream would fail if it is not in a good state. So we need to put std::cin back into a good state before we do anything else with it. That is what std::cin.clear() does. http://www.cplusplus.com/reference/ios/ios/clear/
After the error state is cleared, we also need to extract and throw away the incorrect characters remaining in the input buffer ("abc" in the previous example). That is what std::cin.ignore( 1024, '\n' ) ; // and discard cruft in the input buffer http://www.cplusplus.com/reference/istream/istream/ignore/
does.
#include<iostream>
usingnamespace std;
int main()
{
char c;
int choice;
cin>>c;
while(1) //It will loop till user enters a no.
{
if(c>=48 && c<=57) // 48 is ascii for no. 0 and 57 is ascii for no. 9
{
choice = c-48;
break; // OK come out of the loop
}
else // if it is not b/w 0 and 9
{
cout<<"Enter a no.";
cin>>c;
continue;
}
} //end of while
cout<<"You entered: "<<choice;
return 0;
} //end of main