By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,434 Members | 1,858 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,434 IT Pros & Developers. It's quick & easy.

Validating keyboard input

P: n/a
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;
}
Nov 15 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a

"bildad" <bi****@wi.rr.com> 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().
Nov 15 '05 #2

P: n/a
bildad <bi****@wi.rr.com> 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) ks***@mib.org <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.
Nov 15 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.