Thread: how to test if user input is an int

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

    how to test 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 not 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
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    See here
    zen

  3. #3
    Registered User
    Join Date
    Aug 2001
    Posts
    47
    Code:
    float myInput;
    cin >> myInput;
    if(myInput == (int)myInput)
            cout << "You entered the integer " << myInput;

  4. #4
    Registered User dirkduck's Avatar
    Join Date
    Aug 2001
    Posts
    428
    typecasting...read up

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. memory leak
    By aruna1 in forum C++ Programming
    Replies: 3
    Last Post: 08-17-2008, 10:28 PM
  2. can some one please tell me the cause of the error ?
    By broli86 in forum C Programming
    Replies: 8
    Last Post: 06-26-2008, 08:36 PM
  3. Replies: 3
    Last Post: 05-13-2007, 08:55 AM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM