trouble with arrays of objects

Hello,

I'm having quite a bit of trouble on this program. I have quite a few logic errors that I'm not quite able to iron out.

The program should be able to:
Collect 10 names and ages using an array of objects.
Calculate the average of the ages.
Print the names and ages as well as the person's difference from the average age.

The problem is when trying to input it will keep outputting the prompts after the first try without giving the user a chance to enter in more information.

Also when printing, it keeps outputting garbage instead of what was input.

I know it's a lot but i could really use some help, im sort of having a brain block as for what to do.


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
 #include <iostream>
using namespace std;

class Data{

private:
	int age;
	int totalAge;
	char name[31];

public:
	void storeData();
	void printData();
};

void Data::storeData(){

	cout << "Please enter a name (Max: 30 characters)." << endl;
	cin.getline(name, 30);

	cout << "Please enter an age." << endl;
	cin >> age;

	totalAge = totalAge + age;

}

void Data::printData(){

	cout << "Name: " << name[31];
	cout << " Age is: " << age;
	totalAge = totalAge / 10; //calculates average and then finds difference
	age = totalAge - age;
	cout << " Age difference = " << age << " years" << endl;

}

void main(){

	Data get[10];
	for (int i = 0; i < 10; i++){

		get[i].storeData();
	}

	Data print[10];
	for(int i = 0; i < 10; i++){

		print[i].printData();
	}
}

You are assuming totalAge is object wide, i.e. shared across all objects but its not. Each object of Data has its own totalAge.

Line 46 doesn't need to be there, to print the data you should be using get like you did to input it.
1
2
3
	for (int i = 0; i < 10; i++)
		get[i].printData();
Last edited on

If you are wanting to calculate total ages, create a "getter" function in your class such as:

int getAge() { return age; }

and your main could look like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
void main()
{
	Data get[10];
	int totalAge = 0;

	for (int i = 0; i < 10; i++)
		get[i].storeData();

	for (int i = 0; i < 10; i++) {
		get[i].printData();
		totalAge += get[i].getAge();
	}
	cout << "Total Age: " << totalAge;
}
closed account (j3Rz8vqX)
Problem:
it keeps outputting garbage instead of what was input
totalAge = totalAge + age;//Total age may be garbage

Solution:
1
2
3
4
5
6
7
8
class Data{
public:
    //Constructor
    Data()
    {
        totalAge = 0;//Set totalAge to zero during construction
    }
};

totalAge in the class shouldn't be there or even being used - totalAge can only be seen by its own object as detailed above in a previous post which is why I suggested a getter function to retrieve the age from the object and total it up externally in the main() function.

If you are still getting garbage, post your code so I can see what you have now - I just quickly tested the code with the modifications here and it worked fine.


I think I know what you mean when you refer to garbage - I am guessing you are still trying to run lines 32-34 - remember each object would have its own totalAge, calculations such as this need to be done in the main function so you can access each objects age.
closed account (j3Rz8vqX)
Softrix is correct, I was simply implying why totalAge was accumulating garbage.

Having totalAge within the class will resort to an identical value of age, which may not be your intentions.

totalAge should have a greater scope than Data if it is to accumulate age from multiple Datas.

This is how my code looked when I tested it with changes:

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

#include <iostream>
using namespace std;

class Data{

private:
	int age;
	char name[31];

public:
	void storeData();
	void printData();
	int getAge();
};

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

void Data::storeData()
{
	cout << "Please enter a name (Max: 30 characters)." << endl;
	cin.getline(name, 30);
	cout << "Please enter an age." << endl;
	cin >> age;
}

void Data::printData(){
	cout << "Name: " << name;
	cout << " Age is: " << age;
}

void main()
{
	Data get[10];

	int totalAge = 0;
	int average = 0;

	// read in the data
	for (int i = 0; i < 10; i++)
		get[i].storeData();

	// print out the data
	for (int i = 0; i < 10; i++) {
		get[i].printData();
		totalAge += get[i].getAge();
	}

	// show statistics
	average = totalAge / 10;
	cout << endl << "Total of all ages: " << totalAge << endl;
	cout << "Average is: " << average << endl;

}
Topic archived. No new replies allowed.