First of all, other questions about usage of sscanf
do not answer my question because the common answer is to not use sscanf
at all and use fgets
or getch
instead, which is impossible in my case.
The problem is my C professor wants me to use scanf
in a program. It's a requirement.
However the program also must handle all the incorrect input.
The program must read an array of integers. It doesn't matter in what format the integers
for the array are supplied. To make the task easier, the program might first read the size of the array and then the integers each in a new line.
The program must handle the inputs like these (and report errors appropriately):
- 999999999999999...9 (numbers larger than integer)
- 12a3 (don't read this as an integer 12)
- a...z (strings)
- 11 aa 22 33
all in one line (this might be handled by discarding everything after 11)
- inputs larger than the input array
There might be more incorrect cases, these are the only few I could think of.
If the erroneous input is supplied, the program must ask the user to input again until
the correct input is given, but the previous correct input must be kept (only incorrect
input must be cleared from the input stream).
Everything must conform to C99 standard.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…