0
Data Types
Under types we have the numeric types. My question is, since we have the INT and FLOAT under the numeric data types. I was wondering is there any data type like in python the way we have the EVAL statement which holds both the int and floating points numbers in C++ rather than having to specify all the time but have something which may prevent an error from happening incase the user entered a wrong digit number that is an int in place of a float. I'm sure if my question is clear how I've phrased it but anyone understands, they can help me. Thank you!
7 Respostas
+ 1
python automatically works with float and int.No need for decleration. Division leads to a float.
print(10/2)
# 5.0
print(round(10/2))
# 5
+ 1
You need to check the type of a value before assigning it or to cast it before the assignment, if you can't use implicit declaration like it is supported by python. Most languages don't support that.
+ 1
In some applications it is not too uncommon to do this casting within try-catch-blocks, try to cast & assign as integer, break or catch and try to cast & assign as xyz and so on... Until finally no more data types supported, throw & handle.
+ 1
Templates in C++ like this here:
https://code.sololearn.com/cM3ws23MGrGK/?ref=app
+ 1
Thanks Sandra Meyer
0
But in a case where you want to ask the user to input a number and that user is not sure if it's a foat or int, hence to prevent the error from happening is there any function like the way "eval" works in python were the user can enter int or float without changing anything
0
You can always use a generic type, "Object".