从程序中得到错误的输出.编译时没有错误
getting bad output from program. compiles without errors
here is my header file:
#ifndef weeklyEmp_h
#define weeklyEmp_h
// SPECIFICATION FILE weeklyEmp.h
//
//----------------------------------------------------------------------
#include <string>
using namespace std;
class weeklyEmp{
public:
//constructors
weeklyEmp();
weeklyEmp (string initName,
double initHours,
double initRate,
int initExemptions,
string initFilingStatus);
//--modifiers
void set_hours(double thisWeeksHours);
//post: Set the hours worked for a given week
void set_rate(double thisWeeksRate);
//post: Change the employee's hourly rate of pay
//--accessors
double grosPay () const;
//post: Return gross pay with overtime
double incomeTax() const;
//post: Return the federal income tax
double FICATax() const;
//post: Return the social security tax
string name() const;
//post: Return the employee's name
private:
string my_name;
double my_hours;
double my_rate;
int my_exemptions;
string my_filingStatus;
};
#endif //weeklyEmp_h
这是一个包含的。cpp文件:
#include <iostream>
#include <string>
#include "weeklyEmp.h"
using namespace std;
const double WEEKLY_ALLOWANCE = 39.42;
const double FICA_TAX_RATE = 0.0765;
weeklyEmp::weeklyEmp()
{
my_name = "?name?";
my_hours = 0.0;
my_rate = 0.0;
my_exemptions; 0;
my_filingStatus = "?filingStatus?";
}
weeklyEmp::weeklyEmp(string initName,
double initHours,
double initRate,
int initExemptions,
string initFilingStatus)
{
my_name; initName;
my_hours; initHours;
my_rate; initRate;
my_exemptions; initExemptions;
my_filingStatus; initFilingStatus;
}
//--modifiers
void weeklyEmp::set_hours(double thisWeeksHours)
//post: Set the hours worked for a given week
{
my_hours = thisWeeksHours;
}
void weeklyEmp::set_rate(double thisWeeksRate)
//post: Change the employee's hourly rate of pay
{
my_rate = thisWeeksRate;
}
//--accessors
double weeklyEmp::grosPay() const
//post: Return gross pay with overtime
{if(my_hours <=40) return my_hours * my_rate;
else
return (40*my_rate) + (my_hours-40) * 1.5 * my_rate;
}
double weeklyEmp::incomeTax() const
//post: Return the federal income tax
{
double result(0.0);
double taxableIncome(grosPay() - my_exemptions * WEEKLY_ALLOWANCE);
if(my_filingStatus == "S" || my_filingStatus == "s")
{
if (taxableIncome <= 23.00)
result = 0.00;
else if(taxableIncome <= 397.00)
result = 0.15 * (taxableIncome - 23.00);
else if(taxableIncome <= 928.00)
result = 56.10 + 0.28 * (taxableIncome - 397.00);
else if(taxableIncome <= 2121.00)
result = 204.78 + 0.33 * (taxableIncome - 928.00);
else
result = 598.47 + 0.28 * (taxableIncome - 2121.00);
}
if(my_filingStatus == "M" || my_filingStatus == "m")
{
if(taxableIncome <= 65.00)
result = 0.00;
else if(taxableIncome <= 689.00)
result = 0.15 * (taxableIncome - 65.00);
else if(taxableIncome <= 1573.00)
result = 93.60 + 0.28 * (taxableIncome - 689.00);
else if(taxableIncome <= 3858.00)
result = 341.12 + 0.33 * (taxableIncome - 1573.00);
else
result = 1095.17 + 0.28 * (taxableIncome - 3858.00);
}
/* round to the nearest penny */
/* include compfun.cpp for round function */
result = (result, 2);
return result;
}
double weeklyEmp::FICATax() const
//post: Return the social security tax
{
return grosPay() * FICA_TAX_RATE;
}
string weeklyEmp::name() const
//post: Return the employee's name
{
return my_name;
}
最后是主cpp文件:
#include <iostream>
#include <string>
#include "weeklyEmp.h"
#include "COMPFUN.H"
using namespace std;
int main()
{
string name;
double rate;
double hours;
int exemptions;
string filingStatus;
cout <<"Name: ";
cin >> name;
cout << "Hourly Rate:";
cin >> rate;
cout << "Hours Worked:";
cin >> hours;
cout << "Exemptions: ";
cin >> exemptions;
cout<< "S) ingle / M) arried: ";
cin >> filingStatus;
cout << endl;
weeklyEmp emp(name, hours, rate, exemptions, filingStatus);
double net = emp.grosPay() - emp.incomeTax() - emp.FICATax();
cout << "Employee: " << name << endl;
cout << "Gross Income FICA Net" << endl;
decimals(cout, 2);
cout << emp.grosPay() << emp.incomeTax() <<emp.FICATax() <<net << endl;
cout.width(9); cout << emp.grosPay();
cout.width(13); cout << emp.incomeTax() << endl;
system("pause");
return 0;
}
这是我得到的垃圾输出。我认为错误是在。cpp文件(不是主要的,但包括在内)。我找不到它。
名称:马克每小时工资率:10工作时间:10豁免:1S) single/M) delivered: S
员工:马克总收入净额8567285355521621000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.002.00655397329697403930000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.007911888025824217100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.00按任意键继续…
我很确定你的问题是这个构造函数:
weeklyEmp::weeklyEmp(string initName,
double initHours,
double initRate,
int initExemptions,
string initFilingStatus)
{
my_name; initName;
my_hours; initHours;
my_rate; initRate;
my_exemptions; initExemptions;
my_filingStatus; initFilingStatus;
}
看起来你用;
代替=
,所以这些成员将具有未确定的值,因为它们没有被初始化。如果您启用了警告,它应该指出这些行。在gcc
和-W -Wall -pedantic
编译时,对这些行给出以下警告:
warning: statement has no effect [-Wunused-value]
相关文章:
- 编译并运行后,输出未出现在 Visual Studio 代码中
- std::cout输出int时出现编译错误
- 在由Sublime文本3编译后在cmd上显示Java程序输出
- 这是使用回溯的 nqueen 问题,但我使用了动态 2d 数组,我的程序编译良好,但不返回任何输出
- 代码编译没有任何输出,入门程序
- 编译 gsoap 输出文件 (onvif) 时出现问题
- 如何在编译过程中输出C 类型信息
- Linux MinGW:在编译时,他输出了 8 个"nultiple definitions"列表
- 将char8_t const* 输出到 cout 和 wcout,一个编译一个不编译
- GCC 预处理器输出和编译一次通过,v2
- 已编译的 protobuf 文件的意外输出路径
- Visual Studio 编译的程序在尝试减小输出大小后无法正确初始化
- 尝试用 c++ 制作一个简单的加法器.编译成功,但输出错误
- Protobuf:了解原型文件的编译输出
- 二进制搜索 - 代码编译并运行后不显示输出
- 代码可以编译,但没有输出
- 在不同于依赖DLL的文件夹中输出已编译目标的正确方法是什么
- 编译输出程序集
- GCC将为运行时或编译输出留下固定值的算术吗?
- 禁用编译输出