New course! Every coder should learn Generative AI!
Try a free lesson0
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 ?
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
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.
0
Cain Eviatar Give alphabet as input.
0
Nikhil Sharma huh? I didn't get you..
he never mentioned that he's reading the number as a string.
0
Cain Eviatar There are bugs in your code.
Try these input
3
34ff
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.
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?
0
Nikhil Sharma
is that what you want?
input 3 = returm int
input 3.5f = return float
0
Cain Eviatar I suppose thats what has been asked.
0
https://code.sololearn.com/cx67x5JBFIzL/?ref=app
bad but simple solution.
I wouldn't use it in real project.