r/C_Programming • u/UsualLonely4585 • May 29 '26
Question HELP!!!!
while(1){
if(scanf(" %d",&input_choice)==1){
break;
};
while(1){
if(getchar()=='\n'){
break;
}
}
};
Guys what am i doing wrong can you please tell me .
i am sorry if i am asking very basic thing . i read some documentation online but couldn't figure out what is going wrong
0
Upvotes
9
u/AlexTaradov May 29 '26
Cool title.
You can at least describe what you are trying to do and what is the issue.
Providing more complete code might be useful too.
1
u/Total-Box-5169 May 31 '26
The code is missing error checks. EOF (end of file) error would result in an infinite loop. Both scanf and getchar return a negative in case of error, so at the very least you need to break out of the loop in such case.
0
u/Level-Pollution4993 May 29 '26
So you are trying to take in a int value from the user in the first while loop and you're dealing with the buffer storing an extra \n after the scanf with the second nested while, is that right? Then whats the problem, I quickly tried it on a online compiler and it works?