这是一项家庭作业,我是C ++的新手,但我需要计算员工的工资。它没有1000美元以下的明显答案,而是给了我一个1.5 * 10 ^ 23的负数或类似的数字。我做错了什么?这就是我对代码所拥有的:
//class .cpp file
#include <string>
#include <fstream>
#include "MyEmployee.h"
using namespace std;
MyEmployee::MyEmployee()
{
EMPLOYEE_NUMBER = 0;
NAME = "";
STREET_ADDRESS = "";
PHONE_NUMBER = "";
HOURLY_WAGE = 0;
WEEKLY_HOURS = 0;
GROSS_PAY = 0;
STATE_TAX = .075;
FEDERAL_TAX = .2;
OVERTIME_RATE = 1.5;
NET_PAY = 0;
FOURTY = 40;
OVERTIME_PAY = 0;
}
MyEmployee::MyEmployee(int e, string n, string s, string p, double h, double w)
{
EMPLOYEE_NUMBER = e;
NAME = n;
STREET_ADDRESS = s;
PHONE_NUMBER = p;
HOURLY_WAGE = h;
WEEKLY_HOURS = w;
}
int MyEmployee::getEmployeeNumber() const
{
return EMPLOYEE_NUMBER;
}
int MyEmployee::setEmployeeNumber(int e)
{
EMPLOYEE_NUMBER = e;
return EMPLOYEE_NUMBER;
}
string MyEmployee::getName() const
{
return NAME;
}
string MyEmployee::setName(string n)
{
NAME = n;
return NAME;
}
string MyEmployee::getStreetAddress() const
{
return STREET_ADDRESS;
}
string MyEmployee::setStreetAddress(string s)
{
STREET_ADDRESS = s;
return STREET_ADDRESS;
}
string MyEmployee::getPhoneNumber() const
{
return PHONE_NUMBER;
}
string MyEmployee::setPhoneNumber(string p)
{
PHONE_NUMBER = p;
return PHONE_NUMBER;
}
double MyEmployee::getHourlyWage() const
{
return HOURLY_WAGE;
}
double MyEmployee::setHourlyWage(double h)
{
HOURLY_WAGE = h;
return HOURLY_WAGE;
}
double MyEmployee::getWeekHours() const
{
return WEEKLY_HOURS;
}
double MyEmployee::setWeekHours(double w)
{
WEEKLY_HOURS = w;
return WEEKLY_HOURS;
}
double MyEmployee::calcPay()
{
if (WEEKLY_HOURS > FOURTY)
{
OVERTIME_PAY = ((WEEKLY_HOURS - FOURTY) * HOURLY_WAGE) * OVERTIME_RATE;
GROSS_PAY = OVERTIME_PAY + (FOURTY * HOURLY_WAGE);
NET_PAY = GROSS_PAY - (GROSS_PAY * FEDERAL_TAX) - (GROSS_PAY * STATE_TAX);
}
else
{
GROSS_PAY = WEEKLY_HOURS * HOURLY_WAGE;
NET_PAY = GROSS_PAY - (GROSS_PAY * FEDERAL_TAX) -(GROSS_PAY * STATE_TAX);
}
return NET_PAY;
}
//This is my "driver" or main code:
#include <string>
#include <iostream>
#include <iomanip>
#include "MyEmployee.h"
using namespace std;
void printCheck(MyEmployee&);
int main()
{
MyEmployee emp1(1, "Dale Nelson", "480 UVU lane, Orem, UT 84058", "801-863-8000", 10, 45);
MyEmployee emp2(2, "John Smith", "161 South 550 East, Enterprise, Utah 84725", "801-867-5309", 12.5, 30);
printCheck(emp1);
system("PAUSE");
system("CLS");
printCheck(emp2);
system("PAUSE");
return 0;
}
void printCheck(MyEmployee& employ1)
{
cout.precision(2);
cout << "-------------------Fluff Shuffle Electronics----------------------" << endl;
cout << "\nPay to the order of " << employ1.getName() << " ............................$" << fixed << setprecision(2) << employ1.calcPay() << endl;
cout << "United Bank of Orem" << endl;
cout << "------------------------------------------------------------------" << endl;
cout << "Hours worked: " << employ1.getWeekHours() << endl;
cout << "Wages: $" << fixed << employ1.getHourlyWage() << endl;
}
答案 0 :(得分:0)
你的每周时间是0和40是40.在你的第一个分支机构,你有每周小时 - 四十分,这将给你-40。我不知道你在找什么,但你应该从那里开始
答案 1 :(得分:0)
你有两个costructors
。一个是默认的(没有任何参数),第二个是参数。
MyEmployee::MyEmployee()
在这里,您要将许多值设置为默认值(初始化为所需的值)
MyEmployee::MyEmployee(int e, string n, string s, string p, double h, double w)
在这里,您只设置了几个参数,使所有其他参数保持未初始化,即FOURTY
,OVERTIME_RATE
,FEDERAL_TAX
,STATE_TAX
等垃圾。
当你按照下面的方式创建对象时,会调用参数化构造函数,保持很多东西不被启动。
MyEmployee emp1(1, "Dale Nelson", "480 UVU lane, Orem, UT 84058", "801-863-8000", 10, 45);
因此,请在constructor
中初始化其他变量。
答案 2 :(得分:0)
变量FOURTY
的值应该是40,这太可怕了。将它作为成员变量(就好像每个员工的价值可能不同)更是如此(尽管在软件工程中并不完全不现实)。但是,您只在两个构造函数的一个中设置其值。在使用的构造函数中,它的值仍未定义,这会导致问题。