Input validation in turbo c is one of the problem by many programmers. If the input is not properly filtered by the program, it can ruined the entire program. Here is my sample program.
#include <math.h>
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <stdlib.h>
void main_menu(void);
void sub_menu(void);
void area_of_circle(void);
void area_of_triangle(void);
void area_of_square(void);
void input_validation(char tempo[6], int mem_pt);
void save_pt(int memo);
void save_pt(int memo)
{
switch (memo)
{
case 1: area_of_circle();
case 2: area_of_triangle();
case 3: area_of_square();
}
}
void input_validation(char tempo[6], int mem_pt)
{
int deci_ptr=0,i=0;
while (tempo[i]!=’\n’)
{
if (tempo[i]==’.'&&deci_ptr==1)
{
printf(”\nToo much decimal point!!!”);
getch();
save_pt(mem_pt);
}
if (tempo[i]<’0′||tempo[i]>’9′)
{
printf(”\nPlease enter a number!!!”);
getch();
save_pt(mem_pt);
}
if (strlen(tempo)==1&&tempo[i]==’.')
{
printf(”\nPlease enter a number!!!”);
getch();
save_pt(mem_pt);
}
if (tempo[i]==’.') deci_ptr=1;
i++;
}
}
void main_menu(void)
{
char c;
clrscr();
printf(”Main Menu:\n\n”);
printf(”[a] Area of Circle\n”);
printf(”[b] Area of Triangle\n”);
printf(”[c] Area of Square\n”);
printf(”Select: “);
c=getche();
switch (c)
{
case ‘a’: area_of_circle();
case ‘A’: area_of_circle();
case ‘b’: area_of_triangle();
case ‘B’: area_of_triangle();
case ‘c’: area_of_square();
case ‘C’: area_of_square();
default: printf(”\nPlease select from the menu!”);getch();main_menu();
}
}
void sub_menu(void)
{
char c;
printf(”\n\nGoto Main Menu: [Y/N]: “);
c=getche();
switch (c)
{
case ‘y’: main_menu();
case ‘Y’: main_menu();
case ‘n’: printf(”\nGood bye!!!”);getch();exit(0);
case ‘N’: printf(”\nGood bye!!!”);getch();exit(0);
default: printf(”\nError!!!”);getch();sub_menu();
}
}
void area_of_circle(void)
{
char number[6];
float area,radius;
clrscr();
printf(”Area of Circle:\n\n”);
printf(”Enter the radius of circle: “);
fgets(number,6,stdin);
input_validation(number,1);
radius=atof(number);
printf(”\nThe area of the circle is %f”,radius*radius*M_PI);
getch();
sub_menu();
}
void area_of_triangle(void)
{
char number[6];
float area,base,height;
clrscr();
printf(”Area of Triangle:\n\n”);
printf(”Enter base: “);
fgets(number,6,stdin);
input_validation(number,2);
base=atof(number);
printf(”\nEnter height: “);
fgets(number,6,stdin);
input_validation(number,2);
height=atof(number);
printf(”\nThe area of the triangle is %f”,base*height/2);
getch();
sub_menu();
}
void area_of_square(void)
{
char number[6];
float area,height;
clrscr();
printf(”Area of the Square:\n\n”);
printf(”Enter the height: “);
fgets(number,6,stdin);
input_validation(number,3);
height=atof(number);
printf(”\nThe area of the square is %f”,height*height);
getch();
sub_menu();
}
void main(void)
{
main_menu();
}
Happy programming. 