Well, thank you guys for your suggestions. I fixed my problem by getting rid of member function compute_avg(). Instead, I created a get_age() member function. So, I keep a running total of the age in main() in a loop. Then, I find the average in main() by dividing by number of objects.
7stud said:
Or, you could create static data members of the class that keep track of the total age and the number of objects, and then you can use a single object to access that data to calculate the average.
My questions are:
1) How do I get access to that single object in main() when I have an array of <i>Personal</i> objects?
2) If I want to calculate the average of ages of Personal objects of only a certain range, how would I do that without doing it in main()?
Thank you in advance for your answers.
-Justin
P.S. my new correctly functioning code is below:
Code:
#include <iostream>
using std::cout;
using std::cin;
using std::getline;
using std::endl;
#include <iomanip>
using std::setw;
using std::setprecision;
#include <cstring>
class Personal {
public:
// default constructor
Personal(const char *fname="Joe", const char *lname="Smith", const int years_old=20);
// set functions
void input_personal(); // input data of 1 person including first name, last name, and age
void print_data(float); // must send average as argument to function to find deviation of
// each person's age from the average, and then output in neat format
// get functions
inline int get_age() const; // gets age of each person
// destructor
~Personal();
private:
char first_name[30]; // store first name of max. 30 characters
char last_name[30]; // store last name of max. 30 characters
int age; // store age
};
// constructor
Personal::Personal(const char *fname, const char *lname, const int years_old)
{
// copy fname into first_name and be sure it fits
int length = strlen(fname);
length = (length < 30 ? length : 29);
strncpy (first_name, fname, length);
first_name[length] = '\0';
// copy lname into last_name and be sure it fits
length = strlen(lname);
length = (length < 30 ? length : 29);
strncpy (last_name, lname, length);
last_name[length] = '\0';
// error check value put into age
age = (years_old < 150 ? years_old : 20); // default age at 0
} // end Personal constructor
// input Personal info.
void Personal::input_personal()
{
// How come I don't need to enter a null at the end? Does setw restriction put one in 30th character?
cout << "Enter first name: ";
cin >> setw(30) >> first_name; // input first name
int length = strlen(first_name);
first_name[length]='\0';
cout << "Enter last name: ";
cin >> setw(30) >> last_name; // input last name
length = strlen(first_name);
first_name[length]='\0';
cout << "Enter age: ";
cin >> age;
while (age < 0 || age > 150) {
cout << "Age must be between 0 to 150. Re-enter: ";
cin >> age;
}
} // end function input_personal
inline int Personal::get_age() const {
return age;
}
void Personal::print_data(float avg) {
float avg_diff = age - avg;
cout << last_name << ", " << first_name << " " << age << " "
<< avg_diff << endl;
}
//destructor
Personal::~Personal() {
}
int main()
{
const int NUM = 10; // define for how many people to output personal info.
Personal person[NUM];
unsigned int total_age=0;
float average, dev_from_avg;
for (int k=0; k < NUM; k++) {
person[k].input_personal(); // input info. for each person
}
for (int b=0; b < NUM; b++) {
total_age += person[b].get_age(); // finds running total of the ages
}
average = total_age / NUM;
cout << "\nThe average of the ages is " << setprecision(10) << average << endl;
for (int r=0; r < NUM; r++) {
person[r].print_data(average); // must pass average so it can calculate deviation from avg
// and output it along with other info. in print_data()
}
cout << endl;
return 0;
}