# Thread: Do While Loop Problem

1. ## 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.

2. 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...

3. 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)
{
}```

4. 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";```

5. 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)
{
}```
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.

6. 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);```

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).

8. Got it to work! Thanks ya'll, appreciate everyone's help. I shall now punch myself furiously for being so dumb haha. peace!

9. It seems to me that when your program starts it jumps straight into the loop not taking into account any user input. You need the user input first so you can make a decision based upon that input.

Try something like,

Code:
```if ( accountNumber != -1 ) {