Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Validating keyboard input

Reply
Thread Tools

Validating keyboard input

 
 
bildad
Guest
Posts: n/a
 
      10-23-2005
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 for all invalid
input. Just for example, if user inputs 'abc' an error is caught, but if
user inputs '432' the program hangs. Any clarification appreciated.

Thanks,

Bill


#include <stdio.h>

int main(void)
{
float temp_i, temp_o;
char which_i, which_o;
char junk;

printf("Enter a temperature and indicate\n");
printf("if it's Fahrenheit or Celsius [##.# C/F]: ");
if (scanf("%f %c", &temp_i, which_i) == 2)
{
switch (which_i)
{
case 'C':
case 'c':
temp_o = (temp_i * (9.0/5.0)) + 32;
which_o = 'F';
break;

case 'F':
case 'f':
temp_o = (temp_i - 32) * (5.0/9.0);
which_o = 'C';
break;

default:
which_o = 0;
break;
}
if (which_o)
{
printf("%0.1f %c is %0.1f %c.\n",
temp_i, which_i, temp_o, which_o);
}
else
{
printf("You failed to enter C or F.\n");
}
}
else
{
printf("You failed to use the proper syntax.\n");
}

do {
junk = getchar();
} while (junk != '\n');

getchar();

return 0;
}
 
Reply With Quote
 
 
 
 
Malcolm
Guest
Posts: n/a
 
      10-23-2005

"bildad" <(E-Mail Removed)> wrote
> 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 for all invalid input.
> Just for example, if user inputs 'abc' an error is caught, but if user
> inputs '432' the program hangs. Any clarification appreciated.
>
> Thanks,
>
> Bill
>
>
> #include <stdio.h>
>
> int main(void)
> {
> float temp_i, temp_o;
> char which_i, which_o;
> char junk;
>
> printf("Enter a temperature and indicate\n");
> printf("if it's Fahrenheit or Celsius [##.# C/F]: ");
> if (scanf("%f %c", &temp_i, which_i) == 2)
> {
> switch (which_i)
> {
> case 'C':
> case 'c':
> temp_o = (temp_i * (9.0/5.0)) + 32;
> which_o = 'F';
> break;
>
> case 'F':
> case 'f':
> temp_o = (temp_i - 32) * (5.0/9.0);
> which_o = 'C';
> break;
>
> default:
> which_o = 0;
> break;
> }
> if (which_o)
> {
> printf("%0.1f %c is %0.1f %c.\n",
> temp_i, which_i, temp_o, which_o);
> }
> else
> {
> printf("You failed to enter C or F.\n");
> }
> }
> else
> {
> printf("You failed to use the proper syntax.\n");
> }
>
> do {
> junk = getchar();
> } while (junk != '\n');
>
> getchar();
>
> return 0;
> }


The problem is that scanf() eats whitespace, so the newline is consumed if
you enter a valid number, but not if you enter alphabetical characters
(because the function stalls on the first non-digit).

Then the program hits the getchar() lines, because only one field has been
converted. I suspect it won't hang if you enter a few characters.

It's not well written, but scanf() is a hard function to use in a solid
fashion. In production code you would probably call a line gobbling function
(fgets has its own problems) and then use sscanf().


 
Reply With Quote
 
 
 
 
Keith Thompson
Guest
Posts: n/a
 
      10-23-2005
bildad <(E-Mail Removed)> writes:
> 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 for
> all invalid input. Just for example, if user inputs 'abc' an error is
> caught, but if user inputs '432' the program hangs. Any clarification
> appreciated.


It doesn't really hang; it just waits for you to input a
non-whitespace character.

>> #include <stdio.h>

>
> int main(void)
> {
> float temp_i, temp_o;
> char which_i, which_o;
> char junk;
>
> printf("Enter a temperature and indicate\n");
> printf("if it's Fahrenheit or Celsius [##.# C/F]: ");
> if (scanf("%f %c", &temp_i, which_i) == 2)


The format string consists of 3 directives:

"%f" reads a number in floating-point format.

" " reads input up to the first non-whitespace character.

"%c" reads a single character, which may or may not be whitespace.

So if the user types "432" followed by a newline, the "%f" consumes
the "432", and the " " consumes the newline (and any whitespace
following it). As soon as the user types a non-whitespace character
(such as 'F' or 'C'), the scanf() will consume it and finish.

Or rather, it would if the third argument to the scanf() call were
correct. Given the "%c" directive, the corresponding argument needs
to be a pointer-to-char; you've given it a char. Change "which_i" to
"&which_i".

For better error handling, if you want to require all the input to be
on one line, you can use fgets() to read the entire line, then use
sscanf() to validate and parse it. sscanf(), unlike scanf(), fails
immediately if the input is incomplete, rather than waiting for more
input to appear.

You should also handle the case of fgets() reading an incomplete line.

--
Keith Thompson (The_Other_Keith) http://www.velocityreviews.com/forums/(E-Mail Removed) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
can I use scanf to get input (some times user enters input sometimes not, just hit keyboard)? santa19992000@yahoo.com C Programming 4 09-09-2005 03:38 AM
validating user form input with vs.net/vb.net front end and backend darrel ASP .Net 1 06-24-2004 10:07 PM
Please help: Validating input from a combination of controls Paul ASP .Net 1 11-29-2003 08:53 PM
validating input (Me.request.form.get(0)) Thierry ASP .Net 1 11-11-2003 10:50 AM
validating input from the form Thierry ASP .Net 1 11-10-2003 02:46 PM



Advertisments