我正在试图找出如何将计算出的总工资存储到grossPay变量中,以便将其用于计算净工资。使用结构时,我会抛弃它。
#include<string>
#include <iostream>
using namespace std;
struct Workers {
string name;
double hourlyWage;
int hoursWorkedPerDay;
int hoursWorkedPerWeek;
};
int healthTax = 130;
double otherTaxes = 0.17;
double grossPay;
int main() {
Workers JohnDoe;
JohnDoe.name = "John Doe";
JohnDoe.hourlyWage = 8.50;
JohnDoe.hoursWorkedPerDay = 6;
cout<<"- John Doe's weekly earnings - "<< endl;
cout<<"Hourly rate: $8.50" << endl;
cout<<"Hours worked per day: 6" << endl;
cout<<"Gross pay: $" << 7 * (JohnDoe.hourlyWage * JohnDoe.hoursWorkedPerDay)<<endl; // gross pay
grossPay; // how to store gross pay into this variable?
cout<<"Net pay: $" << otherTaxes * (grossPay - healthTax) / 100; // net pay
}
答案 0 :(得分:4)
我认为您可能无法理解变量的工作原理。您不存储公式,存储计算公式的结果:
double grossPay = 7 * (JohnDoe.hourlyWage * JohnDoe.hoursWorkedPerDay);
答案 1 :(得分:1)
其他答案是正确的,但稍微好一点的解决方案是将Worker
引用作为参数并返回结果的函数:
double getGrossPay(const Workers& worker) {
return 7 * (worker.hourlyWage * worker.hoursWorkedPerDay);
}
然后简单地说:
cout<<"Gross pay: $" << getGrossPay(JohnDoe) <<endl; // gross pay
答案 2 :(得分:0)
这就是你要做的事情:
double grossPay = 7 * ( JohnDoe.hourlyWage * JohnDoe.hoursWorkedPerDay );
然后您可以使用它,在这种情况下,打印它:
cout<<"Gross pay: $" << grossPay <<endl;