In the program listed below, include the class definition that asks the user to
ID: 3638008 • Letter: I
Question
In the program listed below, include the class definition that asks the user to enter data for three employees and then displays the entered data. When compiling the inormation on my visual studio 2010 express, it states: I am using "UNDECLARED VARIABLES IN MY DATA COMPILED WHICH CAUSES THE ENTIRE PRORGAM COMPILED TO CRASH WITH ERRORS"! What is the right way to declare variables with the term empinfo, so the prorgam compiles and outputs the neccessary data?
#include
using namespace std;
class employee
{
private:
int id, maxHours;
float payRate;
public:
void setInfo (int pin, float pay, int hours)
{
empInfo[pin].id = pin;
empInfo[pin].payRate = pay;
empInfo[pin].maxHours = hours;
}
void viewInfo (int record)
{
cout << empInfo[record].id;
cout << endl << empInfo[record].payRate;
cout << endl << empInfo[record].maxHours;
}
};
int main ( )
{
int pin, hours, record;
float pay;
employee empInfo[1000];
cout << "Enter employee 3 digit ID: ";
cin >> pin;
cout << "Enter employee pay rate: ";
cin >> pay;
cout << "Enter employee max hours: ";
cin >> hours;
empInfo[pin].setData(pin, pay, hours);
cout << "Enter the employee ID# of the record you wish to view: ";
cin >> record;
empInfo.viewDate(record);
system("PAUSE");
return 0;
}
Explanation / Answer
You cannot use the object of the same class to define its method, instead use the variable name of the class directly.
using namespace std;
class employee
{
private:
int id, maxHours;
float payRate;
public:
void setInfo (int pin, float pay, int hours)
{
id = pin;
payRate = pay;
maxHours = hours;
}
void viewInfo () // No parameters req.
{
cout << id;
cout << endl << payRate;
cout << endl << maxHours;
}
};
int main ( )
{
int pin, hours, record;
float pay;
employee empInfo[1000];
cout << "Enter employee 3 digit ID: ";
cin >> pin;
cout << "Enter employee pay rate: ";
cin >> pay;
cout << "Enter employee max hours: ";
cin >> hours;
empInfo[pin].setData(pin, pay, hours);
cout << "Enter the employee ID# of the record you wish to view: ";
cin >> record;
empInfo[record].viewDate();
system("PAUSE");
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.