Mittwoch, 30. März 2011

scanf() error handling

scanf() error handling

#define FLUSE while (getchar() != '\n')

. . .
printf("\nPls enter the num of units sold "0;
while(scanf("%d", &units) != 1)

// scanf returns 1 if number read correctly

printf("\aInvalid number. try again :");

} // while

1. that scanf() thinks that these "invalid data" are the beginning of the next field.

2. its purpose is to read through th input stream looking for the end of a line. When it finds the end of the line, it terminates.

3. When it find the newline, it throw it away too, but at point, the loop stops.

4. In other words, it flushes the input stream to a newline character.

Keine Kommentare: