结果总是返回0并访问类

时间:2012-08-19 23:49:30

标签: c++

我的代码存在一些问题。

首先,使用类似的代码,无论我输入什么信息,它总是返回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;
}
嘿,伙计们,谢谢你的帮助。我现在已经完成了大部分工作。它显示所有信息。我现在正在努力让它显示员工摘要数据。我修改了我的代码以使其更清晰,因为我正在尝试给予我的每一个建议,因为我学到了最好的东西。

2 个答案:

答案 0 :(得分:1)

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

您需要使用以下表单:

object name.member name = value

答案 1 :(得分:1)

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

employee1.Overtime();
employee2.Overtime();
employee3.Overtime();