"chuck" <ch****************@lmco.com> wrote in message
news:11**********************@g10g2000cwb.googlegr oups.com...
I need some help with validating user input. I am writing a C computer
program for an intro to C course. Here is the situation. I am
creating an application that will do currency conversions. The user
will be presented with a list of 5 selections they can make. They will
then be prompted for which selection they want to enter (which can only
be 1-5, no characters or anything like it). Once they select the
number, 1 for Euro, 2 for Deutsche Mark etc, it will prompt you for how
many US dollars you want to convert to your selected currency. After
entering a dollar number > 0, it will do the conversion and display it
for you. My problem has been is validating user input and how to go
about it. During the step for selecting what currency you want to
convert to and when entering the amount of use dollars, they have to be
certain data types. For the selection they can only be ints 1-5. If
it is anything else they will go thru a loop and be prompted again to
give a selection number (1-5). I have been unsuccessful in doing this
so I decided to start over. The link below has what I have so far and
now when I put a character say 'f' when it asks me for a selection
number, it asks me again for a selection # (meaning that i inputted a
wrong data type). The only thing is though that when I enter a 1 to
select Euro the app does absolutely nothing. I have a feeling I messed
up my loop. Can anyone help? The pastebin link is below.
http://pastebin.com/763164
If any further information is needed please let me know. Thank in
advance!
It is a naughty no-no to flush stdin. From the C FAQ:
12.26a: How can I flush pending input so that a user's typeahead isn't
read at the next prompt? Will fflush(stdin) work?
A: fflush() is defined only for output streams. Since its
definition of "flush" is to complete the writing of buffered
characters (not to discard them), discarding unread input would
not be an analogous meaning for fflush on input streams.
See also question 12.26b.
References: ISO Sec. 7.9.5.2; H&S Sec. 15.2.
12.26b: If fflush() won't work, what can I use to flush input?
A: It depends on what you're trying to do. If you're trying to get
rid of an unread newline or other unexpected input after calling
scanf() (see questions 12.18a-12.19), you really need to rewrite
or replace the call to scanf() (see question 12.20).
Alternatively, you can consume the rest of a partially-read line
with a simple code fragment like
while((c = getchar()) != '\n' && c != EOF)
/* discard */ ;
(You may also be able to use the curses flushinp() function.)
There is no standard way to discard unread characters from a
stdio input stream, nor would such a way necessarily be
sufficient, since unread characters can also accumulate in
other, OS-level input buffers. If you're trying to actively
discard typed-ahead input (perhaps in anticipation of issuing a
critical prompt), you'll have to use a system-specific
technique; see questions 19.1 and 19.2.
References: ISO Sec. 7.9.5.2; H&S Sec. 15.2.
-----------------------------------------------------------
I suggest that you investigate the is* family of functions (isdigit(),
ispunct(), etc.).
They will prove helpful for your purposes.
A simpler alternative is to simply call sscanf() and check for errors.