if i do not know the kind of input the user will enter what data type do i introduce. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

if i do not know the kind of input the user will enter what data type do i introduce.

say if i introduce int. and the user enters a decimal. it could affect the code.

5th Feb 2022, 6:19 PM
Jojo Emeka
Jojo Emeka - avatar
8 Answers
+ 7
Which programming language?
5th Feb 2022, 6:59 PM
Miss Selenophilia 🌙
Miss Selenophilia 🌙 - avatar
+ 4
What program are you trying to write? and please specify a relevant programming language in the tags above.
5th Feb 2022, 6:33 PM
Ipang
+ 4
You can take a string variable because it works for all data types and then put some if else to put it to the required data type
5th Feb 2022, 7:59 PM
Adi Nath Bhawani
Adi Nath Bhawani - avatar
+ 4
Hi Manav Roy, Sorry but it seems your example doesn't work as I tested it. `auto` type specifier doesn't help for this case. A variable with `auto` type specifier needs an initializer for its value, which is used by the compiler ti deduce a type considered proper for the data. https://en.cppreference.com/w/cpp/language/auto
6th Feb 2022, 3:45 AM
Ipang
+ 2
In python there is no such requirement as there is automatic data type assignment when a variable is Initialized. For languages such as C++ there is "auto" keyword for automatic data type assigning.
7th Feb 2022, 9:35 AM
Shivansh Chauhan
Shivansh Chauhan - avatar
+ 1
c++
5th Feb 2022, 8:17 PM
Jojo Emeka
Jojo Emeka - avatar
+ 1
Jojo Emeka an option is to find the int of the input and find out if there is more to the original input minus the int. If so it's a double. Here is an example https://code.sololearn.com/chwr4gtF8B3W/?ref=app
6th Feb 2022, 6:24 AM
BroFar
BroFar - avatar
0
Manav Roy Just what i needed thanks bro!!
5th Feb 2022, 8:21 PM
Jojo Emeka
Jojo Emeka - avatar