+ 14

[SOLVED] What's wrong in this????😅😅

look at this code try input a) 4 & 5 output = 9 (why can't the output is 9.0) b) 4 & 5.5 output = 9 (why can't it show some error. as input is float type value) c) 4.5 & 5.5. output = 4 ( why can't it show some error)& ( why it show 4 , may be it take the first input as 4 and second input as decimal of 4.5 Is equal to zero 0, hence the result is 4+0 = 4) and if that, then Is there a problem with compiler????? and plz 👉don't suggest to change data type😂 https://code.sololearn.com/cVNSd2wCP5U1/?ref=app

19th Dec 2017, 6:59 AM
Ankush Kumar
Ankush Kumar - avatar
4 odpowiedzi
+ 12
The code failed at the input stage, attempts to assign floating point number into integers will cause problem, as the value following the decimal point is not accepted, it becomes garbage in the input stream. The next input failed because the input stream has garbage value in it. Check if the input was correctly assigned to the variable. cin >> a; cout << "is input a good " << cin.good(); cin >> b; cout << "\nis input b good " << cin.good(); This way you know if both input was accepted well, cin.good() returns boolean, represented as 1 or 0.
19th Dec 2017, 10:11 AM
Ipang
+ 7
I know When i change its type to float or double it run perfect But i want to know how compiler complies input 4.5 & 5.5 As output 4
19th Dec 2017, 7:13 AM
Ankush Kumar
Ankush Kumar - avatar
+ 2
You have declared inputs as INT and when you take float values for these inputs it converts them to integer values. Declare inputs as float and then try it. https://code.sololearn.com/cNV6Z82ch5Dn/?ref=app Check this code, it will give right answers.
20th Dec 2017, 4:32 AM
ABDUL MANAN
ABDUL MANAN - avatar
+ 2
Like Deepak Gautama said. If you did not Assign a value to the integer then it assigns before the decimal 4.5 = 4 but since you assigned the value 0 to b then it checked the bad input and signed back to default.
24th Dec 2017, 11:37 PM
Jonathan Wilkins
Jonathan Wilkins - avatar