•
•
•
•
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
![]() |
•
•
Join Date: Jan 2005
Posts: 1
Reputation:
Rep Power: 0
Solved Threads: 0
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
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
•
•
Join Date: Nov 2004
Location: India
Posts: 57
Reputation:
Rep Power: 0
Solved Threads: 1
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...
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...
>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:
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 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; }
Member of: Beautiful Code Club.
![]() |
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
•
•
•
•
•
•
•
•
DaniWeb C++ Marketplace
Similar Threads
- school project: simple C++ game (need help with keyboard input) (C++)
- How do I get a form to capture ALL keyboard input? (C#)
- Keyboard Input (Java)
- Capturing Keyboard Input (C)
Other Threads in the C++ Forum
- Previous Thread: why can't you use a switch statment with a string?
- Next Thread: This may be a stupid question....



Linear Mode