# Do While Loop Problem

• 10-13-2005
DrKillPatient
Do While Loop Problem
Hey, sup yall. I gotta write this program that checks an inputted bank account's credit limit against input/expenditures, the code is below

Code:

```#include <iostream> using std::cout; using std::cin; using std::endl; int main() { int accountNumber; double beginningBalance; double totalCharges; double totalCredits; double creditLimit; double balance;   do {   cout << "Enter Account Number (-1 To End): ";   cin >> accountNumber;   cout << "Enter Beginning Balance: ";   cin >> beginningBalance;   cout << "Enter Total Charges: ";   cin >> totalCharges;   cout << "Enter Total Credits: ";   cin >> totalCredits;   cout << "Enter Credit Limit: ";   cin >> creditLimit;   cout << "\nAccount Number: " << accountNumber;   cout << "\nCredit Limit: " << creditLimit;   balance = ((beginningBalance + totalCredits) - (totalCharges));   cout << "\nBalance is: " << balance;   if (balance > creditLimit)   cout << "\nCredit Limit not exceeded!\n";  else   cout << "\nCredit Limit exceeded!\n"; } while (accountNumber != -1);   return 0; }```
Now, it should output this:

Should Output:
----------------

Enter Account Number (-1 To End): 100
Enter beginning balance: 5394.78
Enter Total Charges: 1000.00
Enter Total Credits: 500.00
Enter Credit Limit: 5500.00
Account: 100
Credit Limit: 5500.00
Balance: 5894.78
Credit Limit Exceeded

Enter Account Number (-1 to End): -1

But it outputs this:

Outputs:
----------

Enter Account Number (-1 To End): 100
Enter Beginning Balance: 5394.78
Enter Total Charges: 1000.00
Enter Total Credits: 500.00
Enter Credit Limit: 5500.00
Account Number: 100
Credit Limit: 5500
Balance is: 4894.78
Credit Limit exceeded!

Enter Account Number (-1 To End): -1
Enter Beginning Balance

-----

So basically neither the do while or the balance = ((beginningBalance + totalCredits) - (totalCharges)) works. So basically the program achieves nothing apart from inducing a severe headache upon myself. I've only been learning c++ a week or so, so please forgive any simple errors I've made. Can someone please point me in the direction of my error, or suggest any ways of actually making this work!?! Any comments much appreciated, thanks, peace.
• 10-13-2005
Decrypt
Entering -1 doesn't work because the do while loop doesn't check until the end of the loop. It doesn't monitor it the whole time. If you don't want to input all of that if the user enters -1 for the account number (and you don't) you have to check whether or not they entered -1 before the program goes on to inputting everything else.

The balance formula as written works fine: 5394.78 + 500 - 1000 = 4894.78

If you're looking for a different answer, check to make sure the formula is correct...
• 10-13-2005
vaibhav
Just ask user whether to continue or not in last because do while loop first runs the loop and then it check the condition.If condition is true then it again runs the loop.If you want to use your code as it is use while loop.
Code:

```while(accountnum!=-1) { //Your code }```
• 10-13-2005
Decrypt
After further review, the call on the field stands, with the addition of:

I think your credit limit check is wrong. This:
Code:

```if (balance > creditLimit)   cout << "\nCredit Limit not exceeded!\n";  else   cout << "\nCredit Limit exceeded!\n";```
should be:

Code:

```if (balance < creditLimit)   cout << "\nCredit Limit not exceeded!\n";  else   cout << "\nCredit Limit exceeded!\n";```
• 10-13-2005
Decrypt
Quote:

Originally Posted by vaibhav
Just ask user whether to continue or not in last because do while loop first runs the loop and then it check the condition.If condition is true then it again runs the loop.If you want to use your code as it is use while loop.
Code:

```while(accountnum!=-1) { //Your code }```

Actually, given OP code, that won't work either. If accountNumber is the first thing asked for within the loop, it will still run through the whole loop before checking again. Doing that isn't really any different from the do...while loop.

The OP will have to change the structure of the program slightly if he/she wants to exit immediately after entering -1 for the account number.
• 10-13-2005
vaibhav
i am sorry while loop will also never work as it is.You may put some condition after inputting accountnum
Code:

```do { cin>>accountnum; if(accountnum==-1) break; //Further code }while(1);```
• 10-13-2005
Enahs

You want to add the additional charges (since this is a record of debit) and subtract the credit (since that is like paying off debit).

```if ( accountNumber != -1 ) { your main code } else {   cout << "you have chosen to exit";   }```