Usually, an infinite loop results from a programming error - for example, where the conditions for exit are incorrectly written. Intentional uses for infinite loops include programs that are supposed to run continuously, such as product demo s or in programming for embedded system s.
while loop represents the infinite condition as we provide the '1' value inside the loop condition. As we already know that non-zero integer represents the true condition, so this loop will run infinite times. We can also use the goto statement to define the infinite loop.
I think in none of the case you will get a -1.So if you want to terminate the loop you can prompt the user to give a special character that can signify the end of input. Or if you are reading it from a file then you can use the EOF to terminate the loop.
You can press Ctrl + C .
scanf
consumes only the input that matches the format string, returning the number of characters consumed. Any character that doesn't match the format string causes it to stop scanning and leaves the invalid character still in the buffer. As others said, you still need to flush the invalid character out of the buffer before you proceed. This is a pretty dirty fix, but it will remove the offending characters from the output.
char c = '0';
if (scanf("%d", &number) == 0) {
printf("Err. . .\n");
do {
c = getchar();
}
while (!isdigit(c));
ungetc(c, stdin);
//consume non-numeric chars from buffer
}
edit: fixed the code to remove all non-numeric chars in one go. Won't print out multiple "Errs" for each non-numeric char anymore.
Here is a pretty good overview of scanf.
I think you just have to flush the buffer before you continue with the loop. Something like that would probably do the job, although I can't test what I am writing from here:
int c;
while((c = getchar()) != '\n' && c != EOF);
scanf()
leaves the "a
" still in the input buffer for next time. You should probably use getline()
to read a line no matter what and then parse it with strtol()
or similar instead.
(Yes, getline()
is GNU-specific, not POSIX. So what? The question is tagged "gcc" and "linux". getline()
is also the only sensible option to read a line of text unless you want to do it all by hand.)
Due to the problems with scanf
pointed out by the other answers, you should really consider using another approach. I've always found scanf
way too limited for any serious input reading and processing. It's a better idea to just read whole lines in with fgets
and then working on them with functions like strtok
and strtol
(which BTW will correctly parse integers and tell you exactly where the invalid characters begin).
Rather than using scanf()
and have to deal with the buffer having invalid character, use fgets()
and sscanf()
.
/* ... */
printf("0 to quit -> ");
fflush(stdout);
while (fgets(buf, sizeof buf, stdin)) {
if (sscanf(buf, "%d", &number) != 1) {
fprintf(stderr, "Err...\n");
} else {
work(number);
}
printf("0 to quit -> ");
fflush(stdout);
}
/* ... */
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With