![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Dec 2006
Posts: 3
Rep Power: 0
![]() |
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. |
|
|
|
|
|
#2 |
|
Battle Programmer
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 754
Rep Power: 3
![]() |
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 ![]()
__________________
<insert disclaimer here> <insert shameless plug for Visual Studio here> |
|
|
|
|
|
#3 |
|
Newbie
Join Date: Dec 2006
Posts: 3
Rep Power: 0
![]() |
To output the information do I need to include it in the while loop or before.
|
|
|
|
|
|
#4 |
|
Battle Programmer
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 754
Rep Power: 3
![]() |
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
__________________
<insert disclaimer here> <insert shameless plug for Visual Studio here> |
|
|
|
|
|
#5 |
|
Newbie
Join Date: Dec 2006
Posts: 3
Rep Power: 0
![]() |
Thanks so much. I made the changes that you suggested and now it is running right.
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Beginner's trouble with software delays (TCNT) | JoeSmith | Assembly | 0 | May 12th, 2005 12:26 AM |
| Help in QBASIC (I think it's similar to VB) | phoenix987 | Visual Basic | 3 | May 9th, 2005 12:33 PM |
| Help with a QBASIC program | phoenix987 | Other Programming Languages | 4 | May 5th, 2005 12:27 PM |
| WinSock accept() hangs program in Do loop... | layer | C++ | 5 | Apr 29th, 2005 11:28 AM |
| Timing loop problems | badbasser98 | C++ | 11 | Mar 10th, 2005 8:30 PM |