0

i need help to make this program work
i created a class and made the setter and getter and it compiles
now i made search by name and id but it doesn't work and i need to
find max average for 10 students and print information of the student with max avg
i tried but it doesn't work and the usuall problem i can't call any function in the main
(don't know how) any help or ideas are wellcomed
thanks in advance

#include<iostream.h>
#include<conio.h>
#include<string.h>
class student{
private:
int Id;
int Age;
float Avg;

char *Name;
int Level;
public:
student();
~student();

void setName(char*);
void setId(int);
void setAvg(float);
void setAge(int);
void setLevel(int);
int max(float,float,float,float,float,float,float,float,float,float);
char getName();
int getId();
int getAvg();
int getAge();
int getLevel();

void search_By_Id();
//void searchbyname();

void enterdata(char*,int&,int&,int&);
void show();
};
//**************************
student::student(){
Age=0;
Id=0;
Avg=0.0;
Name[0]='a';

Level=0;
}

student::~student(){
}
void student::setId(int a){
Id=a;
}

void student::setLevel(int y){
Level=y;
}

void student::setName(char* x){
Name=x;
}


void student::setAvg(float c){
Avg=c;
}


void student::setAge(int d){
Age=d;
}

char student::getName(){
return *Name;
}


int student::getAvg(){
return Avg;
}


int student::getAge(){
return Age;
}


int student::getId(){
return Id;
}
int student::getLevel(){
return Level;
}
/*void student::display(){
cout<<"test";
}
*/
void student::enterdata(char*,int&,int&,int&){

cout<<"enter name :"<<endl;
cin>>Name;

cout<<"enter Id :"<<endl;
cin>>Id;
cout<<"enter age :"<<endl;
cin>>Age;

cout<<"enter level:"<<endl;

cin>>Level;

}
void student::show()
{
   int i;
   for(i=0;i<=10;i++){
   //cout<<"name is"<<Name<<endl;
   cout<<"age is"<<Age<<endl;
   cout<<"id is "<<Id<<endl;
   cout<<"avg is"<<Avg<<endl;
   cout<<"level is"<<Level<<endl;

   }
}

void student::search_By_Id()
{

	int id;

	cout<<"Enter studentId to be searched  : ";
	cin>>id;

	if((Id==id)==0)
	{
		clrscr();
		cout<<"No student wuth this id";

	} else{
	       cout<<getId();
	      cout<<getName();
	       cout<<getAge();
	       cout<<getLevel();
	       cout<<getAvg();

		cout<<endl;
	       }
}

/*
void student::searchByName()
{


	charname[11][14];

	cout<<"Enter  Name to be searched  : ");
	cout>>name
		if( strcmp(studentcharName[11][14]==name[10][20])
		{

		  cout<<getId();
		  //cout<<getName();
		  cout<<getAge();
		  cout<<getLevel();
		  cout<<getAvg();
		  cout<<endl;

		}


       elseif(strcmp(Name[11][14],name[10][20])==0)

	{
		clrscr();
		cout"No name ";

	}
}
*/

int student::max(float Avg1,float Avg2,float Avg3,float Avg4,float Avg5,float Avg6,float Avg7,float Avg8,float Avg9,float Avg10){
   int max=Avg1;
   
   if(Avg2>max)
   max=Avg2;
   
   else if(Avg3>max)
   max=Avg3;

   else if(Avg4>max)
   max=Avg4;

   else if(Avg5>max)
   max=Avg5;

   else if(Avg6>max)
   max=Avg6;

   else if(Avg7>max)
   max=Avg7;

   else if(Avg8>max)
   max=Avg8;

   else if(Avg9>max)
   max=Avg9;

   else if(Avg10>max)
   max=Avg10;
   
   return max;
}



//*********************
int main(){

student obj;

float Avg;

int i;

int Avg1,Avg2,Avg3,Avg4,Avg5,Avg6,Avg7,Avg8,Avg9,Avg10;
clrscr();
for(i=0;i<=10;i++){
   cout<<"please enter avg:"<<endl;
    cin>>Avg;

}


return 0;

}
2
Contributors
1
Reply
2
Views
10 Years
Discussion Span
Last Post by Ancient Dragon
0

>>i can't call any function in the main (don't know how)
almost identical to calling any normal function except you have to reference the name of the object. For example if you want to call search_By_Id() then do it like this

obj.search_By_Id();

setName() is wrong. since Name has been declared as a pointer you need to allocate memory for it. But if you declare Name as std::string you don't have to worry about that. If you want to keep it as char* then code that function like this:

void student::setName(char* x){
       Name= new char[strlen(x)+1];
       strcpy(Name, x);
}

getName() is also coded wrong. It should return a const pointer because its value should not be changed by the calling function. and coded as below.

const char* student::getName(){
    return Name;
}
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.