how to determine if user input is an int

This is a discussion on how to determine if user input is an int within the C Programming forums, part of the General Programming Boards category; This is not part of my assignment, but I would like to figure out how to test the user input ...

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    6

    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;

    system("PAUSE");
    return 0;
    }

    Thanx

  2. #2
    zen
    zen is offline
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    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 cin.fail() which will set the fail state if a non-numeric character is passed when an int is expected.
    zen

  3. #3
    Registered User mlupo's Avatar
    Join Date
    Oct 2001
    Posts
    72
    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
    else
    do something else.


    I hope this helps

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

    Quzah.

  5. #5
    zen
    zen is offline
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    that
    zen

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. NEED HELP READING FILE and PRINTING
    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, 06:45 AM
  5. Switch/case Problems (long code in post)
    By Wraithan in forum C++ Programming
    Replies: 2
    Last Post: 12-01-2005, 05:40 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21