Advertisement
ASP_Volume3 Validation/ Processing #66530

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

Resumen de IA: 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.

Código fuente
original-source
#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();
}
Comentarios originales (3)
Recuperado de Wayback Machine