C++ how to check for variable type?

  • Context: C/C++ 
  • Thread starter Thread starter zeion
  • Start date Start date
  • Tags Tags
    C++ Type Variable
Click For Summary

Discussion Overview

The discussion revolves around how to ensure that user input in C++ is of the integer type when using the 'cin' input stream. Participants explore methods for validating input and handling errors when the input does not meet the expected type.

Discussion Character

  • Technical explanation
  • Conceptual clarification
  • Debate/contested

Main Points Raised

  • One participant seeks a method to check if user input is an integer when using 'cin >>'.
  • Another participant explains that 'cin >> n' returns a boolean indicating the success of the input operation and suggests using a loop to repeatedly prompt for input until a valid integer is entered.
  • It is noted that entering a decimal number like 2.54 will not trigger an error, as 'cin' will only read the integer part and leave the invalid character in the input stream.
  • To handle invalid input more robustly, a suggestion is made to read input as a string, validate it, and then convert it to an integer using the 'atoi()' function.
  • One participant expresses confusion about the purpose of 'cin.ignore(1000, '\n');' in the error handling loop.
  • Another participant explains that 'cin.ignore()' is necessary to clear the invalid input from the stream, allowing for a new input attempt after an error.

Areas of Agreement / Disagreement

Participants generally agree on the need to handle invalid input but have differing levels of understanding regarding specific functions like 'cin.ignore()' and the implications of using 'cin >>' with different types of input.

Contextual Notes

There are unresolved questions about the exact behavior of 'cin' when encountering invalid input and the best practices for validating user input in C++. The discussion does not reach a consensus on the most effective method for input validation.

zeion
Messages
455
Reaction score
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
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.
 
I understand all that except for the cin.ignore(1000,'\n'); What is that for?
It seems to loop forever without the ignore.
 
Have you read comment in the code?
 
Yeah but I don't understand why it needs to do that.
 
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.
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 118 ·
4
Replies
118
Views
10K
Replies
12
Views
3K
  • · Replies 8 ·
Replies
8
Views
5K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 14 ·
Replies
14
Views
35K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 25 ·
Replies
25
Views
3K
  • · Replies 7 ·
Replies
7
Views
2K