Solved

# Can anyone tell me what is wrong with my program in order for it to work in c++?

Posted on 2006-03-20
266 Views
/*
Program Calculator Week3 Individual Assignment
Version 2.2
Date: March 17, 2006
Display the amount of monthly payment amount for a mortgage payment enter by user to display loan amount, interest rate, and term of loan.
*/
#include <math.h>
#include <string>
#include <iostream>

using namespace std

int main()
{
// Set variables use for mortgage payment
double amount;                // Loan amount
int term;                     // Loan term
int month = 12;               // 1 year = 12 months
int term_months;              // Loan term in month
double rate;                  // Loan interest rate
int choice;                   // Used for choice dialog
double monthly_payment;       // Variable for monthly payment
double term_rate;             // Holds percentage as a decimal

cout << "Enter the loan amount in dollars: ";
cin >> amount;

cout << "Enter the loan term in years: ";
cin >> term;

cout << "Enter the interest rate as a percentage: ";
cin >> rate;

term_months = (term*12);    //Converts years to months
term_rate = (rate/100);         // Converts Percentage to Decimal

double amortization_factor = 1 + (rate / 100 / 12);
monthly_payment = amount * (amortization_factor - 1.0) / (1.0 - pow(amortization_factor, -term_months));

cout <<  "Your Mortgage amount is\n";
cin <<  "Amount: \$ " <<  amount << endl;                   //Prints out Amount
cout <<  "Your Mortgage is being taken out for" << endl;
cin <<  "Years: " << term << endl;                         //Prints out Term
cout <<  "The interest rate is\n";
cin <<  "Percent: " << rate << endl;                       //Prints out Rate
cout <<  "Your Monthly Payment Amount is " << endl;
cin <<  "Payment Amount: \$ " << monthly_payment <<endl;    //Prints out monthly payment

//Allows the user to end or start over

char ans;
do
{
cout<< "Do you want to continue (Y/N)?\n";
cout<< "You must type a 'Y' or an 'N'.\n";
cin >> ans;
}
while((ans !='Y')&&(ans !='N')&&(ans !='y')&&(ans !='n'));

/*the loop continues until the user enters the correct response*/
0
Question by:Miliano

LVL 2

Expert Comment

I don't know about the math part, but I see two problems:
1) You missed a ";" after "using namespace std"
2) The last four lines starting with "cin" should be changed to "cout"
0

Author Comment

I have corrected the information you provided but I am still having major issues with this program.  Help anyone
0

LVL 2

Expert Comment

0

LVL 8

Expert Comment

I agree. I made the changes kjpus suggested, and it compiles with a single warning (the variable choice isn't used).

What happens when you compile? What are you compiling with?

--
Troy
0

Author Comment

The only problem that I am getting now is that my math calculation does not appear to be working correctly after making all the changes that has been mention.  Here is my code as of today.

#include <math.h>
#include <string>
#include <iostream>
using namespace std;

int main()
{
// Local variable declarations of main
double amount;                // Loan amount
int term;                        // Loan term
int month = 12;               // 1 year = 12 months
int term_months;              // Loan term in month
double rate;                  // Loan interest rate
char choice;                   // Used for choice dialog
double monthly_payment;       // Variable for monthly payment
double term_rate;             // Holds percentage as a decimal

// Introduction:
// Display the title and the purpose of the program to the user
do {
cout << "Enter the loan amount in dollars: ";
cin >> amount;

cout << "Enter the loan term in years: ";
cin >> term;

cout << "Enter the interest rate as a percentage: ";
cin >> rate;

// Input Module
// This module asks the user to enter in the amounts for the principal,
// annual interest rate and the number of years.

term_months = (term*12);    //Converts years to months
term_rate = (rate/100);         // Converts Percentage to Decimal
//mortgage calculation
monthly_payment = ((amount*term_rate/month*pow(term_rate/month+1,term_months))/(pow

(term_rate/month+1,term_months)-1), 2) ;

cout <<  "Your Mortgage amount is\n";
cout <<  "Amount: \$ " <<  amount << endl;                   //Prints out Amount
cout <<  "Your Mortgage is being taken out for" << endl;
cout <<  "Years: " << term << endl;                         //Prints out Term
cout <<  "The interest rate is\n";
cout <<  "Percent: " << rate << endl;                       //Prints out Rate
cout <<  "Your Monthly Payment Amount is " << endl;
cout <<  "Payment Amount: \$ " << monthly_payment;           //Prints out monthly paymet

//Allows the user to re-enter information or exit out program
cout << "Would you like to perform another calculation? ";
cin >> choice;
} while (choice == 'Y' || choice == 'y');
return 0;

}
0

LVL 3

Assisted Solution

What is it supposed to be calculating?  Can you give an example of the some sets of values you're entering, what you expect as output, and what the output currently is?
0

LVL 2

Accepted Solution

Your math doens't work because - well, your equation is not right. It should be:

monthly_payment=amount*term_rate/month/(1-pow(1+term_rate/month,-term_months));

Now you can finish your homework:)
0

Author Comment

I like to start off by thanking both of you for assisting me in something that had me going for quite awhile.  I am going to look over and see why my equation ends up being wrong in the first place.

Thanks
0

LVL 2

Expert Comment

If you look at the modified monthly_payment formula, you'll notice yours is mathematically the same as mine if you remove the ",2" part. What happened to your statement is that the stuff before comma is evaluated to the correct monthly_payment but the ",2" is a seperate statement. And in C, if you seperates two statements with comma, it will evaluate to the latter half. That's why you always get a monthly_payment of 2.
0

Author Comment

My program is listed above.

Help anyone looking to add some more information to my program that allows me to moddify 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-term loans, the list will scroll off the screen.  That does not let the information scroll off the screen, but display a partial list and then allow the user to continue the list.  Allow the user to loop back and enter new data or quit.

I know that I should be able to show a 6 month plan and than allow the user to input to get the rest of it so it would not scroll of the screen.  I have been having a hard time understanding how to do it.
0

## Featured Post

### Suggested Solutions

TextBlock Stops Updating 2 48
seriesUp challenge 7 83
array220 challenge 8 32
delphi parse string to params 3 49
INTRODUCTION We all know how to code. But at times you simply want to insert a common code block into your existing code and amend it as per your requirements. This tool not only saves you time but also saves you the pain of typing it all out aga…
A short article about problems I had with the new location API and permissions in Marshmallow
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …