C++ | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

C++

Write C++ program to read a number in the main function, then pass it to a function named check( ) to check the number if it is an integer or float ?

15th May 2018, 9:00 PM
Hussam Raheem
Hussam Raheem - avatar
10 Answers
0
scan the input as string and check whether string is [0-9]*.[0-9]* or [0-9]* PS: look for . in string
15th May 2018, 9:24 PM
Nikhil Sharma
Nikhil Sharma - avatar
0
https://code.sololearn.com/cZbRt8hKN6Ku/?ref=app a simple compile time solution to check the type of the inputted number with the use of the powerful tools C++17 provides.
15th May 2018, 9:36 PM
Cain Eviatar
Cain Eviatar - avatar
0
Cain Eviatar Give alphabet as input.
15th May 2018, 9:38 PM
Nikhil Sharma
Nikhil Sharma - avatar
0
Nikhil Sharma huh? I didn't get you.. he never mentioned that he's reading the number as a string.
15th May 2018, 9:39 PM
Cain Eviatar
Cain Eviatar - avatar
0
Cain Eviatar There are bugs in your code. Try these input 3 34ff
15th May 2018, 9:40 PM
Nikhil Sharma
Nikhil Sharma - avatar
0
Nikhil Sharma you're passing an Integer value into float. ( Compiler is converting the int to float ) if you want to check if the value that has being typed is int or float you'll need to use std::ostringstream for casting and a little parsing for the string.
15th May 2018, 9:42 PM
Cain Eviatar
Cain Eviatar - avatar
0
Cain Eviatar Your code needs to check whether the entered number is integer or float. I entered 3, it needs to give integer as output. Isn't it? Or am i missing something?
15th May 2018, 9:45 PM
Nikhil Sharma
Nikhil Sharma - avatar
0
Nikhil Sharma is that what you want? input 3 = returm int input 3.5f = return float
15th May 2018, 9:47 PM
Cain Eviatar
Cain Eviatar - avatar
0
Cain Eviatar I suppose thats what has been asked.
15th May 2018, 9:52 PM
Nikhil Sharma
Nikhil Sharma - avatar
0
https://code.sololearn.com/cx67x5JBFIzL/?ref=app bad but simple solution. I wouldn't use it in real project.
15th May 2018, 10:10 PM
Cain Eviatar
Cain Eviatar - avatar