我试图找出我的代码有什么问题。我必须使用一个结构来提示用户输入workerNumber,hoursWorked和hourlyWage。我有idNumber,hoursWorked和hourlyWage运行良好,但问题在于我的calc函数。我无法弄清楚如何计算加班时获得的金钱是1.5倍,并且能够将其打印到屏幕上。我不停地混淆奇怪的数字。
#include<iostream>
using namespace std;
struct Worker
{
int idNumber;
int hoursWorked;
double hourlyRate;
double earned;
};
void input(Worker & theData);
//Postcondition: theData.idNumber, theData.hoursWorked, and theData.hourlyRate are given input values
// the user must input into these values.
void print(const Worker &);
void calc(Worker & theWage);
void main()
{
Worker Data;
input(Data);
print(Data);
system("pause");
}
void input(Worker & theData)
{
cout << "Enter the Employee idNumber";
cin >> theData.idNumber;
cout << "Enter the Hours Worked.";
cin >> theData.hoursWorked;
cout << "Enter the HoutlyRate for under 41 hours.";
cin >> theData.hourlyRate;
}
void print(const Worker & w)
{
cout << w.idNumber << "\n"
<< w.hoursWorked << "\n"
<< w.hourlyRate << "\n"
<< w.earned << endl;
}
void calc(Worker & theWage)
{
if (theWage.hoursWorked <= 40)
{
theWage.earned = theWage.hoursWorked * theWage.hourlyRate;
}
else
{
int basePay;
basePay = theWage.hoursWorked * theWage.hourlyRate;
theWage.earned = (theWage.hoursWorked - 40) * 1.5 + basePay;
}
}
#include<iostream>
using namespace std;
struct Worker
{
int idNumber;
int hoursWorked;
double hourlyRate;
double earned;
};
void input(Worker & theData);
//Postcondition: theData.idNumber, theData.hoursWorked, and theData.hourlyRate are given input values
// the user must input into these values.
void print(const Worker &);
void calc(Worker & theWage);
void main()
{
Worker Data;
input(Data);
print(Data);
system("pause");
}
void input(Worker & theData)
{
cout << "Enter the Employee idNumber";
cin >> theData.idNumber;
cout << "Enter the Hours Worked.";
cin >> theData.hoursWorked;
cout << "Enter the HoutlyRate for under 41 hours.";
cin >> theData.hourlyRate;
}
void print(const Worker & w)
{
cout << w.idNumber << "\n"
<< w.hoursWorked << "\n"
<< w.hourlyRate << "\n"
<< w.earned << endl;
}
void calc(Worker & theWage)
{
if (theWage.hoursWorked <= 40)
{
theWage.earned = theWage.hoursWorked * theWage.hourlyRate;
}
else
{
int basePay;
basePay = theWage.hoursWorked * theWage.hourlyRate;
theWage.earned = (theWage.hoursWorked - 40) * 1.5 + basePay;
}
}
答案 0 :(得分:0)
必须在calc()
功能之前main
调用print()
函数。
void main()
{
Worker Data;
input(Data);
calc(Data); // This line was forgotten.
print(Data);
system("pause");
}
您看到的奇怪数字是未初始化的earned
变量。