Help with classes, arrays, and sorting

Hey guys,
I am really struggling to understand how to use a array to store a value from a class and then sort it. I have only used classes once and pretty much got hand held through the whole thing, so I have a limited understanding of it. This is my problem, and below it is what I have so far (it's probably wrong).

Thanks,
Tucker

2. Sorting Youngest to Oldest

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.

Sample Output (user input is bold) :

Enter name (-1 to stop): Bart
Enter age of Bart: 10
Enter name (-1 to stop): Lisa
Enter age of Lisa: 8
Enter name (-1 to stop): Maggie
Enter age of Maggie: 1
Enter name (-1 to stop): Homer
Enter age of Homer: 36
Enter name (-1 to stop): Marge
Enter age of Marge: 34
Enter name (-1 to stop): -1

Name: Maggie, age: 1
Name: Lisa, age: 8
Name: Bart, age: 10
Name: Marge, age: 34
Name: Homer, age: 36

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <iostream>
#include <string>
using namespace std;

class Person
{
private:
     // Person private data members
        
     string name;
     int age;        

public:

Person(string fName, int years)
{
     name = fName
     
     if (years <0)
     {
          years = 0;
     }
     
     age = years;
}
     
int main()
{
    const int ARRAY_MAX = 100;
    int name = 0;
    int age = 0;
    int people[ARRAY_MAX];
    
    for (int i = 0, int i <100, i++)
    {
        while (Person.name != -1)
        {
             cout << "Enter name (-1 to stop): ";
             cin >> name;
             cout << "Enter age: ";
             cin >>age;
          

Hey guys, I have an update with a more specific question. Here's my code first:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
class Person
{
private:
     // Person private data members
        
     string name;
     int age;        

public:
     Person();
     Person(string, int);
     string getName();
     void setName();
     int getAge();
     void setAge();
};
     Person::Person()
     {
          name = " ";
          age = 0;
     }
     

     Person::Person(string fName, int years)
     {
          name = fName;
          age = years;
     
     
     }


string Person::getName()
{
    return name;
}

int Person::getAge()
{
    return age;
}

void Person::setName(string name)
{
     name = fName;
}       

void Person::setAge(int age)
{
     age = years;
}

void Person::displayPerson()
{
     cout << "
}

}
void sort(person[])
{
     
}
     
int main()
{
    const int ARRAY_MAX = 100;
    int person[ARRAY_MAX];
    string name = " ";
    
    for (int i = 0; i <100; i++)
    {
        Person person;

        cout << "Enter name (-1 to stop): ";
        cin >> name;
        person.setName()
        while (name =! -1)
        {
             cout << "Enter age: ";
             cin >>age;
        }

    }
} 


My questions are:
1. Is there a better way to do my Person class without all of the double colons?
2. I have no idea how to do the array at the bottom. I know that's not a question, I just need help, any kind of hint would be perfect.
2.1. I also don't know how to make the person object increase by one with the for loop.

EDIT: The underlined code is stuff I don't understand but my TA told me to type. If anyone can explain why I need that I would really appreciate it.
Last edited on
Here, analyze

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;

class Person
{
    public:
    char szFirstName[128];
    char szLastName[128];
    int nSocialSecurityNumber;
    char dob[10];
};

Person getPerson()
{
    Person person;

    cout << "\nEnter another Person\n"
    << "First name: ";
    cin >> person.szFirstName;

    cout << "Last Name: ";
    cin >> person.szLastName;

    cout << "Social Security Number: ";
    cin >> person.nSocialSecurityNumber;

    cout << "Date of birth: ";
    cin >> person.dob;

    return person;
}

int getPeople(Person people[], int nMaxSize)
{
    int index;
    for(index = 0; index < nMaxSize; index++)
    {
        char cAnswer;
        cout << "Enter another name? (Y or N): ";
        cin >> cAnswer;

        if(cAnswer != 'Y' && cAnswer != 'y')
        {
            break;
        }

        people[index] = getPerson();
    }
    return index;
}

void displayPerson(Person person)
{
    cout << "First name: " << person.szFirstName << endl;
    cout << "Last name: " << person.szLastName << endl;
    cout << "Social Security number : " << person.nSocialSecurityNumber << endl;
    cout << "Date of Birth: " << person.dob << endl;
}

void displayPeople(Person people[], int nCount)
{
    for(int index = 0;index < nCount; index++)
    {
        displayPerson(people[index]);
    }
}

void sortPeople(Person people[], int nCount)
{
    int nSwaps = 1;
    while(nSwaps != 0)
    {
        nSwaps = 0;

        for(int n = 0; n < (nCount - 1); n++)
        {
            if(people[n].nSocialSecurityNumber > people[n+1].nSocialSecurityNumber)
            {
                Person temp = people[n+1];
                people[n+1] = people [n];
                people[n] = temp;

                nSwaps++;
            }
        }
    }
}

int main(int nNumberofArgs,char* pszArgs[])
{
    Person people[128];

    cout << "This program stores names, SSN, and DOB\n";
    int nCount = getPeople(people, 128);

    sortPeople(people, nCount);

    cout << "\nHere is the list sorted by "
    << "social security number" << endl;
    displayPeople(people,nCount);

    system("PAUSE");
    return 0;
}
1. How exactly do you mean,
without all of the double colons
?
The scope resolution operator(::) is necessary for your member function definitions.

1.1. Unless your instructor insists upon it, I would recommend removing all get/set functions. They are redundant, and there's really no reason for using them. Ever.

1.2. I don't think this was intentional, but your displayPerson() function is incomplete. Probably due to a copy-paste accident.

2. What exactly do you need help with? What does the exercise ask for?
From what it looks like, the user is asked to give a name and age for up to 100 people, but they can choose to quit providing information at any time. Is this correct?

2.1. I think you might be confused. Elaborate?
Topic archived. No new replies allowed.