User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 397,770 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,531 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser:
Views: 5003 | Replies: 3
Reply
Join Date: Jan 2005
Posts: 1
Reputation: goforit is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
goforit goforit is offline Offline
Newbie Poster

Keyboard input or "stuck in a loop"

  #1  
Jan 5th, 2005
Hello,

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");
else
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.

Thanks,

JS
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Apr 2004
Posts: 3,462
Reputation: Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light 
Rep Power: 16
Solved Threads: 138
Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Keyboard input or "stuck in a loop"

  #2  
Jan 5th, 2005
Reply With Quote  
Join Date: Nov 2004
Location: India
Posts: 57
Reputation: harshchandra is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 1
harshchandra harshchandra is offline Offline
Junior Poster in Training

Re: Keyboard input or "stuck in a loop"

  #3  
Jan 6th, 2005
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[i] - '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...
Reply With Quote  
Join Date: Sep 2004
Posts: 6,050
Reputation: Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of 
Rep Power: 26
Solved Threads: 416
Super Moderator
Narue's Avatar
Narue Narue is offline Offline
Expert Meanie

Re: Keyboard input or "stuck in a loop"

  #4  
Jan 8th, 2005
>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[i] - '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:
Originally Posted by Naruizer
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);
        break;
      }
    }
    else
      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.
Member of: Beautiful Code Club.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb C++ Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 4:25 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC