Validating user input?

C

chuck

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!
 
C

chuck

chuck said:
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!

Update: pastebin link is - http://pastebin.com/763213
 
D

Dann Corbit

chuck said:
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.
 
C

chuck

Dann said:
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.

Thanks for the response. My question was mainly dealing with how to
examine input thru loops and such.
 
O

octangle

If you want to learn to fish...

Have you run this in a debugger? Stepping through a program can be a
very educational way of learning how to create good logic.

If you just want a fish...

In reality this could be written with only one printf and one scanf
(and no getchar()) - place the prompting string and the input function
inside a while loop that will iterate the loop when a flag variable is
true (or false) depending upon whether you prefer testing positive or
negative conditions... the trick is to initialize the flag to force the
loop to be entered the first time... the input value should be tested
after the scanf with an if statement this if statement should change
the flag to a value that will cause the loop to exit when a valid value
is entered or in the else condition print an error message and leave
the flag alone (so that the loop will repeat and a new value may be
entered).

Either way enjoy your fish...
 
C

chuck

octangle said:
If you want to learn to fish...

Have you run this in a debugger? Stepping through a program can be a
very educational way of learning how to create good logic.

If you just want a fish...

In reality this could be written with only one printf and one scanf
(and no getchar()) - place the prompting string and the input function
inside a while loop that will iterate the loop when a flag variable is
true (or false) depending upon whether you prefer testing positive or
negative conditions... the trick is to initialize the flag to force the
loop to be entered the first time... the input value should be tested
after the scanf with an if statement this if statement should change
the flag to a value that will cause the loop to exit when a valid value
is entered or in the else condition print an error message and leave
the flag alone (so that the loop will repeat and a new value may be
entered).

Either way enjoy your fish...

Could you please be a little more descriptive I am very new to C and
found it a tad hard to follow in order to understand what i need to
code.
 
D

Dann Corbit

Could you please be a little more descriptive I am very new to C and
found it a tad hard to follow in order to understand what i need to
code.

Look up the isdigit() function, which will tell you if the character you saw
was a digit from 0 to 9.
Look up the ispunct() function, which will tell you if the character you saw
was punctuation.
If you allow scientific notation, you will also need to check for {perhaps}
'e', 'E', 'd' and 'D'.

Since it is currency, I would suggest that you can assume fixed point
format.

Also, float is a very poor choice for storing currency values. Double would
be a little bit better.

IMO-YMMV.

Reading the currency value into your program as an integer is a bad idea.
1.29 will be converted into 1 (or perhaps an error will be raised). I guess
that only allowing integral values would be bad for the purpose of the
assignment, but I do not know for sure.

You may also want to check for a currency symbol like the dollar sign or the
pound sign or the deutchmark sign or the euro sign, etc.

If you want to get fancy-pants, you could check for commas and alternative
formats like angle brackets for negative values (sometimes used in banking
applications because they are much more striking than a negative sign).
 
O

octangle

To be more descriptive I have to write the code for you... Is this what
you want? Is this for a grade?
 
O

octangle

Dann said:
Look up the isdigit() function, which will tell you if the character you saw
was a digit from 0 to 9.
Look up the ispunct() function, which will tell you if the character you saw
was punctuation.
If you allow scientific notation, you will also need to check for {perhaps}
'e', 'E', 'd' and 'D'.

Since it is currency, I would suggest that you can assume fixed point
format.

Also, float is a very poor choice for storing currency values. Double would
be a little bit better.

IMO-YMMV.

Reading the currency value into your program as an integer is a bad idea.
1.29 will be converted into 1 (or perhaps an error will be raised). I guess
that only allowing integral values would be bad for the purpose of the
assignment, but I do not know for sure.

You may also want to check for a currency symbol like the dollar sign or the
pound sign or the deutchmark sign or the euro sign, etc.

If you want to get fancy-pants, you could check for commas and alternative
formats like angle brackets for negative values (sometimes used in banking
applications because they are much more striking than a negative sign).

This is all good stuff, but I think that all their looking for is a
little help with syntax and structure as opposed to creating the next
internation currency trading killer app...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top