我有两个ifs,第一个是加班是否有效,但我不能得到第二个如果工作,这是奖金工资,如果天工作大于5。 它没有准备好我输入的代码为bonuspay,
#include<iostream>
#include<iomanip>
using namespace std;
int main() {
float ftax, stax, SDI, SS, hw, hp, dw(0), pay, netpay, gp, OvertimePay = 0,
bonusPay(0);
int daysWorked(0);
cout << "please enter the hoursWorked: ";
cin >> hw;
cout << "---------------------" << endl;
cout << "please enter the hourlyPay: ";
cin >> hp;
cout << "---------------------" << endl;
cout << "please enter the daysWorked in the week: ";
cin >> dw;
if (hw > 40) {
OvertimePay = (hw - 40) * hp * .5;
if (daysWorked > 5) {
bonusPay = (hw - 40) * hp * .25;
}
}
gp = (hw * hp) + (OvertimePay) + (bonusPay);
ftax = gp * .10;
stax = gp * .08;
SDI = gp * .01;
SS = gp * .06;
netpay = gp - (ftax + stax + SDI + SS);
cout << " grosspay =\t\t\t\t\t" << gp << endl;
cout << " federal taxes =\t\t\t\t" << ftax << endl;
cout << " state taxes =\t\t\t\t\t" << stax << endl;
cout << " SDI =\t\t\t\t\t\t" << SDI << endl;
cout << " Social Securities =\t\t\t\t" << SS << endl;
cout << " netpay =\t\t\t\t\t" << netpay << endl;
cout << "---------------------" << endl;
cout << "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t" << endl;
cout << " grosspay = " << gp << endl;
cout << " federal taxes = " << ftax << endl;
cout << " state taxes = " << stax << endl;
cout << " SDI = " << SDI << endl;
cout << " Social Securities = " << SS << endl;
cout << " netpay = " << netpay << endl;
cout << "---------------------" << endl;
cout << "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t" << endl;
cout << left;
cout << setw(30) << " grosspay =" << gp << endl;
cout << setw(30) << " federal taxes =" << ftax << endl;
cout << setw(30) << " state taxes =" << stax << endl;
cout << setw(30) << " SDI =" << SDI << endl;
cout << setw(30) << " Social Securities =" << SS << endl;
cout << setw(30) << " netpay =" << netpay << endl;
cout << "---------------------" << endl;
system("pause");
}
答案 0 :(得分:1)
从你的问题开始:
但我无法获得第二项工作,如果工作天数大于5则为奖金
尽管您的要求并不完全清楚,但代码中的一个问题当然是您检查daysWorked
值
if (daysWorked > 5) {
// ...
但你从未在初始化后再次设置
int daysWorked(0);
我可以在您的代码示例中找到的任何地方。
因此daysWorked
的值始终为0
,而if()
子句中的代码会被忽略。
答案 1 :(得分:0)
更改
cin >>dw
至cin >>daysWorked
答案 2 :(得分:0)
我在这里可以看到两个错误。 你初始化了
float dw(0);
和
int daysWorked(0);
您没有理由通过代码初始化,只需将其留在
float dw;
int daysWorked;
我在你的代码中看到的另一个问题是,你将工作天数的值存储到dw中
cout << "please enter the daysWorked in the week: ";
cin >> dw;
但是你正在检查daysWorked
if (daysWorked > 5)
之间没有任何代码导致将dw的值转移到daysWorked。
我的建议是从
改变dw和bonusPayfloat dw(0);
float bonusPay(0);
到
float dw;
float bonusPay;
并删除
int daysWorked(0);
最后
if (hw > 40) {
OvertimePay = (hw - 40) * hp * .5;
if (dw > 5) {
bonusPay = (hw - 40) * hp * .25;
}
}