Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C++ (http://www.programmingforums.org/forum15.html)
-   -   Trouble with while loop. (http://www.programmingforums.org/showthread.php?t=12167)

amg11872 Dec 11th, 2006 11:39 PM

Trouble with while loop.
 
Hi. I am new to this forum. I have written a program to calculate the outstanding balance, interest payment, and principal payment for a loan. When I compile it does not display any results. I believe the error is in my while loop but I have not had any luck locating the error.

:

int main()
{
        float OUTSTANDING_BAL = 80000.000000;
        const float MONTHLY_PYMT = 300.000000;
        float principal;
        float monInt;
        float balance;
                                   
        cout << "Beginning      Interest        Principal      EndingLoan" << endl;
        cout << "Payment        Payment        Balance        Balance" << endl;
        cout << "_ _ _ _ _      _ _ _ _ _      _ _ _ _ _      _ _ _ _ _ _" << endl;
       
        balance = OUTSTANDING_BAL;

        while (balance <= 80000.000000)
        {
                balance = balance - MONTHLY_PYMT;
                monInt = balance * (0.10/12);

                if (balance < MONTHLY_PYMT)
                        principal = balance;
                else
                        principal = MONTHLY_PYMT - monInt;

                balance= balance - principal;
                                                                     
                cout << setiosflags(ios::fixed)
                        << setiosflags(ios::showpoint)
                        << setw(11) << setprecision(6);
        }
        return 0;


I would appreciate any help that I can get.

Jimbo Dec 12th, 2006 12:03 AM

So, in your loop, you have the cout with lots of formatting stuff, but you don't actually output anything. I'm guessing that's your problem.

Side note: when copmaring floating point numbers, using < and > is a bit risky, since they aren't stored precisely in memory. It is possible that 80000.0 on some machines may be stored as slightly larger or smaller, which could result in the loop never being run. One way around this is to use a tolerance level, e.g.
:

double epsilon = 0.05; // tolerance level
if(balance <= 80000.00 + epsilon)
// or for equality
if(fabs(balance - 80000.00) < epsilon)


PS - Welcome to the forum :D

amg11872 Dec 12th, 2006 12:48 AM

To output the information do I need to include it in the while loop or before.

Jimbo Dec 12th, 2006 1:18 AM

You need to cout the data. Like this:
:

    cout << setiosflags(ios::fixed)
      << setiosflags(ios::showpoint)
      << setw(11) << setprecision(2);
    cout << MONTHLY_PYMT;
    cout << setw(12) << monInt ; // repeatedly set the width
    cout << setw(13) << principal ; // for each field
    cout << setw(17) << balance << endl; // and a newline at the end

This should be in the loop like it already is. You can play with the widths a bit to get 'em how you want 'em to look. Also, I noticed that once your balance is 0, you'll still loop. You should put another condition to prevent an infinite loop.

amg11872 Dec 12th, 2006 7:21 AM

Thanks so much. I made the changes that you suggested and now it is running right.


All times are GMT -5. The time now is 1:39 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC