# Thread: Problem ending loop

1. ## Problem ending loop

I have written a program for updating a customer's account balance and am having a problem ending the looping part of it.

Here is my source code:
Code:
```#include <iostream>

using std::cout;
using std::cin;
using std::endl;
using std::fixed;

#include <iomanip>

using std::setprecision;

int main( )
{
int accountNumber;   // customers account number
double beginningBalance;   // customers balance
double totalCharges;   // charges on the account
double totalCredits;   // credits to the account
double creditLimit;   // credit limit on the account
double newBalance;    // new beginning balance

cout << setprecision( 1 ) << fixed;

while( accountNumber != -1 ) {
cout << "Enter account Number: " << endl;
cin >> accountNumber;
cout << "Enter beginning balance: " << endl;
cin >> beginningBalance;
cout << "Enter total charges: " << endl;
cin >> totalCharges;
cout << "Enter total credit: " << endl;
cin >> totalCredits;
cout << "Enter credit limit: " << endl;
cin >> creditLimit;
}

if ( beginningBalance >= creditLimit ) {
cout << "Account: " << accountNumber << endl;
cout << "Credit limit: " << creditLimit << endl;
cout << "Balance: " << beginningBalance << endl;
cout << "Credit Limit Exceeded" << endl;
}

else {
cout << "Enter beginning balance: " << endl;
cin >> beginningBalance;
cout << "Enter total charges: " << endl;
cin >> totalCharges;
cout << "Enter total credit: " << endl;
cin >> totalCredits;
cout << "Enter credit limit: " << endl;
cin >> creditLimit;

}

newBalance = beginningBalance + totalCharges - totalCredits;

return 0;
}```
I am sure that it is a simple solution that I am just overlooking after 2 hours of trying to stop the loop.

2. First, initialize accountNumber when you declare it. This won't fix your problem, but it is necessary for this program.

Your while loop will end when you type -1 for the account number while running the program. Are you sure that's what you mean?

3. Code:
`while( accountNumber != -1 )`
Enter -1 for the account number?

4. The loop doesn't make sense. Every time you run through the loop, you erase all your previous data that you had before. Perhaps you need an array?

5. my guess is that you want to break the loop the moment the user
enters -1. what you dont realise is that a while loop performs its
condition checking at the start of each iteration, it doesnt monitor
it during the iteration, so you can enter in -1 and still proceed
with the rest of the loop.

what i would do myself is change the looping condition to

while (1)

that means the looping condition doesnt depend on a variable,
its always true. but how would that help? when you read in
accountnumber, use an if statement to check and see if it is -1.
inside that statement, call a break statement:

if (accountNumner == -1)
break;

that will fix it for you. in fact, you could leave your current looping
condition in place but this makes it redundant. also, using this
method, you don't need to initialise accountNumber, but its good
practice for anything but the most trivial of programs. i myself
am a rebel and dont normally initialise unless necessary, but
thats another matter.

Popular pages Recent additions