Register to reply

C++ how to check for variable type?

by zeion
Tags: check, type, variable
Share this thread:
zeion
#1
Mar5-12, 05:06 PM
P: 467
So, I want to use cin >> to get a user input that must only be int.
How can I check the type of the input?

Thanks.
Phys.Org News Partner Science news on Phys.org
Experts defend operational earthquake forecasting, counter critiques
EU urged to convert TV frequencies to mobile broadband
Sierra Nevada freshwater runoff could drop 26 percent by 2100
jtbell
#2
Mar5-12, 05:48 PM
Mentor
jtbell's Avatar
P: 11,786
The expression 'cin >> n' not only performs the indicated input (or tries to), it also returns a boolean value indicating whether the input was successful or not, which you can test by putting the input expression in an if-statement or loop-statement.

int n;
cout << "Give me an integer: ";
while (!(cin >> n))
{
    cout << "Hey dummy, I said give me an integer! Try again: ";
    // clears the input stream's status flag
    cin.clear();
    // skip past the next newline, or 1000 chars, 
    // whichever comes first
    cin.ignore(1000,'\n');  
}
cout << "You entered " << n << "." << endl;
This isn't a complete solution because if you enter something like 2.54, it stops reading at the period and puts 2 into n, and does not signal an error. To catch that, you'll have to read the input as a string, check whether it's all digits, and then convert it to an int, maybe using the atoi() function.
zeion
#3
Mar5-12, 07:19 PM
P: 467
I understand all that except for the cin.ignore(1000,'\n'); What is that for?
It seems to loop forever without the ignore.

Borek
#4
Mar6-12, 02:07 AM
Admin
Borek's Avatar
P: 23,599
C++ how to check for variable type?

Have you read comment in the code?
zeion
#5
Mar6-12, 08:39 AM
P: 467
Yeah but I don't understand why it needs to do that.
jtbell
#6
Mar6-12, 10:50 AM
Mentor
jtbell's Avatar
P: 11,786
The >> operator (stream input operator) starts from its current position in the input stream, skips over whitespace (blanks, tabs, newlines), then reads characters until it finds one that cannot be part of the desired input data type. It stops reading at that point, and leaves that "invalid" character in the input stream. If it hasn't found any characters that can be part of the desired input data type, it returns 'false' in the loop test in my example.

So if >> is trying to read an integer, but you enter a word instead, the input fails and the input stream is left positioned at the beginning of the word. In order to read the number that you hopefully will enter correctly in response to the error message, you have to tell the stream to skip past that word. We normally use a newline ("enter" key) to terminate input, so the reasonable thing to do is to skip as far as the newline. That's what cin.ignore() does. You also have to give it a maximum number of characters to skip, which is what the 1000 is for. It can actually be any number that's big enough to ensure that you actually reach the newline while skipping.


Register to reply

Related Discussions
How can I override my instance variable of array type Programming & Computer Science 7
Homework check (too easy) with on/off design analysis for variable area turbojet Aerospace Engineering 0
How to check if my lapack,cbas lib support long double type? Computers 2
Check work on 2 variable function. Calculus & Beyond Homework 3
Simple 2 variable integral just want to check my answer Calculus & Beyond Homework 2