Solved

Limiting Input

Posted on 2004-09-18
213 Views
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
Question by:jyhiroko
• 2
• 2

LVL 36

Expert Comment

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

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

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

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

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

Question has a verified solution.

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

Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
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 opening and reading files in the C programming language.