Create a class employee with employee no, name and salary. Write a menu driven CPP program (i) to create an employee database (ii) to display the content of the database and (iii) to sort the database.

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<process.h>
#include<iomanip.h>
struct Employee
{
   int eno;
   char name[30];
   float salary;
};
void input(Employee E[10], int n)
{
   for(int i=0;i<n;i++)
   {
     cout<<"\nEnter Employee Number : ";
     cin>>E[i].eno;
     cout<<"\n           Enter Name : ";
     gets(E[i].name);
     cout<<"\n         Enter Salary : ";
     cin>>E[i].salary;
   }
}
void show(Employee E[10], int n)
{
     cout<<endl<<"               The Employee Details"<<endl;
     cout<<endl<<"--------------------------------------------------";
     cout<<endl<<"Emp.No    Name of Employee              Salary";
     cout<<endl<<"--------------------------------------------------";
     for(int i=0;i<n;i++)
     {
 cout.setf(ios::left, ios::adjustfield);
 cout<<endl<<setw(10)<<E[i].eno;
 cout<<setw(30)<<E[i].name;
 cout<<setw(10)<<E[i].salary;
     }
     cout<<endl<<"==================================================";
}
void main()
{
     int n, ch, i, j; Employee E[30];
     char choice;
     do
     {
 clrscr();
 cout<<"\n-----MENU-----\n";
 cout<<"\n1. Enter details of employee";
 cout<<"\n2. Sort in descending order of salary";
 cout<<"\n3. Display the details";
 cout<<"\n4. Exit from program";
 cout<<"\n\nEnter your choice : ";
 cin>>ch;
 switch(ch)
 {
   case 1: cout<<"\nEnter the number of Eemployees : ";
    cin>>n;
    input(E,n);
    cout<<"\nThe database is created now";
    cout<<"\n==========================="<<endl;
    break;
   case 2: Employee temp;
    for(i=0;i<n-1;i++)
    {
       for(j=0;j<n-1-i;j++)
       {
   if(E[j].salary<E[j+1].salary)
   {
     temp = E[j];
     E[j] = E[j+1];
     E[j+1] = temp;
   }
       }
    }
    cout<<"\nThe database is sorted now";
    cout<<"\n=========================="<<endl;
    break;
   case 3: show(E,n);
    break;
   case 4: exit(0);
   default:
    cout<<endl<<"Wrong Choice"<<endl;
 }
 cout<<"\n\nDo you want to continue (Y/y) : ";
 cin>>choice;
     }while( choice=='Y'|| choice=='y');
}

No comments:

Post a Comment