Ideas for error checking from command line

I am trying to read three doubles from a user's input on the command line. However, my error checking is something to be desired. If I don't enter the correct number of doubles or don't enter a double the program goes into an infinite loop. Here is the main which is handling the I/O:

If you enter nonsense (e.g. "abc" instead of a number) then you need to "clean up" in the input buffer, as scanf() happens to work in such a way that it reads everything up until it finds something it doesn't like/want. So if you have entered "abc", and scanf() expects a number, it will look at the 'a', say "I don't need that now", and go back to your function with nothing extracted from the input buffer. It is fairly easy to solve by using a loop like this [you may want to make it into a function]:

Code:

int ch;
while((ch = getchar()) != '\n' && ch != EOF);

--
Mats

Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.

Also, you might want to look for EOF, and if you find it, quit the program. You could check scanf()'s return value against EOF, or just use feof(). (Or ch after executing matsp's code.)

dwk

Seek and ye shall find. quaere et invenies.

"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell

Thank you that helped for the infinite loop issue, is there a way to make scanf() stop waiting if not enough inputs were entered and if there are too many inputs scanf() just ignore's the extras correct?

Thank you that helped for the infinite loop issue, is there a way to make scanf() stop waiting if not enough inputs were entered and if there are too many inputs scanf() just ignore's the extras correct?

My code works also to clean out extraneous "valid" input. It does not solve the problem of only one or two inputs when you expect 3. To solve that, you will need to read a string (for example fgets()) and then use sscanf() to read from the string - that ALSO solves the problem of dealing with "bad" input nicely, so you don't actually need any loops or such).

--
Mats

Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.