Seeking Help on my 1st C++ class program!
Hi,
First of all, thank you for opening this post. This is my first C++ program with classes, and I've ran into some trouble.
I am writing program using a class that stores first name, last name, and age for each person. Then, using that info, it calculates average of ages of several people and deviation from avg. for each person.
My problem is my member function "compute_avg()" does not return proper average even though when I output value of average within it, it gives correct average. Because avg. is returned incorrectly, deviation from avg for each person is incorrect as well.
I am posting the code and sample output here for convenience, but you can also download it from attachment. Thank you very much for your help in advance. I need this ASAP!
Code:
#include <iostream>
using std::cout;
using std::cin;
using std::getline;
using std::endl;
#include <iomanip>
using std::ios;
using std::setw;
using std::setprecision;
#include <cstring>
class Personal {
public:
// constructor
Personal(const char *fname="Joe", const char *lname="Smith", const int years_old=0);
// set functions
void input_personal(); // input data of 1 person
double compute_avg(); // find average of ages
double dev_from_avg(); // compute deviation from average
// get function
void print_data(); // prints data of each in formatted output incl. average of ages
// 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
double avg; // store average
double dev_diff; // store person's deviation from the average
};
// 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); // what is name?
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); // what is name?
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 : 0); // default age at 0
avg = 0; // set avg to 0 for each new object
dev_diff = 0; // set deviation difference from avg to 0
} // end Personal constructor
// input Personal info.
void Personal::input_personal()
{
for (int r=0; r < 30; r++) {
first_name[r] = '\0';
last_name[r] = '\0';
}
cout << "Enter first name: ";
cin >> setw(30) >> first_name; // input first name
cout << "Enter last name: ";
cin >> setw(30) >> last_name; // input last name
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
double Personal::compute_avg()
{
static int total = 0;
static int count = 0;
total += age;
count++;
avg = static_cast <double> (total) / count;
return avg;
} // end function compute_avg
double Personal::dev_from_avg() {
dev_diff = age - avg;
return dev_diff;
} // end function dev_from_avg
void Personal::print_data() {
cout << last_name << ", " << first_name << " " << age << " "
<< ios::showpos << dev_diff << endl;
}
// destructor
Personal::~Personal() {
}
int main()
{
const int NUM = 2; // define for how many people to output personal info.
Personal person[NUM];
double average;
for (int k=0; k < NUM; k++) {
person[k].input_personal(); // input info. for each person
}
for (int b=0; b < NUM; b++) {
average = person[b].compute_avg(); // finds average for each new person
}
// output average only after the last person
cout << "\nThe average of ages is: " << ios::dec << setprecision(1)
<< average << endl;
for (int c=0; c < NUM; c++) {
person[c].dev_from_avg(); // finds deviation from avg for each person
}
for (int r=0; r < NUM; r++) {
person[r].print_data();
}
cout << endl;
return 0;
}
Sample Output:
-------------------
Enter first name: Justin
Enter last name: Smith
Enter age: 20
Enter first name: Joe
Enter last name: Smith
Enter age: 22
The average of ages is: 22e+01
Smith, Justin 20 20480
Smith, Joe 22 20481
-----------------------------------------
Once again, I appreciate your time and effort in helping me find the error. Thank you.
Justin