Original Post
I am currently taking a programming class from my university and am running into some strange problems with a program. We have to create an array of pointers to objects, manipulate the data within the objects, perform a calculation, and output everything to the screen.
Here's the code:
/*
prog2.cpp
*/
#include <iostream>
#include <string>
//#include <fstream>
using namespace std;
void processarray();
double calculatesalary();
class employee
{
public:
employee() {}
//Method to set the name variable.
void setname(string n)
{
name = n;
return;
}
//Method to retrieve the name variable
//Returns string
string getname() const
{
return name;
}
//Method to set the years variable.
void setyears(double y)
{
years = y;
return;
}
//Method to retrieve the years variable.
//Returns double
double getyears() const
{
return years;
}
//Method to set the pay variable
void setpay(double p)
{
payrate = p;
return;
}
//Method to retrieve the pay variable
//Returns double
double getpay() const
{
return payrate;
}
//Method to set the salary variable
void setsalary(double s)
{
salary = s;
return;
}
//Method to retrive the salary variable
//Returns double
double getsalary() const
{
return salary;
}
private:
//Variables for employee object.
string name;
double years,
payrate,
salary;
};
/*
Instantiation of an array of pointers to
employee objects
*/
employee * empArray = new employee[1];
void main()
{
/*
Temp variables to be passed into
employee object methods
*/
string n;
double y,
p;
int i;
for(i=0; i<1; i++)
{
//string n; // Temp variable for name input
//double y, // Temp variable for year input
// p; // Temp variable for pay input
cout << "Enter the employee name => ";
cin >> n;
empArray[i].setname(n);
cout << "Enter the number of years which the employee " << endl
<< "has worked for the company => ";
cin >> y;
empArray[i].setyears(y);
cout << "Enter the employee payrate => ";
cin >> p;
empArray[i].setpay(p);
}
processarray();
}
double calculatesalary(employee e)
{
e.setsalary((e.getyears() * e.getpay()));
return e.getsalary();
}
void processarray()
{
int i;
for(i=0; i<1; i++)
{
calculatesalary(empArray[i]);
}
for(i=0; i<1; i++)
{
cout << "\nName: " << empArray[i].getname() << endl;
cout << "Years at Company: " << empArray[i].getyears() << endl;
cout << "Payrate: " << empArray[i].getpay() << endl;
cout << "Salary: " << empArray[i].getsalary() << endl;
cout << "\n" << empArray[i].getpay() << " " << empArray[i].getyears() << " "
<< empArray[i].getsalary() << endl;
}
}
I know the salary calculation doesn't make sense but it was what the professor specifically asked for... I double checked!
Anyhow... say you enter bill for the name, 2 for the years and 2 for the payrate. The calculation makes salary 4 (which I can see from a debugger) but when it tries to output the data at the end, it prints a large negative value for salary... even though the other variables are ok.
Any help would be greatly appreciated!
Thanks in advance...
--------------------------------------
I'm not a freak, I'm just really shiny.
[edited by - RevKilljoy on July 19, 2003 3:59:42 PM]
[edited by - RevKilljoy on July 19, 2003 4:00:35 PM]