0

I need some improvement in my code plz have a look

https://code.sololearn.com/czhso26IJo5I/?ref=app

9th Aug 2020, 3:03 PM
Pr_At_H_Am
Pr_At_H_Am - avatar
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 }
9th Aug 2020, 3:20 PM
Shadow
Shadow - avatar
+ 1
Shadow Thanks a lot It's been very useful.
9th Aug 2020, 4:50 PM
Pr_At_H_Am
Pr_At_H_Am - avatar
9th Aug 2020, 5:08 PM
Pr_At_H_Am
Pr_At_H_Am - avatar