473,287 Members | 1,643 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,287 software developers and data experts.

Validating user input?

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!

Jun 6 '06 #1
9 5809

chuck wrote:
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

Jun 6 '06 #2
"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.
Jun 6 '06 #3

Dann Corbit wrote:
"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.


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

Jun 6 '06 #4
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...

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


Jun 6 '06 #5

octangle wrote:
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...

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


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.

Jun 6 '06 #6
"chuck" <ch********@comcast.net> wrote in message
news:11**********************@i39g2000cwa.googlegr oups.com...
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).
Jun 6 '06 #7

chuck wrote:
Update: pastebin link is - http://pastebin.com/763213 From the link:

if (iSelection = 1){
Replace = by ==

Spiros Bousbouras

Jun 6 '06 #8
To be more descriptive I have to write the code for you... Is this what
you want? Is this for a grade?

Jun 7 '06 #9

Dann Corbit wrote:
"chuck" <ch********@comcast.net> wrote in message
news:11**********************@i39g2000cwa.googlegr oups.com...
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).


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...

Jun 7 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: The Plankmeister | last post by:
Hi... What's the best method of validating input characters? I would like to prevent users submitting exotic characters (such as those acquired on Windows Systems by pressing ALT+) and thought...
2
by: bildad | last post by:
The following 'book example' of validating input seems to be incomplete. Since it is a beginner's book it may be intentional for simplicity. But I would like to know how to make this program work...
17
by: stathis gotsis | last post by:
Hello everyone, I am tying to come up with an elegant way to process some input data that come from a form. When the user hits the 'Submit' button, i want the form to appear again with the...
5
by: Ryan | last post by:
A binding navigator control adds the following code for when the Save button is clicked: Me.Validate() Me.UserBindingSource.EndEdit() Me.UserTableAdapter.Update(Me.UserDataSet.User)" You can...
21
by: gurdz | last post by:
Does anyone know how to perform data validation in C? I have searched google for every possible result, and I either end up with data validation for C++ or nothing at all. I have also searched...
5
by: Kavya | last post by:
I saw these two ways for validating input First Way -------------- #include <iostream> #include <limits> using namespace std; int main() {
232
by: robert maas, see http://tinyurl.com/uh3t | last post by:
I'm working on examples of programming in several languages, all (except PHP) running under CGI so that I can show both the source files and the actually running of the examples online. The first...
3
by: Louis | last post by:
I have a form with multiple input boxes. I want to validate each input box (and force user to correct it) before allowing user to move to another, either using tab key or a mouse click. I try...
3
by: William Gill | last post by:
I have decided that Since I have to update the processing on many of my forms, I'm going to start them all over from scratch. Before I begin I thought I'd solicit comments on using the PHP regex...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.