0
I need some improvement in my code plz have a look
3 Answers
+ 3
Right now, you are deciding the type of the entered number by the second character in the string, since either the if-statement or the else-statement will be executed during the first iteration, followed by the loop's termination with "break".
Instead of looping over the string, why not use "strchr()" from the standard library to look for a point?
https://en.cppreference.com/w/c/string/byte/strchr
That way, you'll be able to get rid of the loop, and the type will be decided correctly.
Example:
// ...
cin >> num;
if ( strchr( num, '.' ) != NULL )
{
// float
}
else
{
// int
}
+ 1
Shadow Thanks a lot
It's been very useful.
+ 1
https://code.sololearn.com/c3e17R9YH3CH/?ref=app
Here's the improved code.