Solved

Limiting Input

Posted on 2004-09-18
5
210 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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
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…
The goal of this video is to provide viewers with basic examples to understand opening and writing to 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.

758 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now