【发布时间】:2019-03-15 00:47:33
【问题描述】:
因此,当输入变量之一出现错误时,我的程序中的所有内容似乎都运行良好。
Ex. 总工资。我需要重新输入员工编号,但它一直给出一开始就已经给出的那个。我不知道如何使重新输入员工编号而不是已经输出成为可能。我只能使用循环和 if 语句。我不会做数组。
请查看以下代码
{
const int QUIT = 0, NO_NEG = 0;
int empNum = 1;
double grossPay, fedWith,
stateWith, ficaWith, sum,
totalGross = 0, totalFed = 0,
totalState = 0, totalFica = 0,
netPay, totalNetPay = 0;
cout << "Enter the following information:\n\n"
<< "Employee Number (0 to quit): ";
cin >> empNum;
while (empNum != QUIT)
{
do
{
cout << "Gross pay: ";
cin >> grossPay;
while (grossPay < NO_NEG)
{
cout << "Gross pay may not be less than zero.\n";
cout << "Re-enter Gross pay: ";
cin >> grossPay;
}
cout << "Federal Withholding: ";
cin >> fedWith;
while (fedWith < NO_NEG)
{
cout << "Federal witholding may not "
<< "be less than zero.\n";
cout << "Re-enter Federal Withholding: ";
cin >> fedWith;
}
cout << "State Withholding: ";
cin >> stateWith;
while (stateWith < NO_NEG)
{
cout << "State witholding may not "
<< "be less than zero.\n";
cout << "Re-enter State Withholding: ";
cin >> stateWith;
}
cout << "FICA Withholding: ";
cin >> ficaWith;
while (ficaWith < NO_NEG)
{
cout << "FICA witholding may not be less than zero.\n";
cout << "Re-enter FICA Withholding: ";
cin >> ficaWith;
}
sum = (stateWith + fedWith + ficaWith);
if (sum > grossPay)
{
cout << "\nERROR: Withholdings cannot"
<< " exceed gross pay.\n"
<< "\nPlease re-enter the data for this employee.\n"
<< "Processing the next employee:\n"
<< "\nEmployee Number (0 to quit): "
<< empNum << endl;
}
cout << "Processing the next employee:\n";
} while (sum > grossPay);
totalGross += grossPay;
totalFed += fedWith;
totalState += stateWith;
totalFica += ficaWith;
netPay = grossPay - sum;
totalNetPay += netPay;
cout << "Employee Number (0 to quit): ";
cin >> empNum;
}
cout << "Total Gross Pay: $ " << totalGross << endl
<< "Total Federal Tax: $ " << totalFed << endl
<< "Total State Tax: $ " << totalState << endl
<< "Total FICA: $ " << totalFica << endl
<< "Total Net Pay: $ " << totalNetPay << endl;
return 0;
}
【问题讨论】:
-
你调试代码了吗?
-
想想您正在打印什么以及何时打印。你继续打印
"Processing the next employee",而这不是代码正在做的事情。当程序不期望任何输入时,您还要求输入。 -
是的,我做到了。我尝试了不同的方式,但我被卡住了。我在编码方面还很新,所以我不知道解决方案是否简单
标签: c++ loops variables while-loop