+ 2
If (a) is not an integer how can i do that with c++
3 ответов
+ 2
Compare the (a) to a dummy int. Because the the typeid name() is "library implementation dependent", (my code::blocks return an 'i'), this how I manged to get round it (I can't remember why I needed to do it now).
int dummyInt;
int myint = 50;
if(typeid(dummyInt).name() == typeid(myint).name())
cout << myint << " is an int";
+ 2
Why would you ever need to do this though, unless you're working with templates? ( in which case there are much better ways of doing this )
It's not like
int myint;
can suddenly change its type to a float. This is not javascript.
It is, and will always be, an int.
In case a is a string, this is not the way of doing things.
Parse it and check if it contains 1 dot or a comma depending on the country, for a floating point or for any non digit character, in that case.
+ 1
This link has some information about how to find the type of a variable:
https://stackoverflow.com/questions/11310898/how-do-i-get-the-type-of-a-variable