Please someone help me with The following C code regarding input validation. | Sololearn: Learn to code for FREE!
Новый курс! Каждый программист должен знать генеративный ИИ!
Попробуйте бесплатный урок
0

Please someone help me with The following C code regarding input validation.

I need to validate the input as numeric and greater than zero and if not, then retake the input but It only takes input One time and goes into infinite loop after that, how do I prevent this please help. https://code.sololearn.com/cyX13zQLnkWN/?ref=app

7th Aug 2021, 7:47 AM
Kishan Khanna
Kishan Khanna - avatar
4 ответов
+ 2
The issue here is that when scanf fails to read input due to type mismatch, the input character remains within the input stream. One way to clear the input stream is through the use of getchar(). In the example below, we attempt to clear stdin until a newline or EOF is met. https://code.sololearn.com/cfFdq1K9SMB7/?ref=app
7th Aug 2021, 9:35 AM
Hatsy Rei
Hatsy Rei - avatar
0
I have tried it in other compilers, the same thing happens every time. please help!
7th Aug 2021, 8:29 AM
Kishan Khanna
Kishan Khanna - avatar
0
1. I'm using goto to jump to line where I took the input to take input again correctly. 2. Please suggest the right way.
7th Aug 2021, 8:56 AM
Kishan Khanna
Kishan Khanna - avatar
0
Thanks Hatsy rei got it😀
7th Aug 2021, 11:13 AM
Kishan Khanna
Kishan Khanna - avatar