我正在尝试使此“ do while”循环运行3次,然后在“ do while”循环内的while循环内显示累加器中包含的数量。
似乎在正确计数,但仅在第一个循环上运行while循环。运行时,它不会继续询问下一组数字,而只是显示第一批(正确累加)。我尝试过切换一些代码并搜索google,但找不到答案。
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int storeNum = 1;
int payRollAmount = 0;
int totalPayroll = 0;
do
{
cout << "Store " << storeNum << ":" << endl;
while (payRollAmount <= -1)
{
cout << "Enter Store's Payroll Amount (-1 to exit): ";
cin >> payRollAmount;
totalPayroll += payRollAmount;
}
storeNum++;
} while (storeNum <= 3);
cout << "The Total Payroll is: " << totalPayroll << endl;
system("pause");
return 0;
}
The code should take in an unknown amount of "payrolls," allow you to exit using -1, and then continue on to the next stores payrolls. It should do this 3 times, and then display the total amount (all numbers entered added together.
答案 0 :(得分:1)
嗨,也许每次迭代都重置payRollAmount
?这样,它将继续请求输入。
for (int amount = 0; amount != -1; ) {
cout << "Enter Store's Payroll Amount (-1 to exit): ";
cin >> amount;
totalPayroll += amount;
}