Dartuso Dartuso - 22 days ago 14
C++ Question

How do I fix a infinite while loop?

I have a function that uses a while loop to receive multiple inputs in the form.

The function compares

id, C for add or D for remove, and amount to 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 to an input received somewhere else

id, charge(ignore this), limit of data, and starting data

951482 6.25 50 35.733

changes are preformed on the starting data

Where the last line is the end of my loop

I currently 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_number && 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 is infinitely looping due to the control variable not updating, I don't understand why this is happening because I asking for a new character and new double.

Edit:
If a include a test statement at the end of the while loop it results in infinitely printing statement. I think I have a problem in my while loop conditions.

Edit 2:
After adding 2 test ouputs the resulting message is a repition of

Bottom of function
951482 X 0.000000


I am not sure why -1 is being set to 0.00000

Edit 3:
i have now change my while statement to

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


I believe this is the source of my problems I am trying to get all three conditions to be true for the loop to end.

Edit 4:
I added additional info on the problem at the top

i want my while loop to check for this line at the end of my data

951482 X ‐1

and all three values must be correct,
account number should be the same as the header
the character should be x
and the value MUST be -1 not any other negative value.

Edit 5:
The correct answer is below thank you to everyone who suggested things I will work on asking better question with more background information

Answer

How about changing your while() statement to:

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