input validation for integer
This Code Validate The Input provided by You Within The Integer Range -32768 to 32767 at Run Time. It checks each character and shows on the screen if it is valid .
AI
ИИ-обзор: This codebase represents a historical implementation of the logic described in the metadata. Our preservation engine analyzes the structure to provide context for modern developers.
Исходный код
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
void main()
{
int n,i,l;
int MAX = 4;
char s[7],ch;
clrscr();
printf("\nEnter the Integer Value within -32768 and 32767: ");
i=0;
while((ch = getch())!='\r'||i==0 || (MAX==5&&i==1))
{
if(ch == '\b' && i >0)
{
printf("%c",'\b');
printf("%c",' ');
printf("%c",'\b');
i--;
if(s[i] == '-')
{
--MAX;
}
}
else if( i == 0 && ch == '-')
{
s[i++] = ch;
putch(ch);
++MAX;
}
else if(i== MAX)
{
s[i] = '\0';
n=atoi(s);
if(s[0] == '-' && (n > -3276 || (n == -3276 && ch <= '8')))
{
s[i++] = ch;
putch(ch);
}
else if(s[0] != '-' && (n < 3276 || (n == 3276 && ch <= '7')))
{
s[i++] = ch;
putch(ch);
}
}
else if(i < MAX && ch >= '0' && ch <='9')
{
s[i++] = ch;
putch(ch);
}
}
s[i] = '\0';
n=atoi(s);
printf("\n\nThe Value, You have Entered is: %d",n);
getch();
}
Оригинальные комментарии (3)
Восстановлено из Wayback Machine