Thread: Do While Loop Problem

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    9

    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. #2
    Moderately Rabid Decrypt's Avatar
    Join Date
    Feb 2005
    Location
    Milwaukee, WI, USA
    Posts
    300
    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...
    There is a difference between tedious and difficult.

  3. #3
    Registered User
    Join Date
    Aug 2005
    Posts
    113
    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
    }

  4. #4
    Moderately Rabid Decrypt's Avatar
    Join Date
    Feb 2005
    Location
    Milwaukee, WI, USA
    Posts
    300
    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";
    There is a difference between tedious and difficult.

  5. #5
    Moderately Rabid Decrypt's Avatar
    Join Date
    Feb 2005
    Location
    Milwaukee, WI, USA
    Posts
    300
    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.
    Last edited by Decrypt; 10-13-2005 at 09:00 AM.
    There is a difference between tedious and difficult.

  6. #6
    Registered User
    Join Date
    Aug 2005
    Posts
    113
    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);

  7. #7
    ^ Read Backwards^
    Join Date
    Sep 2005
    Location
    Earth
    Posts
    282
    Your math is wrong.

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

    Read other post for why your loop trouble is not working.

  8. #8
    Registered User
    Join Date
    Oct 2005
    Posts
    9
    Got it to work! Thanks ya'll, appreciate everyone's help. I shall now punch myself furiously for being so dumb haha. peace!

  9. #9
    Registered User
    Join Date
    Oct 2005
    Posts
    10
    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 ) {
    
    your main code
    
    }
    else {
    
      cout << "you have chosen to exit";
      
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Addition problem in loop
    By murjax in forum C Programming
    Replies: 3
    Last Post: 07-01-2009, 06:29 PM
  2. validation problem in a loop (newbie question)
    By Aisthesis in forum C++ Programming
    Replies: 11
    Last Post: 05-10-2009, 10:47 PM
  3. For Loop Problem
    By xp5 in forum C Programming
    Replies: 10
    Last Post: 09-05-2007, 04:37 PM
  4. Loop problem
    By Tesnik in forum C++ Programming
    Replies: 29
    Last Post: 08-23-2007, 10:24 AM
  5. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM