Data Types | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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!

14th Jul 2020, 3:07 AM
Simangolwa Lifwatila
Simangolwa Lifwatila - avatar
7 Answers
+ 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
14th Jul 2020, 3:28 AM
Slick
Slick - avatar
+ 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.
14th Jul 2020, 3:36 AM
Sandra Meyer
Sandra Meyer - avatar
+ 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.
14th Jul 2020, 3:38 AM
Sandra Meyer
Sandra Meyer - avatar
+ 1
Templates in C++ like this here: https://code.sololearn.com/cM3ws23MGrGK/?ref=app
14th Jul 2020, 5:21 AM
Sandra Meyer
Sandra Meyer - avatar
+ 1
14th Jul 2020, 5:45 AM
Simangolwa Lifwatila
Simangolwa Lifwatila - avatar
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
14th Jul 2020, 5:12 AM
Simangolwa Lifwatila
Simangolwa Lifwatila - avatar
0
You can always use a generic type, "Object".
14th Jul 2020, 5:17 AM
Sandra Meyer
Sandra Meyer - avatar