如何修复一个无限while循环

How do I fix a infinite while loop?

本文关键字:一个 无限 while 循环 何修复      更新时间:2023-10-16

我有一个函数,它使用while循环来接收表单中的多个输入。

函数比较

id, C表示添加,D表示删除,amount to add/remove by

C

D

951482 C 29.628

D

D

951482 X‐1

函数将这些行与其他地方接收到的输入行进行比较

id, charge(忽略这个),数据限制和起始数据

951482 6.25 50 35.733

更改在起始数据

上执行。

最后一行是我的循环的结束

我现在有

    void read_and_total (bool& any_errors, double& starting_disk_usage,
                     double& max_disk_usage, double& end_disk_usage,
                     int& account_number)
{
    int line_account;
    double data_change;
    char transaction_char;
    int line_number = 2;
    end_disk_usage = starting_disk_usage;
    max_disk_usage = starting_disk_usage;

    cin >> line_account >> transaction_char >> data_change;
    any_errors = false;
    while (account_number != line_account && transaction_char != 'X' && data_change != -1)
    {
        cout << line_account << ' ' <<  transaction_char << ' ' << data_change      << endl;
        if (transaction_char == 'D')
            end_disk_usage =  end_disk_usage - data_change;
        if (transaction_char == 'C')
            end_disk_usage = end_disk_usage + data_change;
        if (max_disk_usage < end_disk_usage)
            max_disk_usage = end_disk_usage;
        //Error proccessing
        if (transaction_char != 'C' && transaction_char != 'D' &&
                transaction_char != 'X')
        {
                cout << "Error in line #" << line_number << " - "
                     << "invalid transaction code" << ' ' << transaction_char
                     << ' ' << endl;
                any_errors = true;
        }
        if (data_change < 0)
        {
            cout << "Error in line #" << line_number << " - "
                 << "invalid transaction amount " << data_change << endl;
            any_errors = true;
        }
        if (line_account != account_number) {
            cout << "Error in line #" << line_number << " - "
                 << " non‐matching account number" << line_account
                 << endl;
            any_errors = true;
        }
        cout << "Bottom of function" << endl;
        cin >> line_account >> transaction_char >> data_change;
    }
    return;
    }

我的while循环是无限循环,由于控制变量不更新,我不明白为什么会发生这种情况,因为我要求一个新的字符和新的双精度。

编辑6:

Ok,它实际上仍然不工作,但是我的while语句中的错误已经暴露,我想比较account_number和line_account,这是我使用坏变量名的错误。

所以当我使用其中一个解

while(account_number != line_account && transaction_char != 'X' && data_change != -1)

不进入循环,因为它总是false,并跳过其余的数字

my while loops正在测试这一行

951482 X ‐1

,其中line_account number与我的account_number相同字符是X值恰好是-1

当我删除对account_number的检查时,它可以工作,但添加了它后,它总是无法进入循环。

编辑4:

我在顶部添加了关于这个问题的额外信息

我想让我的while循环检查数据

末尾的这一行

951482 X‐1

和三个值必须正确,账号应与标题相同字符应该是x并且该值必须为-1,而不是任何其他的负值。

编辑3:

我现在把我的while语句改为

while (!(account_number == line_number && transaction_char == 'X' && data_change == -1))

我相信这就是我的问题的根源,我试图让所有三个条件都为真,循环结束。

编辑2:

添加2个测试输出后,结果消息是

的重复
Bottom of function
951482 X 0.000000

我不知道为什么-1被设置为0.00000

编辑:

如果在while循环的末尾包含一个test语句,则会导致无限打印语句。我想我的while循环条件有问题

如何将您的while()语句更改为:

while(account_number != line_number && transaction_char != 'X' && data_change != -1))
{
  :
  :
}

我怀疑

while (!(account_number == line_number && transaction_char == 'X' && data_change == -1))
应该

while (account_number != line_number || transaction_char != 'X' || data_change != -1)