Problem ending loop

This is a discussion on Problem ending loop within the C++ Programming forums, part of the General Programming Boards category; I have written a program for updating a customer's account balance and am having a problem ending the looping part ...

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    14

    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. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    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. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Code:
    while( accountNumber != -1 )
    Enter -1 for the account number?

  4. #4
    User
    Join Date
    Jan 2006
    Location
    Canada
    Posts
    498
    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. #5
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    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.
    No No's:
    fflush (stdin); gets (); void main ();


    Goodies:
    Example of fgets (); The FAQ, C/C++ Reference


    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition


    ASCII stupid question, get a stupid ANSI

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. Loop problem
    By Tesnik in forum C++ Programming
    Replies: 29
    Last Post: 08-23-2007, 10:24 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. While loop problem
    By nadkingcole in forum C Programming
    Replies: 2
    Last Post: 05-09-2002, 06:14 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21