checking an int variable

Hi All

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

Thanks
Michael
use the ascii table
> How do i stop the user from entering a char

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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#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" ;
        else if( value >= upper_bound ) std::cerr << "error: the number is too big\n" ;
        else return 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
}
Thank you for your reply. I am trying to run this code in my program and on its own, i get a fatal error message, due to unresolved externals.

Thanks
Michael
> I am trying to run this code in my program and on its own,
> i get a fatal error message, due to unresolved externals.

At the very least, you need a main()
Try this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#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" ;
        else if( value >= upper_bound ) std::cerr << "error: the number is too big\n" ;
        else return 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' ;
}
This is fantastic so much, as i am a newbie at C++ could you explain your code, so i can understand it better
Do you know what a function is?
int read_int( int lower_bound, int upper_bound ) is a function.
i know what a function is, but i wasnt sure of the std:cerr, std::cin.clear, i have never come across these before
> i wasnt sure of the std:cerr

http://en.wikipedia.org/wiki/Standard_streams#Standard_error_.28stderr.29


> std::cin.clear

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.
You can just input a char and check if it is a digit or not.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include<iostream>
using namespace 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 
Topic archived. No new replies allowed.