Thread: how to determine if user input is an int

  1. #1
    Registered User
    Join Date
    Sep 2001

    Question how to determine if user input is an int

    This is not part of my assignment, but I would like to figure out how to test the user input to be sure it is an integer. Is there a native function that I can use? If nit can some one point me in the right dirrection because I am drawing a blank. At first I thought I would do this by comparing the ascii values but I did not figure out how to implement it. Here is the assignment that is actually finished and working. It compares two arrays to figure out the taxrate of an income. Would like to test the variable aswer.

    #include <iostream.h>
    #include <stdlib.h>

    int main()

    const int max = 5;
    int answer = 0, counta = 0, countb = 0, index = 0;
    int income[max] = {0,10000,20000,50000,100000};
    int tax[max] = {0,5,10,15,20};

    cout<<"What is your income? ";
    cin>> answer;

    for (counta = 0; counta < max; counta++) {
    for (countb = 1; countb < (max + 1); countb++) {
    if ((answer >= income[counta]) & (answer <= income[countb])) {
    index = counta; }


    cout<<"Your taxrate is "<< tax[index] << "%" << endl;
    cout<<"Your taxes are $"<< (tax[index]*.01)*answer <<
    endl << endl;

    return 0;


  2. #2
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    This question should be on the C++ board.

    You can either get the input as a character and then covert to an int, or test for which will set the fail state if a non-numeric character is passed when an int is expected.

  3. #3
    Registered User mlupo's Avatar
    Join Date
    Oct 2001
    you can use the isdigit() function to test whether or not a character in a character string is a digit.
    if it is a digit, you an use atoi() to convert a character that represents a digit to an integer.

    for example:

    char somestring[80];

    strcpy(somestring, "123");
    strpy just copies "123" into the array.

    then if you did
    isdigit(somestring[0]; where 0 is the first position in the character array.

    isdigit will return a non-zero integer (1-9) if it finds that the character you input is a digit. It returns 0 if it isn't. Now you can use that to make decisions with. bearing in mind that something that evaluates to 0 is the same as being "false" any non-zero would be equivilent to "true"

    if( isdigit(somestring[0]) )
    do something
    do something else.

    I hope this helps

  4. #4
    All console input is text, therefore, you know that they haven't input an integer. See how easy that was?


  5. #5
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001

Popular pages Recent additions subscribe to a feed

Similar Threads

    By geoffr0 in forum C Programming
    Replies: 4
    Last Post: 04-16-2009, 05:26 PM
  2. memory leak
    By aruna1 in forum C++ Programming
    Replies: 3
    Last Post: 08-17-2008, 10:28 PM
  3. Replies: 8
    Last Post: 03-10-2008, 11:57 AM
  4. Converted from Dev-C++ 4 to Dev-C++ 5
    By Wraithan in forum C++ Programming
    Replies: 8
    Last Post: 12-03-2005, 07:45 AM
  5. Switch/case Problems (long code in post)
    By Wraithan in forum C++ Programming
    Replies: 2
    Last Post: 12-01-2005, 06:40 PM