Consider this scenario:
You ask the user for the original number. They enter this and then press enter:
The input buffer contains this:
Code:
'1', '2', '3', 'a', 'b', 'c', '\n'
So you read with scanf() and you get 123 as your number.
Question:
What will the next call to scanf() result in for a number? Hint: The buffer now contains this:
Code:
'a', 'b', 'c', '\n'
Basically, scanf() will ignore the non-numeric chars in this case, meaning the buffer is stuck this way. If you use scanf() you should be prepared for silly things like this. If you call getchar(), you end up with 'a'. Then you call again, and you end up with 'b'. One hackish solution to line up the input is to read from stdin until you hit either a '\n' or a newline char. For example:
Code:
int c;
...
while(((c = getchar()) != '\n') && (c != EOF));
This is answered in the FAQ btw, so you might get a better explanation from there.