Creating objects of a class with a for loop, then an array containing values from one of the class members.

To give a brief background, here's the assignment I'm working on:
1.Prompt user “How many people are we initializing?”
2.For that amount, using a for loop, make and define that many people with at least 2 member variables of your choice.
3.After each defined person, output some of their data using a member function of the Person class
4.Using a member variables of your choice, create an array consisting of any of those values and sort them from greatest to least.
5. If 2 variables are the same, let the user know.

I'm sure there's something wrong with the method I used to create the objects and the loop for displaying the name of the people with equal age but I can't really see it. Hopefully one of you can.

Here's the code I'm working with
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
108
109
110
111
112
113
114
115
116
117
118
119
#include <iostream>
#include <string>
using namespace std;

class Person {
private:
     string name,eye_color;
     int age, weight;
     bool likes_cuny;

public:
    Person();  // Default CTOR
    //Initializes string name taking a value from user
    void Display();
    // Precondition : name is assigned through constructor, age, weight and eye color are assigned through
    //set values function
    // Postcondition : Will print Person's information
    string getName();
    //Precondition: takes name assigned through constructor.
    //Postcondition: returns name (string) to user.
    int getAge();
    //Precondition: takes in value assigned to priv member 'age'
    //PostCondition: returns that value to user.
    int getWeight();
    //Precondition: takes in value assigned to priv member 'weight'
    //PostCondition: returns that value to user.
    string getColor();
    //Precondition: takes in value assigned to priv member 'eye_color'
    //PostCondition: returns that value to user.
    void setValues();
    //Precondition: Reads in age, weight and eyecolor from user.
    //PostCondition: Function allocates values to priv data age, weight and eyecolor.
};
void swap(int &a, int& b);
//Precondition: function takes two variables and their values
//PostCondition: swaps the values of the variables
void mySort (int A[], int s);
//Precondition: function takes an array and its size.
//PostCondition: starting at the first index, it compares the values stored in those indexes
// and if the current value is higher than the starting index, it will swap their value
// Order of the array will be in descending order.

int main()
{
int amount;
cout << "How many people are we initializing? Min 2 ";
cin >> amount;
int AgeArr[amount]; //Array of size = to amount, will be used later to store the different ages.

for (int i =1; i<=amount;i++){ //loop that defines objects and sets their values/displays
	Person personi;
	personi.setValues();
	personi.Display();
for (int j = 0; j<amount; j++){ //loop that attempts to assign value to index of ageArr by using getAgefunction
 AgeArr[j] = personi.getAge();
 if (AgeArr[j] < AgeArr[j+1]) //Helps sort the array in descending order
 mySort (AgeArr,amount);
 // in case there are equal values it will let the user know. Here's the problem! 
 //it repeats the name of the same person twice. Guessing it's due to post increment, but i can't really see it.
 if (AgeArr[j] == AgeArr[j+1])
 cout << personi.getName() << " & " << personi.getName << " have the same age: " << AgeArr[j+1];
}
}



return 0;
}
//Defining the constructor
Person::Person() {
cout << "Enter name of the person: ";
cin >> this->name;
}
//Display function definition
void Person::Display(){	
cout << name << " is  " << age << " years old and weights " << weight << " lbs." 
<< " His/Her eyes are color " << eye_color << "." <<endl;
}
//getAge function definition
int Person::getAge(){
return age;
}
//getWeight fucntion definition
int Person::getWeight(){
return weight;
}
//getColor Function Definition
string Person::getColor(){
return eye_color;
}
//getName function definition
string Person::getName(){
	return name;
}
//setValues function definition 
void Person::setValues(){
	cout<<"Enter " << name << "'s age: ";
    cin >> age;
	cout<<"Enter " << name << "'s weight: ";
    cin >> weight;
	cout << "Enter " << name << "'s eye color: ";
	cin >> eye_color; 
}
//swap function definition
void swap(int &a, int &b){
	int temp = a;
	a = b;
	b = temp;
}
//sort fucntion definition
void mySort (int A[], int s){
	for (int i = 0; i < s-1; i ++){
		for (int j = 0; j< s-i-1; j++)
		if (A[i] < A[i+1]){
			swap(A[i], A[i+1]);
		}
	}
	
}
Hi!

I can see two issues so far:
1
2
3
4
5
6
7
8
int main()
{
    int amount;
    cout << "How many people are we initializing? Min 2 ";
    cin >> amount;
    int AgeArr[amount]; //HERE

    //[...] 


In C++ you can't initialise arrays with variables, you either need to use a constant or dynamically allocate it.
I think the latter is the one you want to use:
1
2
3
4
5
6
7
8
int main()
{
    int amount;
    cout << "How many people are we initializing? Min 2 ";
    cin >> amount;
    int *agrArr = new int[amount];

    //[...] 



In your for loop you actually only define a person object once, you define the same object over and over again.
That results you in overwriting the data for each iteration of the loop:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
for (int i =1; i<=amount;i++)  //loop that defines objects and sets their values/displays
{
    Person personi;  //HERE
    personi.setValues();
    personi.Display();
  
    for (int j = 0; j<amount; j++)   //loop that attempts to assign value to index of 
    {
         ageArr by using getAgefunction
         AgeArr[j] = personi.getAge();

         if (AgeArr[j] < AgeArr[j+1])   //Helps sort the array in descending order
         mySort (AgeArr,amount);

 // in case there are equal values it will let the user know. Here's the problem! 
 //it repeats the name of the same person twice. Guessing it's due to post increment, but i can't really see it.
 
        if (AgeArr[j] == AgeArr[j+1])
        cout << personi.getName() << " & " << personi.getName << " have the same age: " << AgeArr[j+1];
      }
}


You should maybe try something in the same fashion of the ageArr array so that you access a
different object's data on each iteration of the for loop:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
Person *person = new Person[amount];

for (int i =1; i<=amount;i++)  //loop that defines objects and sets their values/displays
{
    person[i].setValues();
    person[i].Display();
  
    for (int j = 0; j<amount; j++)   //loop that attempts to assign value to index of 
    {
         ageArr by using getAgefunction
         AgeArr[j] = person[i].getAge();

         if (AgeArr[j] < AgeArr[j+1])   //Helps sort the array in descending order
         mySort (AgeArr,amount);

 // in case there are equal values it will let the user know. Here's the problem! 
 //it repeats the name of the same person twice. Guessing it's due to post increment, but i can't really see it.
 
        if (AgeArr[j] == AgeArr[j+1])
        cout << person[i].getName() << " & " << person[i].getName << " have the same age: " << AgeArr[j+1];
      }
}


I hope this is clear enough for you and that it helps you in any way.

Regards,

Hugo.
Last edited on
Thanks Hugo! Your comment helped me fix the problem and the program runs smooth!
Topic archived. No new replies allowed.