/users/9264/steelhouse ♪ always have to check the value that comes back ♪ each scanf() (See. man 3 scanfbut in your case it's okay to do this (1) and correctly program the processing of errors.UPD /users/9264/steelhouse As promised scanf♪This function tries to introduce the number of arguments (in accordance with the format) and returns the number success Readed arguments or EOF (in case of end of input flow (File)). Number scanf First passes the dividers (problem, tabulation, symbol of the new row, etc.) and then converts the numerals into a decimal number (natural, sign + or - allowed). As soon as we meet. not number scanf Turns to reading the next argument, But this "not numerical" remains in the flow!♪That's why you have a program that's cycling. You read the numbers and if you met the letter (or the other one not fitting the symbol), it stays. Next. scanf Trying to read it and leaving it in the flow, etc.The solution is to analyse the number, returned scanf♪ If it is less than expected (and not EOF (for EOF, it must be completed) Pass all the symbols to the new line♪Here's a little example.#include <stdio.h>
#include <stdlib.h>
// прочтем все символы до новой строки
// вернем (для любопытных) их количество
// или EOF (для анализа конца файла (это уже не пустое любопытство))
static int
skip()
{
int c, n = 0;
if (!feof(stdin)) // убедимся, что нужно читать
while ((c = getchar()) != EOF) {
n++;
if (c == '\n')
return n;
}
return EOF;
}
int
main ()
{
int a, b, i = 0, rc;
while (rc != EOF) {
printf ("Enter a,b: "); fflush(stdout);
rc = scanf("%d%d",&a,&b);
printf ("rc = %d a = %d b = %d\n", rc,a,b);
#if ERRDEMO
if (rc == 2)
i = 0;
else if (++i > 5)
break;
#else
if (rc != EOF)
if (rc != 2) {
printf ("Input error rc = %d\n",rc);
printf ("skip %d characters\nTry again ",rc = skip());
}
#endif
}
exit (0);
}
#if ERRDEMO#else...#endifIt's a (if you don't know) imputation directive. I mean, if you're computing gcc a.c
the code between #else and #endif, and ifgcc -DERRDEMO a.c
the code between #if and #else. Naturally, the code is out of #if... #endif is compiled always.Now look at the results (and you can also collect it under the Wind (gcc checked) and try different options)avp@avp-xub11:~/hashcode$ gcc a.c -DERRDEMO
avp@avp-xub11:~/hashcode$ ./a.out
Enter a,b: 1 2
rc = 2 a = 1 b = 2
Enter a,b: 3 a
rc = 1 a = 3 b = 2
Enter a,b: rc = 0 a = 3 b = 2
Enter a,b: rc = 0 a = 3 b = 2
Enter a,b: rc = 0 a = 3 b = 2
Enter a,b: rc = 0 a = 3 b = 2
Enter a,b: rc = 0 a = 3 b = 2
avp@avp-xub11:~/hashcode$ gcc a.c
avp@avp-xub11:~/hashcode$ ./a.out
Enter a,b: 1 2
rc = 2 a = 1 b = 2
Enter a,b: 3 a
rc = 1 a = 3 b = 2
Input error rc = 1
skip 2 characters
Try again Enter a,b: 3 4
rc = 2 a = 3 b = 4
Enter a,b: 5 6f
rc = 2 a = 5 b = 6
Enter a,b: rc = 0 a = 5 b = 6
Input error rc = 0
skip 2 characters
Try again Enter a,b: 5 6
rc = 2 a = 5 b = 6
Enter a,b: rc = -1 a = 5 b = 6
avp@avp-xub11:~/hashcode$
rc = -1 at the end, it's an EOF stamp.Success.UPD 2A little bit. skip() For better work on entry.