6 Replies - 2036 Views - Last Post: 18 February 2008 - 07:50 AM Rate Topic: -----

#1 sustephe   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 11-February 08

Help with amortizatin program

Post icon  Posted 16 February 2008 - 07:40 PM

Hello all,

I have spent a bit of time without much sucess on this program. I would appreciate any help you can offer.

The odd thing is the output from the build does not match the lines and that makes it even more confusing. The formulas;

interest_payment = current_principal * monthly_interest_rate;
current_principal = current_principal - (monthly_payment - interest_payment);

are from the instructor...in case you wonder where they came from. He also wants us to insert the code that is associated with the pseudocode...

if ((month % 12 == 0) & (month+1 < term * 12))

prompt the user whether to show more data...
if user says no
break from the loop

endif
endif

Which has me at a loss.

Susan
//-----------------------------------------
//Course: PRG/410 - C++ Programming
//Author: Susan R. Stephenson
//Date: February 18, 2008
//Title: Mortgage Calclator Week 4 Individual Assignement
//------------------------------------------

//C++ program enhancement #2 (15 points). Modify the mortgage program to input 
//the amount of the mortgage, the term of the mortgage, and the interest rate 
//of the mortgage. Display the mortgage payment amount. Then, list the loan 
//balance, and interest paid for each payment over the term of the loan.  On
//longer loans, the list will scroll off the screen. Display a partial list and
//give the user the option to continue or exit. Allow the user to loop back and 
//enter new data or quit. Insert meaningful comments at least once for every few 
//lines of the program. Run your program for the following input data & capture 
//the output: ($50000, 2 years, 7%) followed by ($1000000, 10 years, 8%) followed
//by ($10000, 1 year, 6%). Submit the source code and the program output to 
//Individual forum.

#include <cmath>
#include <iostream>
using namespace std;

float A = 0; //Total loan amount
float i = 0.00; //Loan interest rate
int y = 0; // Length of loan in years
int quit = 1; // loop function if user wants to quit
float pay = 0; // Calculation of payment amount

int main()
{
do
{
	cout << "Enter total loan amount without commas and press Enter:\n"; //Prompt the user & get the mortgage amount as input

	cin >> A;

	cout << "Enter loan years and press Enter:\n";// Prompt the user and get the # of years as input

	cin >> y;
	
	cout << "Enter interest rate in 0.0000 format and press Enter:\n"; //Prompt the user a & get the interest rate as input

	cin >> i;

	pay =(A*((i/12)/(1- pow((1+(i/12)),-(y*12))))); //Apply the formula to compute the monthly mortage payment

	
	cout << "The payment is: $"<< pay<<"\n";//Output the monthly mortgage payment amount

	double principal; //amortization table
	double totalInterestPaid;
	double balance;
	double interest_payment;
	double current_principal;
	double monthly_interest_rate;
	double monthly_payment;
	double numberOfPayments;

	totalInterestPaid = 0; //assign values

	//title for table
	cout<<"Balance******Interest Paid******Remaining Payments"<< endl;
	cout<< endl;

	do 
	{	//Calculations to perform
		interest_payment = current_principal * monthly_interest_rate;
		current_principal = current_principal - (monthly_payment - interest_payment);
		balance = balance - principal;
		numberOfPayments = numberOfPayments - 1;

		for (int numberOfPayments = 0; numberOfPayments >0; numberOfPayments++)

		//set up table
		cout.width(7);
		cout<<balance<<"";
		cout.width (20);
		cout<<totalInterestPaid<<"";
		cout.width(22);
		cout<<endl;
	}
		while (numberOfPayments>0);

		if (numberOfPayments*12==0);

	cout << "Select 1 to enter different values or 2 to quit\n";//Prompt user whether (s)he has another set of data & get the input
	
	cout<<"Thank you!\n"; //Thank the user and quit

	cin >> quit; 

} while (quit==1);
return 0;
}


output

