C/C++ C++ how to check for variable type?

AI Thread Summary
To ensure user input is strictly an integer in C++, the `cin >>` operator can be used, which returns a boolean indicating the success of the input operation. If the input fails, a loop can be implemented to prompt the user again. The code snippet provided demonstrates this approach, using `cin.clear()` to reset the input stream's status and `cin.ignore(1000, '\n')` to skip invalid input until a newline is encountered. This is crucial because if a non-integer value is entered, the input stream remains at the invalid character, preventing further input attempts. The `cin.ignore()` function helps clear out the invalid input, allowing the user to try again. Additionally, to handle cases where a decimal number is entered, reading the input as a string and validating it before conversion to an integer is recommended.
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.
 
Thread 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...

Similar threads

Replies
8
Views
4K
Replies
5
Views
3K
Replies
14
Views
34K
Replies
10
Views
2K
Replies
25
Views
2K
Replies
7
Views
2K
Back
Top