Sunday, December 27, 2009

Simple highlight menu using array of structure

A simple highlight menu to enter data in an array of structure and the size of the array is dynamically determined by the user also the index of the data to be entered will be determined by the user


#include
#include
#include
#include
#define size 2


struct Employee
{
int flag;
char name[30];
int age;
float salary;
float bonus;
float deduction;
};

void Display(struct Employee emps[]);
void NewEmployee(struct Employee emps[]);

void main(void)
{
struct Employee emps [size];
int pos=0,term=0,i;
char mych;
char menu[3][10]={"New","Display","Exit"};
for (i=0;i {
emps[i].flag=0;
}
do
{
textattr(0x0f);
clrscr();
printf("Menu");
for (i=0;i<3;i++)
{
textattr(0x0f);
if(pos==i)
textattr(0x79);
printf("\n");
cprintf("%s",menu[i]);
}
mych=getch();
switch(mych)
{
case 27://esc
exit(0);
case 9: //tab
pos ++;
if(pos>2)
pos= 0;
break;
case 13: //enter
switch(pos)
{
case 0:
clrscr();
//printf("you pressed new");
NewEmployee(emps);
getch();
break;
case 1:
clrscr();
//printf("you pressed display");
Display(emps);
break;
case 2:
exit(0);
}
break;
case NULL:
mych=getch();
switch(mych)
{
case 72:
pos--;
if(pos<0)
pos=2;
break;
case 80:
pos++;
if(pos>2)
pos=0;
break;
case 71:
pos=0;
break;
case 79:
pos=2;
break;
}
break;
}
}while(term==0);
getch();
}
void NewEmployee(Employee emps[])
{
int index;
printf("Enter index you want: ");
scanf("%d",&index);
if (index >size || index < 0)
{
printf("Enter valid number !!");
}
else if(emps[index-1].flag != 0)
{
printf("Cannot override data!! ") ;
}
else
{
emps[index-1].flag = 1; // Entering data
printf("\nEnter Employee name: ");
scanf("%s",emps[index-1].name);
printf("\nEnter Employee age: ");
scanf("%d",&emps[index-1].age);
printf("\nEnter Employee salary: ");
scanf("%f",&emps[index-1].salary);
printf("\nEnter Employee bonus: ");
scanf("%f",&emps[index-1].bonus);
printf("\nEnter Employee deduction: ");
scanf("%f",&emps[index-1].deduction);
}
}
void Display(Employee emps[])
{
int i;
for(i=0;i {
if(emps[i].flag!=0)
{
printf("\nEmployee name is: %s",emps[i].name);
printf("\nEmployee age is: %d",emps[i].age);
printf("\nEmployee salary is: %f",emps[i].salary);
printf("\nEmployee bonus is: %f",emps[i].bonus);
printf("\nEmployee deduction is: %f",emps[i].deduction);
}
}
getch();
}

No comments:

Post a Comment