Sos !!!

Keane 16

Skilled
#include <conio.h>

#include <iostream.h>

#include <stdio.h>

#include <string.h>

#include <ctype.h>

class student

{

char name[30];

float marks[3];

public:

float total(void);

void getdata(void);

void showdata(void);

};

student akshit[5];

void main()

{

clrscr();

for(int i=0; i<3; i++)

{

akshit.getdata();

akshit.showdata();

}

float total();

getch();

}

void student :: getdata(void)

{

for(int i=0; i<3; i++)

{

cout<<"Enter student's name - ";

gets(akshit.name);

for(int j=0; j<3; j++)

{

cout<<endl<<"Enter Marks in subject "<<j+1<<" - ";

cin>>akshit.marks[j];

}

}

}

void student :: showdata(void)

{

for(int i=0; i<3; i++)

{

cout<<endl<<endl<<"Name of student "<<akshit.name;

for(int j=0; j<3; j++)

cout<<endl<<"Marks - "<<akshit.marks[j]<<endl;

}

}

float student :: total(void)

{

float s=0;

for(int i=0; i<3; i++)

{

for(int j=0; j<3; j++)

s+=akshit.marks[j];

}

return (s);

}



Just want it to run for 3 students for marks in 3 subjects ...

Problem is that I've got a for loop in main() as well as in the function ... so.... how do i remove one ?(Tried removing as it is but then I need to have something for "X" --- akshit.[X].name ...

PS: Final exam tomorrow :p ... So RELPPP !!!
 
hi,
u need to remove the loop in the student:: functions.
as they operate on the objects from which they are called.

u can use the following for functions
Code:
void student :: getdata(void)
{
	cout<<"Enter student's name - ";
	gets(name);
	for(int j=0; j<3; j++)
	{
		cout<<endl<<"Enter Marks in subject "<<j+1<<" - ";
		cin>>marks[j];
	}
}

void student :: showdata(void)
{
	cout<<endl<<endl<<"Name of student "<<name;
	for(int j=0; j<3; j++)
		cout<<endl<<"Marks - "<<marks[j]<<endl;
}

regarding the total() part u cannot call a class function directly as u have.
you could either use
Code:
student::total();
or use it as a friend function and call directly.

hope this helps.
correct me if i am wrong somwhere.
 
you must have removed the loop in the main function as well i think.
i checked with this. its working.
just the output is sometimes not seen as it is not being flushed.
dont remember the windows counterpart of fflush();

Code:
#include <conio.h>
#include <iostream.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>

class student
{
char name[30];
float marks[3];
public:
friend float total(void);
void getdata(void);
void showdata(void);
};

student akshit[5];

void main()
{
for(int i=0; i<3; i++)
{
	akshit[i].getdata();
	akshit[i].showdata();
}
total();
getch();
}

void student :: getdata(void)
{
	cout<<"Enter student's name - ";
	gets(name);
	for(int j=0; j<3; j++)
	{
		cout<<endl<<"Enter Marks in subject "<<j+1<<" - ";
		cin>>marks[j];
	}
}

void student :: showdata(void)
{
	cout<<endl<<endl<<"Name of student "<<name;
	for(int j=0; j<3; j++)
		cout<<endl<<"Marks - "<<marks[j]<<endl;
}

float total(void)
{
	float s=0;
	for(int i=0; i<3; i++)
	{
		for(int j=0; j<3; j++)
			s+=akshit[i].marks[j];
	}
	cout << s;
	return (s);
}

also notice that i have changed the total() function to a friend of the class.
as an alternatvive to friend function u can use
Code:
 student::total();
but u need to make that function as
Code:
 static float total(void);
 
if u dont know what "friend" means then you can try the following.

As you have declared the class variables as private, you need class functions to read them.
You can write the student::total() such that it returns the sum of the marks of the object which called it. and use the return values to calculate the grand total in the main() function.
Code:
float student::total(void)
{
	float s=0;
	for(int j=0; j<3; j++)
			s+=marks[j];
	return (s);
}
in the main function
instead of calling total() use this
Code:
float grandTotal = akshit[0].total()+akshit[1].total()+akshit[2].total();
cout<<grandTotal;
 
Why don't you try what pr0ing has posted. It looks fine. Invoke total similar to how you invoke the other two methods. You need to go through the class instance.
 
Back
Top