c++ - How do I fix a infinite while loop? -
i have function uses while loop receive multiple inputs in form.
the function compares
id, c add or d remove, , amount add/remove by
951482 c 5.250
951482 d 15.111
951482 c 29.628
951482 d 10.200
951482 d 3.175
951482 x ‐1
the function compares these lines input received somewhere else
id, charge(ignore this), limit of data, , starting data
951482 6.25 50 35.733
changes preformed on starting data
where last line end of loop
i have
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; } my while loop infinitely looping due control variable not updating, don't understand why happening because asking new character , new double.
edit 6:
ok not work still error in while statement has been brought light want compare account_number , line_account, fault using bad variable names.
so when used 1 of solutions
while(account_number != line_account && transaction_char != 'x' && data_change != -1) it not enter loop false, , skips on rest of numbers
my while loops testing line
951482 x ‐1 where line_account number same account_number character x , value -1
when remove checking account_number works added fails enter loop.
edit 4:
i added additional info on problem @ top
i want while loop check line @ end of data
951482 x ‐1
and 3 values must correct, account number should same header character should x , value must -1 not other negative value.
edit 3:
i have change while statement
while (!(account_number == line_number && transaction_char == 'x' && data_change == -1)) i believe source of problems trying 3 conditions true loop end.
edit 2:
after adding 2 test ouputs resulting message repition of
bottom of function 951482 x 0.000000 i not sure why -1 being set 0.00000
edit:
if include test statement @ end of while loop results in infinitely printing statement. think have problem in while loop conditions.
i suspect
while (!(account_number == line_number && transaction_char == 'x' && data_change == -1)) should be
while (account_number != line_number || transaction_char != 'x' || data_change != -1)
Comments
Post a Comment