Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Dec 11th, 2006, 10:39 PM   #1
amg11872
Newbie
 
Join Date: Dec 2006
Posts: 3
Rep Power: 0 amg11872 is on a distinguished road
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.
amg11872 is offline   Reply With Quote
Old Dec 11th, 2006, 11:03 PM   #2
Jimbo
Battle Programmer
 
Jimbo's Avatar
 
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 754
Rep Power: 3 Jimbo is on a distinguished road
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>
Jimbo is offline   Reply With Quote
Old Dec 11th, 2006, 11:48 PM   #3
amg11872
Newbie
 
Join Date: Dec 2006
Posts: 3
Rep Power: 0 amg11872 is on a distinguished road
To output the information do I need to include it in the while loop or before.
amg11872 is offline   Reply With Quote
Old Dec 12th, 2006, 12:18 AM   #4
Jimbo
Battle Programmer
 
Jimbo's Avatar
 
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 754
Rep Power: 3 Jimbo is on a distinguished road
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.
__________________
<insert disclaimer here>
<insert shameless plug for Visual Studio here>
Jimbo is offline   Reply With Quote
Old Dec 12th, 2006, 6:21 AM   #5
amg11872
Newbie
 
Join Date: Dec 2006
Posts: 3
Rep Power: 0 amg11872 is on a distinguished road
Thanks so much. I made the changes that you suggested and now it is running right.
amg11872 is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 4:35 AM.

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