Keyboard input or "stuck in a loop"

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jan 2005
Posts: 1
Reputation: goforit is an unknown quantity at this point 
Solved Threads: 0
goforit goforit is offline Offline
Newbie Poster

Keyboard input or "stuck in a loop"

 
0
  #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
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,358
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 237
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Keyboard input or "stuck in a loop"

 
0
  #2
Jan 5th, 2005
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 68
Reputation: harshchandra is an unknown quantity at this point 
Solved Threads: 1
harshchandra harshchandra is offline Offline
Junior Poster in Training

Re: Keyboard input or "stuck in a loop"

 
0
  #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 Quick reply to this message  
Join Date: Sep 2004
Posts: 7,625
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 717
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Keyboard input or "stuck in a loop"

 
0
  #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:
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. int main(void)
  5. {
  6. char buffer[BUFSIZ], *end;
  7. int value;
  8.  
  9. while (fgets(buffer, sizeof buffer, stdin) != NULL) {
  10. if (buffer[0] != '\n') {
  11. value = (int)strtol(buffer, &end, 0);
  12.  
  13. if (*end == '\n' || *end == '\0') {
  14. printf("The value was %d\n", value);
  15. break;
  16. }
  17. }
  18. else
  19. fprintf(stderr, "Please enter a number\n");
  20. }
  21.  
  22. return 0;
  23. }
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.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC