我试图弄清楚如何允许我的程序无限期输入另一个名称。现在,如果您要查找其他名称,则需要重新启动整个程序。该程序计算员工的净工资值。所有员工都可以从支票中扣除标准的$ 45。 如果员工的收入不足以抵扣,则会显示错误。
#include <iostream>
#include <string>
using namespace std;
int
main()
{
// Declare variables
string name;
int hours;
int DEDUCTION = 45;
int gross;
int net;
int rate;
string EOFNAME = "quit";
char again = 'Y';
// Declare input items
cout << "Enter first name or " << EOFNAME << " to quit ";
cin >> name;
if (name == EOFNAME) {
cout << "End of program ";
return 0;
} else {
cout << "Enter hours worked for " << name << endl;
cin >> hours;
cout << "Enter hourly rate for " << name << endl;
cin >> rate;
gross = hours * rate;
net = gross - DEDUCTION;
}
do {
cout << "Net pay for " << name << " is " << net << endl;
break;
} while (net > 0);
{
if (net < 0)
cout << "Deductions not covered. Net is 0." << endl;
}
return 0;
} // end of main
答案 0 :(得分:1)
在这种情况下,您需要实现main loop。可能我建议以下内容:
首先,将main()
中的所有内容放入另一个函数中,例如:
void processEmployeeDeductions()
{
// All the code currently in main()
}
然后从main()
中的无限循环开始,该循环只是反复调用该函数:
int main()
{
while(1) // Loop forever!
{
processEmployeeDeductions();
}
return 0;
}
当代码变得更加复杂时,您将能够更改循环的条件以在特定事件上触发(例如处理程序SIGINT
将标志设置为结束循环)。