VB icon

input validation for integer

Email
Submitted on: 1/10/2015 5:04:00 AM
By: Dharamraj Panwar (from psc cd)  
Level: Advanced
User Rating: By 2 Users
Compatibility: C, C++ (general)
Views: 2421
 
     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 .

 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
				
//**************************************
// Name: input validation for integer
// Description: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 .
// By: Dharamraj Panwar (from psc cd)
//**************************************

#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();
}


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Advanced category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.