0

我的代码有几个问题。

首先,使用这样的代码,无论我输入什么信息,它总是返回 0,关于在哪里解决这个问题以及如何解决的任何建议?我相信这与我的班级员工有关。我将如何解决这个问题?

二、如何访问 int total() 中的信息?我需要访问它以获取最后一点代码。

另外,如果您注意到我可以做些什么来优化我的程序,我欢迎您的建议。我正在学习 C++,并且将永远是一名学生。

// Datamax.cpp
// Created by Kennith Adkins

#include <iostream>
#include <string>

using namespace std;

class Employee
{
public:
string eName;
float eHours;
float eWage;
float ePay;
float eOvertimeHours;
float eOvertimePay;
float eTotalPay;
float eTotalBaseHours;
float eTotalSalary;
float eTotalOvertimeHours;

int Overtime ()
{
    if (eHours > 40)
    {
        eOvertimeHours = (eHours - 40);
        eOvertimePay = (eOvertimeHours * (eWage * 1.5));
        ePay = ((eHours - eOvertimeHours) * eWage);
        eTotalPay = ePay + eOvertimePay;
    }
    else
    {
        ePay = (eHours * eWage);
    }
}
int total()
{
    eTotalBaseHours = (employee1.eHours - employee1.eOvertimeHours) +   (employee2.eHours - employee2.eOvertimeHours) + (employee3.eHours -   employee3.eOvertimeHours);
    eTotalSalary = (employee1.eTotalPay + employee2.eTotalPay + employee3.eTotalPay);
    eTotalOvertimeHours = (employee1.eOvertimeHours + employee2.eOvertimeHours        + employee3.eOvertimeHours);
}
} employee1, employee2, employee3;

// Start the main program here
int main()
{
// Gretting
cout << "Welcome to the Employee Pay Center\n";

// Employee1 information
cout << "Enter the employee name: ";
cin >> employee1.eName;
cout << "Enter the hours worked: ";
cin >> employee1.eHours;
cout << "Enter his or her hourly wage: ";
cin >> employee1.eWage;
cout << endl; // Adding a blank line to space the information out

// Employee2 information
cout << "Enter the employee name: ";
cin >> employee2.eName;
cout << "Enter the hours worked: ";
cin >> employee2.eHours;
cout << "Enter his or her hourly wage: ";
cin >> employee2.eWage;
cout << endl; // Adding a blank line to space the information out

// Employee3 information
cout << "Enter the employee name: ";
cin >> employee3.eName;
cout << "Enter the hours worked: ";
cin >> employee3.eHours;
cout << "Enter his or her hourly wage: ";
cin >> employee3.eWage;
cout << endl; // Adding a blank line to space the information out

// Returning the information to the Employeer
cout << "Employe Name ............ = " << employee1.eName << "\n";
cout << "Base Pay................. = " << employee1.ePay << "\n";
cout << "Hours in Overtime........ = " << employee1.eOvertimeHours << "\n";
cout << "Overtime Pay Amount...... = " << employee1.eOvertimePay << "\n";
cout << "Total Pay................ = " << employee1.eTotalPay << "\n\n";

cout << "Employe Name ............ = " << employee2.eName << "\n";
cout << "Base Pay................. = " << employee2.ePay << "\n";
cout << "Hours in Overtime........ = " << employee2.eOvertimeHours << "\n";
cout << "Overtime Pay Amount...... = " << employee2.eOvertimePay << "\n";
cout << "Total Pay................ = " << employee2.eTotalPay << "\n\n";

cout << "Employe Name ............ = " << employee3.eName << "\n";
cout << "Base Pay................. = " << employee3.ePay << "\n";
cout << "Hours in Overtime........ = " << employee3.eOvertimeHours << "\n";
cout << "Overtime Pay Amount...... = " << employee3.eOvertimePay << "\n";
cout << "Total Pay................ = " << employee3.eTotalPay << "\n\n";

cout << "*******************************************************\n";
cout << "*****************EMPLOYEE SUMMARY DATA*****************\n";
cout << "*******************************************************\n";
cout << "** Total Employee Salaries............ " <<   "**\n";
cout << "** Total Employee Hours............... " <<   "**\n";
cout << "** Total Overtime Hours............... " <<   "**\n";
cout << "*******************************************************\n";
    cout << "*******************************************************\n";


return 0;
}

嘿伙计们,感谢您的帮助。我现在已经完成了大部分。它正在显示所有信息。我现在正在努力让它显示员工摘要数据。我修改了我的代码以使其更清晰,因为我正在尝试给我的每一个建议,因为我亲手学习是最好的。

4

2 回答 2

1

这就是使用非初始化变量所得到的。您没有为您的班级成员设置任何价值,您不能指望编译器猜测您的员工姓名或总工资是多少。

您需要使用以下表格:

object name.member name = value

于 2012-08-19T23:52:09.890 回答
1

当然,您应该在输出这些函数应该产生的结果之前调用这些函数:

employee1.Overtime();
employee2.Overtime();
employee3.Overtime();
于 2012-08-19T23:57:54.877 回答