C++ how to check for variable type?

In summary, to check the type of user input using cin >>, you can use the returned boolean value in an if-statement or loop-statement. To prevent the input stream from getting stuck in a failed state, you can use cin.clear() and cin.ignore() functions to clear the stream and skip over any invalid characters.
  • #1
zeion
466
1
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.
 
Technology news on Phys.org
  • #2
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.

Code:
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.
 
  • #3
I understand all that except for the cin.ignore(1000,'\n'); What is that for?
It seems to loop forever without the ignore.
 
  • #4
Have you read comment in the code?
 
  • #5
Yeah but I don't understand why it needs to do that.
 
  • #6
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.
 

1. How do I check the type of a variable in C++?

To check the type of a variable in C++, you can use the typeid operator. This operator takes in a variable as an argument and returns a type_info object that contains information about the type of the variable.

2. Can I use the typeid operator on any variable type?

Yes, the typeid operator can be used on any variable type in C++. It will return a type_info object that contains information about the type of the variable.

3. How do I compare the type of a variable in C++?

To compare the type of a variable in C++, you can use the typeid operator and the == comparison operator. This will compare the types of two variables and return a boolean value indicating whether they are the same type or not.

4. Is there a way to check for a specific type of variable in C++?

Yes, you can use the typeid operator to check for a specific type of variable in C++. You can compare the type_info object returned by typeid to the type_info object of the type you are looking for using the == comparison operator.

5. Can I use typeid to check for user-defined types?

Yes, you can use typeid to check for user-defined types in C++. However, the type must be defined with the class or struct keywords for it to work properly.

Similar threads

  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
5
Views
4K
  • Programming and Computer Science
4
Replies
118
Views
6K
  • Programming and Computer Science
2
Replies
39
Views
3K
  • Programming and Computer Science
Replies
8
Views
2K
  • Programming and Computer Science
Replies
10
Views
1K
  • Programming and Computer Science
2
Replies
47
Views
3K
  • Programming and Computer Science
Replies
14
Views
31K
  • Programming and Computer Science
Replies
13
Views
1K
Back
Top