Why the secondNumber is 0? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
27th Apr 2022, 6:44 PM
Ноил Кизилов
Ноил Кизилов - avatar
8 Answers
+ 2
On sololearn you have to input all values at the beginning. input format: 2 [enter] 3 If you enter only one value, secondNumber will be 0. Not sure but maybe because 0 is the default value for integers.
27th Apr 2022, 6:49 PM
Denise Roßberg
Denise Roßberg - avatar
+ 2
It's not. Unless you entered "0", in which case the answer is "because you entered it".
27th Apr 2022, 6:50 PM
Simon Sauter
Simon Sauter - avatar
+ 2
If the first one is empty, we have an empty string that seems not to be cast to an integer. For the second one we reached the end of the stream, which is null (no empty string). I am just guessing.
27th Apr 2022, 9:33 PM
Lisa
Lisa - avatar
+ 1
Simon Sauter I only entered 5 without a return and second number is zero. So maybe an empty input is converting to zero by the Convert.ToInt32
27th Apr 2022, 9:12 PM
Paul K Sadler
Paul K Sadler - avatar
+ 1
Paul K Sadler Denise Roßberg I didn't notice that. Interesting. If you enter only one number the second is printed as "0", but if you enter no number the program throws an error.
27th Apr 2022, 9:28 PM
Simon Sauter
Simon Sauter - avatar
+ 1
Console.ReadLine() returns null if there is nothing to be read. Looks like null will just be cast to 0: Console.WriteLine(Convert.ToInt32(null));
27th Apr 2022, 9:28 PM
Lisa
Lisa - avatar
+ 1
Lisa any idea why that works for the second input, but not the first?
27th Apr 2022, 9:29 PM
Simon Sauter
Simon Sauter - avatar
0
Lisa that sounds like a reasonable guess
27th Apr 2022, 9:34 PM
Paul K Sadler
Paul K Sadler - avatar