Reputation: 1
I learn C++ by a book, and always when I have a task to do, I'll add some of my features. Now I wanted to add "check for dummy people", and if they write fractional number, not integer, the console will be closed. I wanted to do like that:
cin >> timing;
if (timing == double) {
...
But the Visual Studio compiler says "type name is not allowed"
.
Please, tell me how to do this check
Upvotes: 0
Views: 81
Reputation: 34628
The type of your timing
variable is static, as are all types in C++. I'm going to assume it's an std::string
. It will never be a double
or an int
. What you can do with it, is translate the information stored in it into another object of a different type.
For example, with std::stod
you can try and convert the string into a double
. This will return a double
for all inputs, e.g. "3.1"
, "0"
, "horse"
, or ""
. For the last two, however conversion will fail, and std::stod
will throw.
However, the return value is always a double
.
Now, you can check if the double
holds an integral value. For instance you can convert the value to long
and back and check if you get the same value. There are more sophisticated ways to check this, but it would be a good start.
Upvotes: 1