首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在C++中使用带有“do while”循环的计数器?

问如何在C++中使用带有“do while”循环的计数器?
EN

Stack Overflow用户
提问于 2019-05-03 06:23:28
回答 1查看 45关注 0票数 0

我试图让这个“do while”循环运行3次,然后在“do while”循环内的一个while循环中显示累加器中包含的数量。

它似乎可以正确计数,但只在第一个循环上运行while循环。运行时,它只显示第一批(正确相加),而不是继续询问下一组数字。我试着交换一些代码,在谷歌上搜索,但找不到答案。

代码语言:javascript
复制
#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.
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-05-03 06:26:38

您好,也许在每次迭代时重置payRollAmount?这样,它将继续请求输入。

代码语言:javascript
复制
for (int amount = 0; amount != -1; ) {
    cout << "Enter Store's Payroll Amount (-1 to exit): ";
    cin >> amount;
    totalPayroll += amount;
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55961005

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档