PDA

View Full Version : What do i do wrong in this code?




Soulstorm
Dec 7, 2005, 08:16 AM
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:
#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
Dec 7, 2005, 08:29 AM
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
Dec 7, 2005, 10:09 AM
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? :o

cemorris
Dec 7, 2005, 11:14 AM
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[i] = 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[i].* calls to a[i]->*.

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
Dec 7, 2005, 12:14 PM
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[i] = 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[i].* calls to a[i]->*.

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
#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
Dec 7, 2005, 04:58 PM
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.

SamMiller0
Dec 8, 2005, 12:39 PM
Don't use c-strings, use the std::string object provided by the STL.