Implement a class Person with the following data members:
• name (string) - name of this person
• age (integer) – age of this person
Write a program that reads in a list of names and ages and stores them in a one-dimensional array of Person objects. The maximum number of names that will be entered is 100 names. After reading in the list of names and ages, sort the list of people from the youngest (lowest age) to oldest (highest age). Then print out the name and age for each person in the sorted list.
I have the code that I have posted below so far. I am getting one error in my int main() for an Expected unqualified- id. Not sure what exactly that means and how to go about fixing it. Any help, hints, or suggestions would be greatly appreciated.
class Person
{
public:
Person();
void arrInit();
void arrInput();
void arrSort();
void arrOutput();
private:
string name; //name of the person ??
int age; //age of the person ??
static const int arrSize = 100; //how many names
int arrAge[arrSize]; //age of the person - array
string arrName[arrSize]; //name of the person - array
};
Person::Person()
{
name = " ";
age = 0;
};
void Person::arrInput()
{
for (int s=0; s < arrSize; s++)
{
cout << "Enter name (-1 to stop): ";
cin >> arrName[s];
//if (arrName[s] == -1)
//{
//break;
//}
cout << "Enter age of " << arrName[s] << ": ";
cin >> arrAge[s];
} //end for
}
Person is the type name, person is the object name. To call a member function, you need to provide the name of an object.
So,
1 2 3 4 5
int main()
{
Person person;
person.arrInput();
}
hints, or suggestions
Don't use arrays. In this case, use a map<string, age> or a vector of pairs (so you can sort it the way you need), or even a multimap<age, string> so the compiler sorts for you.
Don't forget to uncomment your "-1" check. (and use "-1", not -1 there)
Use member initializers in your constructors
The lowercase version is the particular object belonging to the class Person
Looking at the above comments regarding arrays etc. I'm tempted to say there should be both a class Person and a class People. I get the impression here the class is trying to be both.
Actually, re-reading the description of the assignment, it's looks like these data items don't belong inside class Person.
1 2 3
staticconstint arrSize = 100; //how many names
int arrAge[arrSize]; //age of the person - array
string arrName[arrSize]; //name of the person - array
and in any case it specifically refers to a single one-dimensional array of Person objects.