As will become quickly & painfully evident, I am new to "C" programming and trying to teach myself.

I am trying to use scanf function to get numeric input from the keyboard.

My code looks like this
(NOTE: Please forgive syntax errors as I am not copying and pasting working code, but retyping)

function = 0;
while (function < 1 || function > 3) {
printf ("Please enter a Function code. 1 = W 2 = W/V 3 = V\n");
scanf ("%i", &function);
if (function < 1 || function > 3)
printf ("BAD FUNCTION CODE, Please reenter");
printf ("\n");

This code does what I want it to do as long as a number is entered. However, if an alpha is entered it gets stuck in an endless loop, querrying for a new function code but never stopping for user input. Apparently, once the alpha is entered it is stuck in the keyboard buffer and keeps getting read over and over.

I am using an a old version of Borland C++ (V4.52) that I found lying around work and running under Windows 98.

Is there some way to avoid this while still using scanf? Should I just chunk scanf and use getchar and then convert the ASCII to an integer?

Finally, I would like to learn how program "right" so, if while you are looking at the problem if you have any comments on things that I am doing one way that would be better done another (can't be "wrong", since the code runs ..... right?) I would appreciate it.



Recommended Answers

All 3 Replies

if U r getting stuck to a any alpa input ...the best possible way is to take the input as a string .....and then convert it to numeric one......u can also use If Then Else to check whether the input is alpa or number by using ASCII codes......the program codes will be as follows...

char str[10]; /* declaring array */
gets(str); /* Getting a dtring from the user */

then call a function and and return each letter of the array as str - '0' ...simialr to atoi function ... and then checj for ascii code whether it is number or alpha....65 is for A and 97 is for a......i hope u get that...

>gets(str); /* Getting a dtring from the user */
I know you didn't just use gets. You couldn't have. Nobody could possibly be that stupid after nearly 30 years of everybody with a brain screaming the peril of gets at the top of their lungs.

>then call a function and and return each letter of the array as str - '0'
That won't work unless you do a little more than just convert each digit to its integral value.

>simialr to atoi function
I know that atoi is almost as devilish as gets, but why not use a standard library function such as strtol so that you don't have to write some silly (and probably broken) function that tries to duplicate the functionality of something that already exists everywhere!

>i hope u get that
I don't get it. What if I'm using EBCDIC? The only character conversion guaranteed to work involves numeric digits. Those are the only characters guaranteed by the standard to be adjacent with ascending values.

Let's Naruize your answer:

Your problem is that scanf is trying to read an integer and chokes on anything but a valid character for integers. Error handling is easier if you do all input as string data and then either validate the string, or try to convert it to an integer. You might do it like this:

#include <stdio.h>
#include <stdlib.h>

int main(void)
  char buffer[BUFSIZ], *end;
  int value;

  while (fgets(buffer, sizeof buffer, stdin) != NULL) {
    if (buffer[0] != '\n') {
      value = (int)strtol(buffer, &end, 0);

      if (*end == '\n' || *end == '\0') {
        printf("The value was %d\n", value);
      fprintf(stderr, "Please enter a number\n");

  return 0;

By the way, you're officially on my idiot list. I would appreciate if you didn't try to answer questions until you learn the correct answers yourself.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.