----- Build started: Project: Week04, Configuration: Debug Win32 ------
Compiling...
004b.cpp
c:\documents and settings\susan\my documents\visual studio 2008\projects\week04\week04\004b.cpp(87) : warning C4390: ';' : empty controlled statement found; is this the intent?
04c.cpp
c:\documents and settings\susan\my documents\visual studio 2008\projects\week04\week04\04c.cpp(53) : error C2065: 'totalTerm' : undeclared identifier
c:\documents and settings\susan\my documents\visual studio 2008\projects\week04\week04\04c.cpp(55) : warning C4091: '' : ignored on left of 'char' when no variable is declared
c:\documents and settings\susan\my documents\visual studio 2008\projects\week04\week04\04c.cpp(55) : error C2143: syntax error : missing ';' before 'constant'
c:\documents and settings\susan\my documents\visual studio 2008\projects\week04\week04\04c.cpp(56) : warning C4091: '' : ignored on left of 'char' when no variable is declared
c:\documents and settings\susan\my documents\visual studio 2008\projects\week04\week04\04c.cpp(56) : error C2143: syntax error : missing ';' before 'constant'
c:\documents and settings\susan\my documents\visual studio 2008\projects\week04\week04\04c.cpp(59) : error C2065: 'mortgageAmount' : undeclared identifier
c:\documents and settings\susan\my documents\visual studio 2008\projects\week04\week04\04c.cpp(67) : error C2065: 'interest_payment' : undeclared identifier
c:\documents and settings\susan\my documents\visual studio 2008\projects\week04\week04\04c.cpp(67) : error C2065: 'current_principal' : undeclared identifier
c:\documents and settings\susan\my documents\visual studio 2008\projects\week04\week04\04c.cpp(67) : error C2065: 'monthly_interest_rate' : undeclared identifier
c:\documents and settings\susan\my documents\visual studio 2008\projects\week04\week04\04c.cpp(68) : error C2065: 'current_principal' : undeclared identifier
c:\documents and settings\susan\my documents\visual studio 2008\projects\week04\week04\04c.cpp(68) : error C2065: 'current_principal' : undeclared identifier
c:\documents and settings\susan\my documents\visual studio 2008\projects\week04\week04\04c.cpp(68) : error C2065: 'monthly_payment' : undeclared identifier
c:\documents and settings\susan\my documents\visual studio 2008\projects\week04\week04\04c.cpp(68) : error C2065: 'interest_payment' : undeclared identifier
c:\documents and settings\susan\my documents\visual studio 2008\projects\week04\week04\04c.cpp(70) : error C2065: 'monthlyInterest' : undeclared identifier
c:\documents and settings\susan\my documents\visual studio 2008\projects\week04\week04\04c.cpp(73) : error C3861: 'setprecision': identifier not found
c:\documents and settings\susan\my documents\visual studio 2008\projects\week04\week04\04c.cpp(81) : error C2039: 'witdth' : is not a member of 'std::basic_ostream<_Elem,_Traits>'
with
[
_Elem=char,
_Traits=std::char_traits<char>
]
c:\documents and settings\susan\my documents\visual studio 2008\projects\week04\week04\04c.cpp(87) : warning C4390: ';' : empty controlled statement found; is this the intent?
c:\documents and settings\susan\my documents\visual studio 2008\projects\week04\week04\04c.cpp(98) : fatal error C1075: end of file found before the left brace '{' at 'c:\documents and settings\susan\my documents\visual studio 2008\projects\week04\week04\04c.cpp(31)' was matched
Week04.cpp
4D.cpp
c:\documents and settings\susan\my documents\visual studio 2008\projects\week04\week04\4d.cpp(87) : warning C4390: ';' : empty controlled statement found; is this the intent?
Generating Code...
c:\documents and settings\susan\my documents\visual studio 2008\projects\week04\week04\4d.cpp(68) : warning C4700: uninitialized local variable 'current_principal' used
c:\documents and settings\susan\my documents\visual studio 2008\projects\week04\week04\4d.cpp(68) : warning C4700: uninitialized local variable 'monthly_interest_rate' used
c:\documents and settings\susan\my documents\visual studio 2008\projects\week04\week04\4d.cpp(69) : warning C4700: uninitialized local variable 'monthly_payment' used
c:\documents and settings\susan\my documents\visual studio 2008\projects\week04\week04\4d.cpp(70) : warning C4700: uninitialized local variable 'balance' used
c:\documents and settings\susan\my documents\visual studio 2008\projects\week04\week04\4d.cpp(70) : warning C4700: uninitialized local variable 'principal' used
c:\documents and settings\susan\my documents\visual studio 2008\projects\week04\week04\4d.cpp(71) : warning C4700: uninitialized local variable 'numberOfPayments' used
c:\documents and settings\susan\my documents\visual studio 2008\projects\week04\week04\004b.cpp(68) : warning C4700: uninitialized local variable 'current_principal' used
c:\documents and settings\susan\my documents\visual studio 2008\projects\week04\week04\004b.cpp(68) : warning C4700: uninitialized local variable 'monthly_interest_rate' used
c:\documents and settings\susan\my documents\visual studio 2008\projects\week04\week04\004b.cpp(69) : warning C4700: uninitialized local variable 'monthly_payment' used
c:\documents and settings\susan\my documents\visual studio 2008\projects\week04\week04\004b.cpp(70) : warning C4700: uninitialized local variable 'balance' used
c:\documents and settings\susan\my documents\visual studio 2008\projects\week04\week04\004b.cpp(70) : warning C4700: uninitialized local variable 'principal' used
c:\documents and settings\susan\my documents\visual studio 2008\projects\week04\week04\004b.cpp(71) : warning C4700: uninitialized local variable 'numberOfPayments' used
Build log was saved at "file://c:\Documents and Settings\Susan\My Documents\Visual Studio 2008\Projects\Week04\Week04\Debug\BuildLog.htm"
Week04 - 15 error(s), 17 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

