Create a class called Emp that contains data members such as emp code ,emp name and salary.Geneate menu driven program and perform following task

#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<iomanip.h>
class emp
{
	int ecode,sal;
	char name[20];
	public:
		void insert()
		{
			cout<<"\n Ecode?";
			cin>>ecode;

			cout<<"\n Ename?";
			cin>>name;

			cout<<"\n Salary?";
			cin>>sal;
		}

		void disp()
		{
			cout<<endl<<setw(5)<<ecode<<setw(10)<<name<<setw(10)<<sal;
		}

		char* get_name()
		{
			return name;
		}
};
int main()
{
	int ch,cnt=0;
	emp e[5],temp;
	clrscr();

	while(1)
	{
		cout<<endl<<"1.Insert emp data";
		cout<<endl<<"2.Display emp data";
		cout<<endl<<"3.Sorting emp data";
		cout<<endl<<"4.Exit";
		cout<<endl<<"Enter your choice=>";
		cin>>ch;
		if(ch==4)
		{
			cout<<"\n Bye bye have a nice day";
			break;
		}
		switch(ch)
		{
			case 1:
				if(cnt==5)
				{
					cout<<"\n sorry 5 data is insertd...";
				}
				else
				{
					e[cnt].insert();
					cnt++;
				}
				break;

			case 2:
				for(int i=0;i<cnt;i++)
				{
					e[i].disp();
				}
				break;

			case 3:
				if(cnt==1)
				{
					cout<<"\n Sorting is not possible";
					cout<<"\n Only 1 data is there";
				}
				else
				{
					for(int i=0;i<cnt;i++)
					{
						for(int j=0;j<cnt;j++)
						{
							if(strcmp(e[i].get_name(),e[j].get_name())<0)
							{
								temp=e[i];
								e[i]=e[j];
								e[j]=temp;
							}
						}
					}
					cout<<"\n Sorting is done \n";
					for(int i=0;i<cnt;i++)
					{
					e[i].disp();
					}
				}
				break;

			default:
				cout<<"\n Invalid";
		}
	}
	getch();
	return 0;
}

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *