+ 1

Console.In.Peek

Can someone explain this part code in this code please: while (Console.In.Peek() != -1) { Console.Read(); } break; https://code.sololearn.com/cmw5Zx2j7SB5/?ref=app

9th Aug 2022, 9:41 AM
ŠšŠ°Š²ŃƒŠ½ šŸ‰
ŠšŠ°Š²ŃƒŠ½ šŸ‰ - avatar
4 Answers
+ 1
(Console.In.Peek() != -1) checks for the presence of any character in the input console, if there are no characters, then the result will be -1. But as far as I understand this cycle is inappropriate here. I'm not good with c#, but I would fix it.
9th Aug 2022, 11:40 AM
Solo
Solo - avatar
+ 1
Okay, now the last Console.Read() is not needed either, reading the input happens in the loop condition.ā˜ŗļø
9th Aug 2022, 1:18 PM
Solo
Solo - avatar
+ 1
Solo šŸ‘ thanks
9th Aug 2022, 3:58 PM
ŠšŠ°Š²ŃƒŠ½ šŸ‰
ŠšŠ°Š²ŃƒŠ½ šŸ‰ - avatar
+ 1
Okay, now let's go back to the Peek() method to avoid the infinite loop: šŸ˜Ž while(Console.In.Peek()!=-1){ ch = (char)Console.Read(); switch(ch){ ... Now you can enter any number of numbers, or enter nothing. šŸ˜‰
9th Aug 2022, 4:17 PM
Solo
Solo - avatar