Please use code tags: :code:

Is This A Good Question/Topic? 0
  • +

Replies To: Help with amortizatin program

#2 KYA   User is offline

  • Wubba lubba dub dub!
  • member icon

Reputation: 3213
  • View blog
  • Posts: 19,241
  • Joined: 14-September 07

Re: Help with amortizatin program

Posted 17 February 2008 - 02:46 AM

I'll read over this again, but my first over I noticed a few things:

You have declared variables inside a do/while loop. This defines their scope to only this loop, which may not be necessarily bad, but is bad programming practice in my opinion. I also do not care for the names of some of the variables. Underscores should be used for macros or other special instance. I'm running your code now to see what's going on. :)


edit: ACK! There are several places where there are no braces to inclose multistatement loops and there are semicolons after if statements.

edit2: There is one case so far where you intialize numberOfPayments to zero, then user a for loop while numberofPayments is less then zero. That for loop will never be executed.

edit3: All of the variable orignally initialized inside the loop here:

	double principal; //amortization table
	double totalInterestPaid;
	double balance;
	double interest_payment;
	double current_principal;
	double monthly_interest_rate;
	double monthly_payment;
	double numberOfPayments;



Are not assigned any values at all...anywhere

So i'm confused by this:

 //Calculations to perform
 interest_payment = current_principal * monthly_interest_rate;
 current_principal = current_principal - (monthly_payment - interest_payment);
balance = balance - principal;
numberOfPayments = numberOfPayments - 1;



How are these numbers being calculated without any numbers to begin with? Also interest payment depends on current principal to perform its function, but current principal hasn't be calculated yet. so on and so forth down the line. Some more specific information about your assignment would help, or rather, what you are going for.

--kya

This post has been edited by KYA: 17 February 2008 - 02:53 AM

Was This Post Helpful? 0
  • +
  • -

#3 sustephe   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 11-February 08

Re: Help with amortizatin program

Posted 17 February 2008 - 07:32 AM

We are supposed to create a program where we can enter the loan amount, term and interest rate. Once the payment had been displayed, then an amortization table displays.

The formulas are ones that the instructor provided for us to include in the program. My hubby thought they were akward too. Last week I was docked points because I did not follow directions, I was trying to get everything in he is asking for without much luck. Right now I would just like to get the program to run.

Thanks for the help. I was getting pretty frustrated with this.

Susan
Was This Post Helpful? 0
  • +
  • -

#4 sustephe   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 11-February 08

Re: Help with amortizatin program

Posted 17 February 2008 - 02:28 PM

I decided that yesterday I was going up the wrong path and revamped the code I was working on. Still a gazillion errors and it will not run....help! :P :rolleyes:

#include <cmath>
#include <iostream>
#include <iomanip>
#include <cctype>

using namespace std;
using std::cout;

