c++ mortgage program

c++ mortgage program

  • (2 Pages)
  • +
  • 1
  • 2

24 Replies - 25346 Views - Last Post: 07 September 2006 - 09:06 AM Rate Topic: -----

#1 interrupt_00  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 14
  • Joined: 20-September 05

c++ mortgage program

Posted 20 September 2005 - 12:09 PM

Need help learnig C++. I'm using Microsoft Visual Studio 2003. My assignment reads as bellow. Any help would be appreciated. I'm not a programmer of any kind, so really do not know where to start.


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.

Is This A Good Question/Topic? 1
  • +

Replies To: c++ mortgage program

#2 Amadeus  Icon User is offline

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

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: c++ mortgage program

Posted 20 September 2005 - 12:12 PM

We generally do not provide straight out answers, but prefer to guide...can you post what you've done so far?
Was This Post Helpful? 0
  • +
  • -

#3 interrupt_00  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 14
  • Joined: 20-September 05

Re: c++ mortgage program

Posted 20 September 2005 - 03:52 PM

Oops guess I really should have done that. Here is my code that works as a C type program, but when I tryt to use cout it will not go. I have the iostream lib called but still nada unless I use printf. Again, I am using MS Visual Sudio 2003.


// 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

Was This Post Helpful? 0
  • +
  • -

#4 Amadeus  Icon User is offline

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

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: c++ mortgage program

Posted 20 September 2005 - 04:01 PM

Try this:
#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.
Was This Post Helpful? 0
  • +
  • -

#5 interrupt_00  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 14
  • Joined: 20-September 05

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:

Try this:
#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
Was This Post Helpful? 0
  • +
  • -

#6 Amadeus  Icon User is offline

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

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: c++ mortgage program

Posted 20 September 2005 - 04:53 PM

Did you specify the namespace as I changed it...to
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?
Was This Post Helpful? 0
  • +
  • -

#7 interrupt_00  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 14
  • Joined: 20-September 05

Re: c++ mortgage program

Posted 20 September 2005 - 08:39 PM

I think you have hit upon it. I specified it as a console project. I will allso check on the namespace as soon as VS2003 finishes loading on my home system. Thank you for the advice and direction. I'm sure it is only going to get harder, but I think I might be moving in the right direction. I'm assuming this needs to be a win32 application to use the cout and cin.
Was This Post Helpful? 0
  • +
  • -

#8 Amadeus  Icon User is offline

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

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: c++ mortgage program

Posted 21 September 2005 - 05:48 AM

Actually, cout and cin can be used in virtually any type of c++ project...I was more concerned about the inclusion of the std namespace. I believe this project should be a console project, so I think you're on the right track there...try using the namespace line, and see if it works.

Some things will be harder, but I'm sure you'll pick it up very quickly.
Was This Post Helpful? 0
  • +
  • -

#9 interrupt_00  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 14
  • Joined: 20-September 05

Re: c++ mortgage program

Posted 26 September 2005 - 04:29 PM

Thx for your help. my project has moved along nicely up until I started playing with data validation. I'm trying to get my char to parse to double so I can use it in calculations. I've written the code the way I think it should work, but of course it does not. I've punded at this for hours and can't get it work. There is suppossed to be an easy way to do this, but I can't get it to synch up. Here's the update code. can you see what I am doing wrong?

// 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

Was This Post Helpful? 0
  • +
  • -

#10 Amadeus  Icon User is offline

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

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: c++ mortgage program

Posted 26 September 2005 - 04:36 PM

May I ask why you are taking the numerical input as chars? You may be familiar with java, where input was commonly taken as strings of chars, but c++ (and java's scanner class) allow for input to be taken directly as whatever variable type you require...you can take them directly as doubles, unless you have a reason for wanting them as chars that I am not aware of...

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.
Was This Post Helpful? 0
  • +
  • -

#11 interrupt_00  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 14
  • Joined: 20-September 05

Re: c++ mortgage program

Posted 27 September 2005 - 09:29 AM

Hi, I wish I could take the input as a double, but I am attempting to do data validation, and I just don't get it, and can't make it work when I take the input as double and try to check it. If I don't use an array to check I can't get it to work. I have tried six or seven different ways of doing this and have spent ten plus hours at it, none of the methods work completely. The easiest way was to use !cin on the input, but as soon as you type in "3wd4e" or similar it treats it as a valid value. I tried !fail.cin. I even tried this string input thing I really did not understand. This array works but I have to convert the values to doubles once I am done and I can't figure out a way to do it. I’ll have to read about the strtod() and atof() functions you mentioned. I have not seen or heard of them before, and see If I can figure it out. I am just frustrated to the point of using imperfect data validation because I cannot get it to work. Thank you for your help and direction. Oh and yes Java was the first language I messed with so I might be in an incorrect mind set.
Was This Post Helpful? 0
  • +
  • -

#12 dorknexus  Icon User is offline

  • or something bad...real bad.
  • member icon

Reputation: 1255
  • View blog
  • Posts: 4,618
  • Joined: 02-May 04

Re: c++ mortgage program

Posted 27 September 2005 - 09:35 AM

atof(-String to be converted-) will return the floating point value found in the string.

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
}


Was This Post Helpful? 0
  • +
  • -

#13 Amadeus  Icon User is offline

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

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: c++ mortgage program

Posted 27 September 2005 - 10:02 AM

My esteemed colleague Dark_Nexus is correct, but I should point aout that the .good() method of the cin object will validate any defined data type, not just numbers.
Was This Post Helpful? 0
  • +
  • -

#14 interrupt_00  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 14
  • Joined: 20-September 05

Re: c++ mortgage program

Posted 27 September 2005 - 11:22 AM

Yep! that is my problem. I used both .good and .fail and they sort of work, but you can still easily fail the program or enter incorrect information. That is why I was trying to use my array that works great, but I have to convert the data type. There has got to be an easy data validation routine since everyone needs to validate data. I'm suprised there is not a method for it. I'm still trying to figure this out, but everything I do does not work. I'm reading and trying to puzzle out this strod() function.
Was This Post Helpful? 0
  • +
  • -

#15 threeclasses  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 04-September 06

Re: c++ mortgage program

Posted 04 September 2006 - 10:42 AM

I have that part done, but now I need to prompt the user for input and then give an amortization table for the length of the loan.
Help??

View PostAmadeus, on 20 Sep, 2005 - 04:01 PM, said:

Try this:
#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.

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2