Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Soulstorm

macrumors 68000
Original poster
Feb 1, 2005
1,887
1
The exercise is as follows:

Make a program with classes that will have 1 class with 2 private parts: the name of the university and the number of the students. The public part will be void show(), which shows all private parts, and a function called getspoudastes() (getstudents, it is just in greek) which will return the number of the students in the object. The object will have a constructor with no arguments which will be used to give each created object the values of the private variables.

The class will be called TEI (it is a type of university here).

In the main(), make an array of objects of class TEI, a[5]. Then:

1)Make the program show each object's name and students.
2)Show the universities with the greatest number of students. If there's more than one, show them all.

Here is my code:
Code:
#include <iostream>
#include <cstring>


using namespace std;


class tei{
	char name[20];
	int numspoudastes;
public:
	tei();
	void show();
	int getspoudastes();
};

tei::tei(){
	cout << "Give name of the University ";
	gets(name);
	cout << "Enter Student Number: ";
	cin >> numspoudastes;
}

void tei::show(){
	cout << "Edra: " << name << "\nSpoudastes: " << numspoudastes << "\n";
}

int tei::getspoudastes(){
	return numspoudastes;
}

int main(){
	int i;
	int maxspoudastes = -1;
	tei a[5];
	for(i=0; i<5; i++){
		a[i].show();
		if(a[i].getspoudastes()>=maxspoudastes)
			maxspoudastes=a[i].getspoudastes();
	}
	cout << "MAX: \n";
	for(i=0; i<5; i++){
		if(a[i].getspoudastes()==maxspoudastes)
			a[i].show();
	}
	
	return 0;
}
When the program runs, it only asks the name of the first object in the array. Then it doesn't ask the name, instead it prompts me to give a name, but then it skips to the prompt where it asks for a number, and then asks for the number.

Why does this happen?
 

cemorris

macrumors regular
Oct 13, 2004
138
0
The objects are being created all at the same time and trying to use the STD display all at once. Try declaring the array as pointers and doing an explicit new inside your for loop.
 

Soulstorm

macrumors 68000
Original poster
Feb 1, 2005
1,887
1
cemorris said:
The objects are being created all at the same time and trying to use the STD display all at once. Try declaring the array as pointers and doing an explicit new inside your for loop.
I am relatively a newbie... can you tell me how do I do that, if that's not too much to ask? :eek:
 

cemorris

macrumors regular
Oct 13, 2004
138
0
So when you declare your tei array it should be a pointer array instead. Like this

tei* a[5];

Then in your for loop, you would need to exlpicitly instantiate each instance before using it. So the first thing in your for loop should be.

a = new tei();

And finally since you are accessing the pointer rather than a class variable, you need to derefence it in order to call all the functions on the class. You do this by replacing all your a.* calls to a->*.

I hope this makes sense. I would also highly recomend reading by going to http://www.cplusplus.com/doc/tutorial/ and reading up on the two classes section. In there you will find topics on pointers to classes
 

Soulstorm

macrumors 68000
Original poster
Feb 1, 2005
1,887
1
cemorris said:
So when you declare your tei array it should be a pointer array instead. Like this

tei* a[5];

Then in your for loop, you would need to exlpicitly instantiate each instance before using it. So the first thing in your for loop should be.

a = new tei();

And finally since you are accessing the pointer rather than a class variable, you need to derefence it in order to call all the functions on the class. You do this by replacing all your a.* calls to a->*.

I hope this makes sense. I would also highly recomend reading by going to http://www.cplusplus.com/doc/tutorial/ and reading up on the two classes section. In there you will find topics on pointers to classes

converted the code to
Code:
#include <iostream>
#include <cstring>


using namespace std;


class tei{
	char name[20];
	int numspoudastes;
public:
	tei();
	void show();
	int getspoudastes();
};

tei::tei(){
	cout << "Give name of the University ";
	cin.getline(name,20);
	cout << "Enter Student Number: ";
	cin >> numspoudastes;
}

void tei::show(){
	cout << "Edra: " << name << "\nSpoudastes: " << numspoudastes << "\n";
}

int tei::getspoudastes(){
	return numspoudastes;
}

int main(){
	int i;
	int maxspoudastes = -1;
	tei* a[5];
	for(i=0; i<5; i++){
		a[i]=new tei();
		a[i]->show();
		if(a[i]->getspoudastes()>=maxspoudastes)
			maxspoudastes=a[i]->getspoudastes();
	}
	cout << "MAX: \n";
	for(i=0; i<5; i++){
		a[i]=new tei();
		if(a[i]->getspoudastes()==maxspoudastes)
			a[i]->show();
	}
	
	return 0;
}

...but similar things happen. Maybe the constructors are not intented to be used that way. Or maybe I am a total newbie... :)
 

cemorris

macrumors regular
Oct 13, 2004
138
0
Ok dont use the gets or getline function. Simply use the cin instead.
i.e. cin >> name;

Try that instead, and let me know. When I get home tonight i can try to compile and see what the problem is.

Upon further thought, the first version should work, just try the cin>>name instead.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.