+ 4
How to check if the input is an integer in C++?
I ask the user to input an integer number. How to check if the input is an integer and not for example a float? int x; cin>>x; If the user inputs a float, C++ will automatically transform it into an integer, so I cannot use for example if(floor(n)!=n) to check the type (and similar solutions) Thanks!
23 Respostas
+ 6
the code above serves as an exemple:
#include <iostream>
using namespace std;
int x;
int main(){
cout << "Enter a number: ";
while (!(cin >> x)){
cout << "Ooops! Non integer value entered!" << endl;
cin.clear();
cin.ignore(40,'\n');
};
return 0;
}
+ 4
hey! easy, bro's. we r still learning this stuff. n learning with each other. our c++ comments r always wellcome. hug's from brazil đ§đ·
+ 2
@swim I think the solution doesn't work because auto doesn't work like in python, as you said yourself, and causes a compile error as it is (I tried). What you were trying to do there is better achieved with float or double instead of auto. Once you make it work, abs() is not needed, I thought we settled that.
Anyway, if in the program one needs several inputs, like:
int a, b, c, d;
cin >> a;
cin >> b;
cin >> c;
cin >> d;
and the user enters asd -12.3qwe fgh 123
with your solution a will be 0, cin will get into an error state, and all subsequent inputs will be garbage - possible cause for a crash, if, say, one of them is used as an index to an array.
With Davis' solution it will see the wrong input and discard it, thus a will be -12, b will be 123, and it will continue to ask for input until it has enough correct input for all (if it never gets the correct input, it will not go further)
With my "solution" a will be 0, b will be -12, c will be 0 again, d would be 123, and would carry on making the best of what the user gives, without bothering him for better input (I had the Sololearn method of input in mind, where you don't get a second chance to input correct values if you didn't do it at first :) )
As I said, irl I'd use Davis' solution, it's the best.
+ 1
I guess you can declare float or double x and then use floor, like you said.
Or, if you really want to be thorough (maybe user wants to input anything, not necessarily numbers) make x a string then try to convert it with atoi()/atof().
But really, if you ask the user to enter an int and they input something else and the program goes haywire, it's their fault :)
+ 1
#include <cstdlib> // for atoi()
...
string s;
cin >> s; // can read anything
int x = atoi(s.c_str()); // convert to int
Note: .c_str() method of string gives us the char* required by atoi()
For float you would do:
float y = atof(s.c_str());
+ 1
@swim I don't think it makes any difference if the numbers are negative. And auto needs to be initialized, I think, in which case you'd be better off declaring a float, imo :)
Anyway, I noticed that wrong input type doesn't crash the program in the playground, it just inputs 0 (like my solution, and yours I think). So idk what worried siri :)
+ 1
@swim I suppose you meant to say f = -5.3
But -5.3 - (-5) == -5.3 + 5 == -0.3
The compiler in the playground gives an error at auto though :(
It needs to be initialized at declaration, like this:
auto inval = 1.1;
(or something).
Which is equivalent with declaring inval as a float. Really, if you put float instead of auto you get the same result.
+ 1
you can leave the comments, it is an interesting discussion, and the auto type was indeed what I was looking for, thanks!!
+ 1
@swim because most of our comments were irelevant, preferably only clean solutions should be left (I still believe yours doesn't work, despite being chosen, but I don't wanna push you aside or anything; mine is not the best either, personally I'd use Davis' solution).
Anyway, I don't think I'm gonna follow this thread anymore so maybe just leave it as it is.
0
I want to prevent the program to crash if the user inputs a different type..
0
@swim if you'd like, we could delete all the comments and leave only the initial solutions. Maybe edit yours without the abs() if I managed to convince you they're not needed.
Btw, thanks to you, I learned about auto today.
0
@swim nevermind, nobody reads this anyway :)
Davis gave a better solution. I'd just cin.ignore(2000000, '\n');
in case the user copy/pastes an entire book in the input :)
@siri don't be mistaken about auto, it's not what you need in this particular problem, it doesn't change its type at runtime to accomodate your data type. Better look at cin.clear() and cin.ignore() because these seem to be the proper way to handle wrong input.
0
what the answer?