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:
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?
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;
}
Why does this happen?