Solved

Limiting Input

Posted on 2004-09-18
5
217 Views
Last Modified: 2010-04-15
Hi I need help on limiting input. That is to only allow the user to enter 1 char. I'm doing a program that has a menu, and that i would want to allow the user to only enter 1 digit and nothing more.

Eg.
Choice -> 1 (expected to see only 1 if the users enter "1bc" it still only shows 1 unless the user enter 2* or 3 and so forth.
so we won't see

Choice -> 1a
Choice -> 11
Choice -> bb
etc
Enter is required to proceed which is why i din use getch() after that using isdigit to double check.


Thanks for any help that can be rendered.
0
Comment
Question by:jyhiroko
  • 2
  • 2
5 Comments
 
LVL 36

Expert Comment

by:Zyloch
ID: 12093322
Hi jyhiroko,

Might not be the best way, and I'm not sure if C has some function like charAt, but you can use strncpy is one way:

http://www.harpercollege.edu/bus-ss/cis/166/mmckenzi/lect11/l11c.htm

Regards,
Zyloch
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 12093351
You can implement your own input function. Something like

int GetOption()
{
    char key;
    char option=0;

    do {
        key = getch();
        if (isdigit(key)) {
            option = key;
            putch(option);
        } else if (key==8) {  /* backspace */
            option = 0;
            putch(8);
        } else if (key==13) {  /* enter */
            if (option)
                return option;
        }
    }
}

0
 
LVL 55

Assisted Solution

by:Jaime Olivares
Jaime Olivares earned 50 total points
ID: 12093506
Sorry, some mistakes (writting from my memory);

char GetOption()
{
    char key;
    char option=0;

    do {
        key = getch();
        if (isdigit(key)) {
            option = key;
            putch(option);
        } else if (key==8) {  /* backspace */
            option = 0;
            putch(8);
        } else if (key==13) {  /* enter */
            if (option)
                break;
        }
    } while (1);  /* Infinite loop */
    return option;
}
0
 
LVL 9

Accepted Solution

by:
ankuratvb earned 75 total points
ID: 12094413
Hope this is what you're looking for.It allows you to enter only one digit,if you enter another digit after you've already entered one,the previous digit is replaced with the new digit.

The value is accepted only when you press enter.

char GetOption()
{
   char key;
   char option=-1;

   do {
       key = getch();
       if(key==13) break;
       if (isdigit(key)) {
         option = key;
         if(option!=-1)
            putch(8);
         putch(option);
       }
   } while (1);  /* Infinite loop */
   return option;
}

int main()
{
char ch;
ch=GetOption();
printf("%c",ch);
}
0
 
LVL 9

Expert Comment

by:ankuratvb
ID: 12094418
Change this line:
> if(key==13) break;

to

if(key==13 && option!=-1) break;

This will keep asking for a digit even if the user presses enter without entering a digit.
0

Featured Post

ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

792 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question