Another hint: Never use scanfscanf to read numbers from a terminal. If the user types in a letter, the input gets "stuck" - this and anything later in the same scanfscanf will not parse any input. (Read the spec for scanfscanf to see what I mean.)
Best way to do this sort of thing: (1) declare a buffer of size MAX_INPUT_LENGTH (of your choosing), (2) use fgets to read a buffer of that size, (3) use sscanf to parse the number out.
- Declare a buffer of size
MAX_INPUT_LENGTH(of your choosing) - Use
fgetsto read a buffer of that size - Use
sscanfto parse the number out.
The C library functions dealing with strings are a real disaster (as they can easily cause buffer overflow, among other problems). It's annoying that the safest way to do something as simple as what you're doing is as complex as this.