c++ mortgage programc++ mortgage program
24 Replies - 22032 Views - Last Post: 07 September 2006 - 09:06 AM
#1
c++ mortgage program
Posted 20 September 2005 - 12:09 PM
Write a procedural C++ program that calculates and displays the mortgage payment amount given the amount of the mortgage, the term of the mortgage, and the interest rate of the mortgage. In this program, hard code the amount = $200,000, the term = 30 years, and the interest rate = 5.75%. Insert comments in the program to document the program.
Replies To: c++ mortgage program
#2
Re: c++ mortgage program
Posted 20 September 2005 - 12:12 PM
#3
Re: c++ mortgage program
Posted 20 September 2005 - 03:52 PM
// Write a procedural C++ program that calculates and displays the mortgage payment
//amount given the amount of the mortgage, the term of the mortgage, and the interest rate of the mortgage.
//In this program, hard code
//the amount = $200,000
//the term = 30 years
//the interest rate = 5.75%.
//Insert comments in the program to document the program.
#include "stdafx.h"
#include "math.h"
#include <iostream>
#using <mscorlib.dll>
using namespace System;
int main()
{
int a = 200000; //amount of the loan
double i = .00575; // the losn interest rate
int y = 30; //years of the loan
int t = y*12; //loan term in months
int mPayment; //variable for ouputting the payment
mPayment = (a * i) / (1 - pow(1+i,-t)); //Formula to figure mortgage payment amount
printf( "Your Monthly Payment Amount is: $ %i\n", mPayment); //prints out montyly payment amount
return 0;
}
This post has been edited by Dark_Nexus: 07 September 2006 - 09:14 AM
#4
Re: c++ mortgage program
Posted 20 September 2005 - 04:01 PM
#include "math.h"
#include <iostream>
using namespace std;
int main()
{
int a = 200000; //amount of the loan
double i = .00575; // the losn interest rate
int y = 30; //years of the loan
int t = y*12; //loan term in months
double mPayment; //variable for ouputting the payment
mPayment = (a * i) / (1 - pow(1+i,-t)); //Formula to figure mortgage payment amount
cout<< "Your Monthly Payment Amount is: $"<< mPayment; //prints out montyly payment amount
return 0;
}
It's only a slight modification, but it runs fine...I'm not sure how you were using cout before, but this version works...you can likely use it as a base to continue.
#5
Re: c++ mortgage program
Posted 20 September 2005 - 04:40 PM
interrupt_00, on Sep 20 2005, 05:30 PM, said:
Amadeus, on Sep 20 2005, 04:58 PM, said:
#include "math.h"
#include <iostream>
using namespace std;
int main()
{
 int a = 200000; //amount of the loan
 double i = .00575; // the losn interest rate
 int y = 30; //years of the loan
 int t = y*12; //loan term in months
 double mPayment; //variable for ouputting the payment
 mPayment = (a * i) / (1 - pow(1+i,-t)); //Formula to figure mortgage payment amount
 cout<< "Your Monthly Payment Amount is: $"<< mPayment; //prints out montyly payment amount
 return 0;
}
It's only a slight modification, but it runs fine...I'm not sure how you were using cout before, but this version works...you can likely use it as a base to continue.
thx for the help. I was using cout properly. Its nice to see I was not wrong, but this is the error message I get. when I compile in visual studio. When I use printf it works, when I use cout it screws up.
error C2065: 'cout' : undeclared identifier
thx for the help. I was using cout properly. Its nice to see I was not wrong, but this is the error message I get. when I compile in visual studio. When I use printf it works, when I use cout it screws up.
error C2065: 'cout' : undeclared identifier
#6
Re: c++ mortgage program
Posted 20 September 2005 - 04:53 PM
using namespace std;
?
I ran that code in a variety of compilers including VS2003.
Alternately, have you specified this project as a console or Win32 project?
#7
Re: c++ mortgage program
Posted 20 September 2005 - 08:39 PM
#8
Re: c++ mortgage program
Posted 21 September 2005 - 05:48 AM
Some things will be harder, but I'm sure you'll pick it up very quickly.
#9
Re: c++ mortgage program
Posted 26 September 2005 - 04:29 PM
// mortgage program
//header file
#include "stdafx.h"
#include "math.h"
#include <stdio.h>
#include <iostream>
#include <iomanip> //added for setprecision
#include <algorithm>
using namespace std;
bool validNumber(char *chrA) {
int n=0;
int start=0;
if(*chrA == '-') { start = 1; }
for(n=start; *(chrA + n) != 0; n++) {
if ( *(chrA + n) > '9' || *(chrA + n) < '0' && *(chrA + n) != '.') {
return false;
}
}
return true;
}
int main()//begining of main function
{
char Exit = 'y'; //char used in while loop to continue or exit
char P_char[ 10 ]; //char array for Principal
char T_char[ 3 ]; //char array for Term
char I_char[ 5 ]; //char array for Interest
double P = 0; //Amount of the loan
double I = 0; //The loan interest rate
double T =0; //The length of the mortgage in years
int C = 1; //declare variable type for c used as counter later
int M = 12; //Twelve Months in a year
double IM = (I/100)/M;//The interest rate per month
double TM = T * M;//total number of payments over the life of the loan (Term*Months)
double MonthlyPayment; //Variable for Monthly Payment
cout << fixed; //sets fixed decimal point
do{
cout <<"\n\t\t*******************************"; //output to screen
cout <<"\n\t\t** Mortgage Calculator **"; //output to screen like a header
cout <<"\n\t\t*******************************"; //output to screen
cout << endl;
cout <<"\n\t Enter the Loan Information when Prompted"; //output to screen instructions
cout <<"\n\t\t*******************************"; //output to screen
cout << endl;
//char P_char[ 10 ];
cout << "\n\t\tPlease enter the loan Principal: $";
cin >> P_char;
if (validNumber(&P_char[ 0 ])) {
P = double.Parse(P_char);
cout << endl;
}
else {
cout << "\n\t\tyou did NOT enter a valid number! \n\t\tPlease enter nubers only: $";
cin.clear();//reset cin, so more input can be done
cin.ignore(50,'\n');//remove bad data, up to 50 characters or until the end of line is reached
cin >> P_char;
}
//char I_char[ 10 ];
cout << "\n\t\tPlease enter the loan Interest Rate: ";
cin >> I_char;
if (validNumber(&I_char[ 0 ])) {
I = double.Parse(I_char);
cout << endl;
}
else {
cout << "\n\t\tyou did NOT enter a valid number! \n\t\tPlease enter numbers and decimal only: ";
cin.clear();//reset cin, so more input can be done
cin.ignore(50,'\n');//remove bad data, up to 50 characters or until the end of line is reached
cin >> I_char;
}
//char T_char[ 10 ];
cout << "\n\t\tPlease enter the loan Term in years: ";
cin >> T_char;
if (validNumber(&T_char[ 0 ])) {
T = double.Parse(T_char);
cout << endl;
}
else {
cout << "\n\t\tyou did NOT enter a valid number! \n\t\tPlease enter numbers only: ";
cin.clear();//reset cin, so more input can be done
cin.ignore(50,'\n');//remove bad data, up to 50 characters or until the end of line is reached
cin >> T_char;
}
IM = (I/100)/12; //Calculates Interest Monthly
TM = T * M; //Calculates Term Months
MonthlyPayment = P * (IM/(1 - pow((1+IM), -TM))); //Calculates monthly loan payment
while (C >= 0){
cout <<"\n\t\t ******************************"; //prints to screen
cout <<"\n\n\t\tYour Monthly Loan Payment is: $" << setprecision(2) << MonthlyPayment << endl; //prints out montyly payment amount
cout <<"\n\t\t ******************************"; //prints to screen
cout << "\n\t\t";//adds another line and move the press key statement
C--;
}
cout <<"\n\t\t\" y \" to continue any other to quit: "; //output to screen
cin >> Exit; //Input from User
system ("cls"); //clears the screen
} while(Exit == 'y'); //end of do loop
return 0;
} //end of main
This post has been edited by Dark_Nexus: 07 September 2006 - 09:14 AM
#10
Re: c++ mortgage program
Posted 26 September 2005 - 04:36 PM
If you absolutely must take the input as characters or strings, then you can use the strtod() function or the atof() function...but I would still recommend taking the input directly as a double.
#11
Re: c++ mortgage program
Posted 27 September 2005 - 09:29 AM
#12
Re: c++ mortgage program
Posted 27 September 2005 - 09:35 AM
for example;
char mystring[10] = "13.337"; float myfloat = atof(mystring);
myfloat would then be 13.337
also, you may want to try cin.good() when retreiving numbers from users
float myfloat = 0.0;
cin >> myfloat;
if (cin.good())
{
//Input was indeed a floating point value, continue
}
else
{
cout << "ERROR: Input was not a floating point value.\n"l
}
#13
Re: c++ mortgage program
Posted 27 September 2005 - 10:02 AM
#14
Re: c++ mortgage program
Posted 27 September 2005 - 11:22 AM
#15
Re: c++ mortgage program
Posted 04 September 2006 - 10:42 AM
Help??
Amadeus, on 20 Sep, 2005 - 04:01 PM, said:
#include "math.h"
#include <iostream>
using namespace std;
int main()
{
int a = 200000; //amount of the loan
double i = .00575; // the losn interest rate
int y = 30; //years of the loan
int t = y*12; //loan term in months
double mPayment; //variable for ouputting the payment
mPayment = (a * i) / (1 - pow(1+i,-t)); //Formula to figure mortgage payment amount
cout<< "Your Monthly Payment Amount is: $"<< mPayment; //prints out montyly payment amount
return 0;
}
It's only a slight modification, but it runs fine...I'm not sure how you were using cout before, but this version works...you can likely use it as a base to continue.
|
|

New Topic/Question
Reply



MultiQuote





|