Assignment:
2. Write a menu driven program to execute the given class.
A. To search for student by name. If the student does not exist, appropriate message should be given.
B. To display the details of the student having greatest average.
C. To display the details of all the students.
The class student has data members: roll no. & marks of five subjects and member functions are:
• Private member function assign() which assigns streams on the basis of table given below.
Average Marks Stream
96% or more Computer Science
91% - 95% Electronics
86% - 90% Mechanical
81% - 85% Electrical
76% - 80% Chemical
71% - 75% Civil
Otherwise No stream.
• Public member function input() which is used to input details and to call the private function assign().
• Public member function display() to display the contents
My code:
#include <iostream>
#include <string>
using namespace std;
class student
{
char name[50];
int rno;
float marks[5];
char stream[25];
void assign()
{
float average=this->avg();
if(average>70&&average<76) strncpy(stream,"Civil",25);
else if(average>75&&average<81) strncpy(stream,"Chemical",25);
else if(average>80&&average<86) strncpy(stream,"Electrical",25);
else if(average>85&&average<91) strncpy(stream,"Mechanical",25);
else if(average>90&&average<96) strncpy(stream,"Electronics",25);
else if(average>=96) strncpy(stream,"Computer Science",25);
else strncpy(stream,"No stream",25);
}
public:
student() {};
student(student &r)
{
rno=r.rno;
for(int i=0;i<5;i++,marks[i]=r.marks[i]);
strncpy(stream,r.stream,25);
strncpy(name,r.name,50);
}
void input()
{
cout<<"Enter roll no\n";
cin>>rno;
cin.ignore(1,'\n');
cout<<"ENter name\n";
cin.getline(name,50);
cout<<"Enter marks in five subjects\n";
for(int i=0;i<5;i++,cin>>marks[i]);
assign();
}
void display()
{
cout<<"\nROll no: "<<rno<<endl;
cout<<"Name: "<<name<<endl;
cout<<"Average : "<<student::avg()<<" %"<<endl;
cout<<"Stream: "<<stream<<endl;
}
float avg()
{
float average=0;
for(int i=0;i<5;i++,average+=marks[i]);
average/=5;
cout<<"FUNCTION AVERAGE(): AVG= "<<average<<endl; //Diagnostic message
return (average);
}
void search(student *,int n);
};
void student::search(student *a,int n)
{
int i=0;
int found=0;
char nm[50];
cin.ignore(1,'\n');
cout<<"ENter name to search\n";
cin.getline(nm,50);
while(i<n&&(!found))
{
if(strcmp(nm,a[i].name)==0)
{
found=1;
}
i++;
}
if(found) a[i].display();
else cout<<"NOT FOUND\n\n";
}
void highavg(student *stud,int n)
{
cout<<n<<endl;
float max=stud[0].avg();
int p=0;
for(int i=1;i<n;i++)
{
if(max<stud[i].avg())
{
max=stud[i].avg();
p=i;
}
}
cout<<"\nRESULT: \n";
stud[p].display();
}
int main()
{
int n;
student *stud;
cout<<"How many students\n";
cin>>n;
stud=new student[n];
for(int i=0;i<n;i++)
{
stud[i].input();
}
int option;
do {
cout<<"\n1.Search for student\n2.Search for highest average\n3.Display all\nExit with other option\n";
cin>>option;
switch(option)
{
case 1:
stud[0].search(stud,n);
break;
case 2:
highavg(stud,n);
break;
case 3:
for(int i=0;i<n;i++)
{
stud[i].display();
}
cout<<"* * * * * * * * * *\n";
break;
default: break;
}
}
while(option>=1&&option<=3);
return 0;
}
My program gives wierd results.
With option: 1. It says "not found" for any string I input.
Option 2. It gives right result but prints wierd average( eg. 5.567e+028 instead of 95% or something....)
Option 3.Again prints wrong averages...(same as in option 2).
I've lost hope. I thought i was good at this but I dont know anymore.
EDIT: found solution for first issue.