GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 23-Sep-2008, 11:33
mrpenguin mrpenguin is offline
New Member
 
Join Date: Sep 2008
Posts: 1
mrpenguin is on a distinguished road

Show interest payments on a declining balance


Ok so I made this code to show interest payments on a declining balance and it seems to work correctly the first time. But if you choose to run it again with the same input, the output is different.

Here's the code:
CPP / C++ / C Code:
#include <iostream>
using namespace std;

int main(){

	double loan_amt, int_rate, loan, monthly, loan_int, total_int = 0, op = 0, time = 0, percent;
	char ans;

	cout.setf(ios::fixed);
	cout.setf(ios::showpoint);
	cout.precision(2);
	

	do {
		
		//input
	cout << "Input the loan amount: ";
	cin >> loan_amt;
	loan=loan_amt;
	cout << "Input the interest rate: ";
	cin >> int_rate;

//get base monthly payment
monthly = loan_amt/20;

cout << "Interest  Subtracted  Remaining\n";
cout << "__________________________________\n";
do {
	//add 1 to number of months to pay off
	time+=1;

//get int payed
loan_int = (loan*int_rate)/12;
//add this month's interest to the total payed
total_int += loan_int;
//subtract remainder from loan
loan -= (monthly-loan_int);

//check to see if you over-payed
if (loan < 0) {
	//if so get rid of the extra pay and set the remaining loan to 0
	op = (-loan)+loan_int;
	loan = 0;
}


cout <<"$"<< loan_int << "    $" << monthly-loan_int-op << "    $" << loan << endl;
}while (loan > 0 );

cout << "__________________________________\n";
//show total interest
cout << "Total interest -- $" << total_int << endl;

//set time in years
time /= 12;
// set interest in years
total_int /= time;
// calc annual percent interest rate
percent = (total_int*100)/loan_amt;

cout << "Annual % rate -- " << percent << "%" << endl;

//reset vars if repeated
		time = 0;
		total_int = 0;
		op = 0;
		monthly = 0;


//run again?
	cout << "Run again?(y/n):";
	cin >> ans;

	}while(ans == 'y' || ans == 'Y');
	return 0;

}

example input is:
loan amount = 20,000
rate = .1 (for 10%)

first time it gives me about 5% annual interest and second time its about 7%. I can't figure out which variables are messing it up the second time around.

thanks in advance.
  #2  
Old 24-Sep-2008, 03:06
LuciWiz's Avatar
LuciWiz LuciWiz is offline
Moderator
 
Join Date: Jul 2004
Location: Cluj-Napoca (Romania)
Posts: 1,032
LuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the rough

Re: Show interest payments on a declining balance


Quote:
Originally Posted by mrpenguin
first time it gives me about 5% annual interest and second time its about 7%. I can't figure out which variables are messing it up the second time around.

Scanning through the code, the first obvious problem is that you initialize the variables total_int and time to 0 when declaring them and then use them with the assumption that they are 0.

On the second run of the inner loop they will be modified and the residual value will be used in the computation. As a resolution, make sure you initialize the variables before each run of the computation (in the outer loop).

Best regards,
Lucian
__________________
Please read these Guidelines before posting on the forum

"A person who never made a mistake never tried anything new."
Einstein
 
 

Recent GIDBlogAccepted for Ph.D. program by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

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

vB 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
Classic recursion game of 8 Queens errors BKizzle77 Java Forum 11 11-Aug-2008 01:35
Exiting an infinite loop. memmudu C++ Forum 1 31-Dec-2007 07:25
Problem with c++ program do while loop batalia C++ Forum 3 04-Feb-2007 11:21
Text-Based Roulette Game mfm1983 C++ Forum 5 29-Nov-2006 13:20

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 02:12.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.