Thread: How to do this

  1. #1
    Registered User cyberCLoWn's Avatar
    Join Date
    Dec 2003
    Location
    South Africa
    Posts
    124

    How to do this

    Here is the code I have written so far. It's just an exercise but there is one thing I'm struggling with.

    Code:
    // Practise Exercise
    
    #include <iomanip>
    #include <iostream> 
    using namespace std;
    
    int main()
    {  
       int acc_number;
       float Bbalance;
       float charges;
       float credits;
       float limit;
       float Ebalance;
       
       while ( acc_number != -1 )
       {
          // Get the info
          cout << "Enter account number (-1 to end): ";
          cin >> acc_number;
       
          cout << "Enter beginning balance: ";
          cin >> Bbalance;
       
          cout << "Enter total charges: ";
          cin >> charges;
       
          cout << "Enter total credits: ";
          cin >> credits;
       
          cout << "Enter credit limit: ";
          cin >> limit;
       
          // Calculation
          Ebalance = Bbalance + charges - credits;
       
          // if credit limit is exceeded display the following   
          if ( Ebalance > limit )
          {      
                cout << fixed << setprecision( 2 );
             
                cout << "Account:\t\t" << acc_number << '\n'
                     << "Credit limit:\t\t" << limit << '\n'
                     << "Balance:\t\t" << Ebalance << '\n'
                     << "Credit Limit Exceeded.\n" << endl;
          }
          else
                cout << endl;
       }   
       
       cin.get();
       return 0;
    }
    Now if you'll turn your attention to the while loop you'll notice the condition. For the loop to break a person needs to input -1 into the int acc_number. The thing is, this is only tested at the start of the loop which means that if a user inputs -1 the rest of the loop will continue and will only break once it's reached the end and checks the condition. I've sat for a while trying to figure out how to work around this, but the exercise states I need to use a while loop. I need the loop to break the second -1 is entered by the user into acc_number. Any suggestions?

  2. #2
    Registered User
    Join Date
    Dec 2003
    Posts
    101
    Code:
     .......
    while ( acc_number != -1 )
       {
          // Get the info
          cout << "Enter account number (-1 to end): ";
          cin >> acc_number;
          //you may be able to just put 'continue;' right here and take out the conditional statement.. I'm at the wrong computer, no compiler
          if (acc_number == -1)
             break;  //exits the loop
       .......
    Try the 'continue;' and see if that works, it's not necisarally faster because continue checks the while loop's parameters but it looks beter, and it's easier to manage.
    Last edited by 1veedo; 12-26-2003 at 04:42 PM.
    --

  3. #3
    Registered User cyberCLoWn's Avatar
    Join Date
    Dec 2003
    Location
    South Africa
    Posts
    124
    Code:
       while ( acc_number != -1 )
       {
          // Get the info
          cout << "Enter account number (-1 to end): ";
          cin >> acc_number;
          
          if ( acc_number == -1 )
             break;
    I had thought of doing something like that, but then it makes the while's condition a bit pointless. Is this the way to do it or can anyone think of something better?

  4. #4
    Registered User
    Join Date
    Sep 2003
    Posts
    135
    Code:
    #include <iomanip>
    #include <iostream> 
    using namespace std;
    
    int main()
    {  
       int acc_number;
       float Bbalance;
       float charges;
       float credits;
       float limit;
       float Ebalance;
       
       // Get the info
       cout << "Enter account number (-1 to end): ";
       cin >> acc_number;
    
       while ( acc_number != -1 )
       {
          cout << "Enter beginning balance: ";
          cin >> Bbalance;
       
          cout << "Enter total charges: ";
          cin >> charges;
       
          cout << "Enter total credits: ";
          cin >> credits;
       
          cout << "Enter credit limit: ";
          cin >> limit;
       
          // Calculation
          Ebalance = Bbalance + charges - credits;
       
          // if credit limit is exceeded display the following   
          if ( Ebalance > limit )
          {      
                cout << fixed << setprecision( 2 );
             
                cout << "Account:\t\t" << acc_number << '\n'
                     << "Credit limit:\t\t" << limit << '\n'
                     << "Balance:\t\t" << Ebalance << '\n'
                     << "Credit Limit Exceeded.\n" << endl;
          }
          else
                cout << endl;
    
          // Get the info
          cout << "Enter account number (-1 to end): ";
          cin >> acc_number;
       
       }   
       
       cin.get();
       return 0;
    }

  5. #5
    Registered User cyberCLoWn's Avatar
    Join Date
    Dec 2003
    Location
    South Africa
    Posts
    124
    Sometimes I just wanna smack myself with something hard. Thanks so much Omnibus. I'm working through some exercises and it's late. Such a logical thing and I didn't think of it. At least I know now for the future!

Popular pages Recent additions subscribe to a feed