- #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.
How can I check the type of the input?
Thanks.
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;
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.
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.
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.
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.
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.