int main()
{
float A = 0; //Total loan amount
float i = 0.00; //Loan interest rate
int y = 0; // Length of loan in years
int quit = 1; // loop function if user wants to quit
float pay = 0; // Calculation of payment amount
float month = 0;//Addition of term
float term = 0;
double principal; //amortization table
double interestPayment;
double currentPrincipal;
double interestRate;
double monthlyInterestRate, monthlyRate;
double monthlyPayment;
double totalMonths;
double years;
double mortgage;
char more_data = 'y';
char exit = 'n';

while (toupper(more_data) == 'Y')
{
cout << "Enter total loan amount without commas and press Enter:\n"; //Prompt the user & get the mortgage amount as input

cin >> A;

cout << "Enter loan years and press Enter:\n";// Prompt the user and get the # of years as input

cin >> y;

cout << "Enter interest rate in 0.0000 format and press Enter:\n"; //Prompt the user a & get the interest rate as input

cin >> i;

monthlyRate = interestRate / 1200; //compute monthly interest rate
totalMonths = years * 12; //compute # of months

pay =(A*((i/12)/(1- pow((1+(i/12)),-(y*12))))); //Apply the formula to compute the monthly mortage payment

cout << "The payment is: $"<< mortgage<< "for"<<years<<"years @"<<interestRate<<"%"<<endl;//Output the monthly mortgage payment amount
int count = 0 ;

cout << "The payment is: $"<< pay<<"\n";//Output the monthly mortgage payment amount
cout<< "Your monthly payment will be:";
cout<<fixed<<setprecision(2)<<monthlyPayment<<endl;

//title for table
cout<<"Balance******Interest Paid******Remaining Payments"<< endl;
cout<< endl;
// for every month
for (count = 1; count <= 10; count = count +1)
{
do
//Calculations to perform
interestPayment = currentPrincipal * monthlyInterestRate;
currentPrincipal = currentPrincipal - (monthlyPayment - interestPayment);

cout << count << endl ;
}
// end for

return 0 ;
}

// nested if
if '(number < 0)';
{
cout << "Number is negative" ;
}

if ((month == term *12));//Break from loop
{
cout << "Do you want to continue?";
}

}
// end if


cout << "Select y to enter different values or n to quit;";//Prompt user whether (s)he has another set of data & get the input
cin >> more_data;
cout ;<< "Thank you!\n"; //Thank the user and quit

cin ;>> quit;

{ while (quit==1);
return 0;
Was This Post Helpful? 0
  • +
  • -

#5 KYA   User is offline

  • Wubba lubba dub dub!
  • member icon

Reputation: 3213
  • View blog
  • Posts: 19,241
  • Joined: 14-September 07

Re: Help with amortizatin program

Posted 17 February 2008 - 06:09 PM

use code tags please :)
Was This Post Helpful? 0
  • +
  • -

#6 sustephe   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 11-February 08

Re: Help with amortizatin program

Posted 18 February 2008 - 07:23 AM

Umm....I don't know what code tags are.

Susan

View PostKYA, on 17 Feb, 2008 - 06:09 PM, said:

use code tags please :)

Was This Post Helpful? 0
  • +
  • -

#7 Amadeus   User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 253
  • View blog
  • Posts: 13,507
  • Joined: 12-July 02

Re: Help with amortizatin program

Posted 18 February 2008 - 07:50 AM

It refers to encasing your code in the following tags:

[ code ] your code here [/ code ]

Without the spaces, of course.

Can you post the errors you are receiving? At the very least, I notice the following:
do
//Calculations to perform
interestPayment = currentPrincipal * monthlyInterestRate;
currentPrincipal = currentPrincipal - (monthlyPayment - interestPayment);


This appears to be the beginning of a do while loop - but there does not seem to a be while portion, as the for loop closes immediately after this portion.
}
// end for

return 0;
}


This will close the for loop and exit the program. anything after this will not be executed.

cout << "Select y to enter different values or n to quit;";//Prompt user whether (s)he has another set of data & get the input
cin >> more_data;
cout;<< "Thank you!\n"; //Thank the user and quit

cin;>> quit;

{ while (quit==1);
return 0;


that section of code is entirely outside of your main function. that aside, it has the following further problems:
cout;<< "Thank you!\n"; //Thank the user and quit

cin;>> quit;


A semi colon should not be used immediately after the cin and cout statements
There is an opening brace before the while statement that has no purpose, nor does it have a corresponding closing brace.
A while staement does not end with a semi colon, unless it is a do-while construct.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1