yeah ,Occasionally you may need a routine in on of your programs that restricts input. An example required all numeric input, no commas or other funny characters. There are really no C routines to deal with this, so you must concoct your own. As a foundation, you can use the INPUT.C program introduced here
In this example, u needed a routine to input a numerical value. The scanf function could not be used; if the user typed in a number such as 40,000, then scanf would interpret that as two values, 40 and zero. The gets function could read input, but the comma again would cause only the value 40 to be read. The best solution would be to write a bulletproof input routine that would do several things:
Restrict the user from typing in more characters than necessary (in this case, only 5 digits)
Ensure that only numbers zero through 9 could be entered.
Allow the user the ability to backspace and erase as well as potentially cancel input.
Each of these conditions can easily be met with a modified input function,
The following program shows how such a routine could be written. Pay attention to the input function and the slew of case statements toward the end.
Name: S_INPUT.C
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#define CR 0x0d //carriage return
#define LF 0x0a //Line feed
#define BACKSPACE 0x08
#define NULL 0 //Empty character
#define TRUE 1
#define FALSE 0
void input(char *string, int length);
void main()
{
char string[6];
long value;
printf("Enter a 5-digit value: ");
input(string,5);
value = atol(string);
printf("\nYou entered %ld\n",value);
}
/* The customized input() routine */
void input(char *string, int length)
{
int done = FALSE;
int index = 0;
char ch;
string[0] = NULL; // init the buffer
do
{
ch = getch();
/* Check to see if the buffer is full */
if (index == length)
{
switch(ch)
{
case CR:
break;
case BACKSPACE:
break;
default:
ch = NULL;
break;
}
}
/* process the keyboard input */
switch(ch)
{
case CR: //Enter key
putchar(ch);
putchar(LF);
string[index] = NULL;
done = TRUE;
break;
case BACKSPACE:
if(index==0)
{
break;
}
else
{
putchar(ch);
putchar(' ');
putchar(ch);
index--;
break;
}
case NULL:
break;
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
putchar(ch);
string[index] = ch;
index++;
break;
default:
break;
}
}
while(!done);
}
Type the above source code into your editor.
Normally, the input function would allow any text to be entered. This is done by including the following snippet of code as the default:
putchar(ch);
string[index] = ch;
index++;
break;
However, in this program, that snippet is used only when the characters '0' through '9' are entered. The default condition - which holds true for all other characters - is merely a break statement. This means that only the numbers zero through 9 are allowed as input, and whatever string is modified by the input routine will be a number with no nonsense.
Similar modifications to the standard input routine can allow further restrictions. For example, you could modify the program to accept only uppercase characters. (In that case an isupper test would have to be made before input was examined.) Or you can restrict input to vowels or letters common to both Roman and Greek typefaces. It just requires a little deft manipulation and your very own input routine.