如何修复一个无限while循环
How do I fix a infinite while loop?
我有一个函数,它使用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)
- 为什么这个递归函数会创建一个无限循环?
- 如何找出为什么我的程序一直有一个无限循环?
- 为什么我有一个无限循环,我如何以不同的方式做到这一点
- WinUsb_WritePipe进入了一个无限循环
- 为什么我会得到一个无限循环(因子)?
- 为什么我总是得到一个无限循环
- 为什么这个程序不表现 LIke 一个无限的输入行 - c++
- 我得到一个无限的循环,我不确定为什么或如何修复它
- 这是一个无限循环吗?程序提前完成
- 为什么它是一个无限循环?
- 为什么我的程序在输入字符时有一个无限循环
- 为什么这段代码最终会进入一个无限循环,读取std::cin
- 如何使其成为一个无限循环
- Tcl_CreateInterp怎么会变成一个无限循环呢
- C++,得到一个无限循环
- C++ 使用 cin.fail(), cin.clear() ---我有一个无限循环,不知道为什么
- 这个递归函数(回文)变成了一个无限循环
- 当对象被卡在一个无限循环中而被破坏时会发生什么
- 代码在小输入时运行良好,但在大输入时就像一个无限循环
- 从文件中读取信息后,会发生一个无限循环