Wednesday 27 January 2016

menu driven program for the use of structure

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<process.h>
struct student
{
int rno;
char name[20];
int marks;
};
void main()
{
clrscr();
student s[3];
char ans='y';
int ch;
do
{
clrscr();
cout<<"Options are"<<endl;
cout<<"1. Insert records"<<endl;
cout<<"2.Display all records"<<endl;
cout<<"3.Display records on the basis of roll number"<<endl;
cout<<"4.Display records in ascending order of roll number"<<endl;
cout<<"5.Display records in descending order of marks"<<endl;
cout<<"6.Display student with top marks"<<endl;
cout<<"7.Display average marks"<<endl;
cout<<"8.Exit"<<endl;
cout<<"Enter Your Choice"<<endl;
cin>>ch;
switch(ch)
{
case 1:
int i;
for(i=0;i<3;i++)
{
cout<<"Enter the rollno name and marks of a student"<<endl;
cin>>s[i].rno;
gets(s[i].name);
cin>>s[i].marks;
}
break;
case 2:

cout<<"The data of the students is"<<endl;
for(i=0;i<3;i++)
{

cout<<s[i].rno<<"\t";
cout<<s[i].name<<"\t";
cout<<s[i].marks<<endl;
}
break;
case 3:
int rn;
cout<<"Enter roll number to search"<<endl;
cin>>rn;
for(i=0;i<3;i++)
{
   if(s[i].rno==rn)
   {
       cout<<s[i].rno<<"\t";
cout<<s[i].name<<"\t";
cout<<s[i].marks<<endl;
   }
}
break;
case 4:

student S;
for(i=0;i<3;i++)
{
for(int j=0;j<3-i-1;j++)
{
 if(s[j].rno>s[j+1].rno)
 {

S=s[j];
s[j]=s[j+1];
s[j+1]=S;
}
}
}

cout<<"The data of the students in ascending order of roll no is"<<endl;
for(i=0;i<3;i++)
{

cout<<s[i].rno<<"\t";
cout<<s[i].name<<"\t";
cout<<s[i].marks<<endl;
}
break;

case 5:

for(i=0;i<3;i++)
{
for(int j=0;j<3-i-1;j++)
{
 if(s[j].marks<s[j+1].marks)
 {

S=s[j];
s[j]=s[j+1];
s[j+1]=S;
}
}
}

cout<<"The data of the students in descending order of marks is"<<endl;
for(i=0;i<3;i++)
{

cout<<s[i].rno<<"\t";
cout<<s[i].name<<"\t";
cout<<s[i].marks<<endl;
}

break;

case 6:

for(i=0;i<3;i++)
{
for(int j=0;j<3-i-1;j++)
{
 if(s[j].marks<s[j+1].marks)
 {

S=s[j];
s[j]=s[j+1];
s[j+1]=S;
}
}
}

cout<<"The details of the students securing top marks is"<<endl;

cout<<s[0].rno<<"\t";
cout<<s[0].name<<"\t";
cout<<s[0].marks<<endl;


break;
case 7:
int sum=0;

cout<<"The average marks are"<<endl;
for(i=0;i<3;i++)
{

sum=sum+s[i].marks;
}

cout<<sum/3<<endl;
break;


case 8:
cout<<"Press any key to exit.................";
getch();

exit(0);
default:
cout<<"Enter from the given choice "<<endl;
cout<<"Press any key to exit................."<<endl;
getch();
break;

}
cout<<"return to main menu y/n"<<endl;
cin>>ans;
}while((ans=='y')||(ans=='Y'));
getch();
}




This program is made for entry of three students at a time . You can change the number of students by changing the size of the array of structure and the value where ever loops are used
Hope this program will clear a lot of concepts like searching and sorting .


Make new programs always