Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 132,625 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,064 people online right now. Registration is fast and FREE... Join Now!




c++ mortgage program

3 Pages V  1 2 3 >  
Reply to this topicStart new topic

c++ mortgage program, c++ mortgage program

interrupt_00
post 20 Sep, 2005 - 11:09 AM
Post #1


New D.I.C Head

*
Joined: 20 Sep, 2005
Posts: 14


My Contributions


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.
User is offlineProfile CardPM

Go to the top of the page

Amadeus
post 20 Sep, 2005 - 11:12 AM
Post #2


g++ -o drink whiskey.cpp

Group Icon
Joined: 12 Jul, 2002
Posts: 12,176



Thanked 33 times

Dream Kudos: 25
My Contributions


We generally do not provide straight out answers, but prefer to guide...can you post what you've done so far?
User is offlineProfile CardPM

Go to the top of the page

interrupt_00
post 20 Sep, 2005 - 02:52 PM
Post #3


New D.I.C Head

*
Joined: 20 Sep, 2005
Posts: 14


My Contributions


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.


CODE
// 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: 7 Sep, 2006 - 08:14 AM
User is offlineProfile CardPM

Go to the top of the page

Amadeus
post 20 Sep, 2005 - 03:01 PM
Post #4


g++ -o drink whiskey.cpp

Group Icon
Joined: 12 Jul, 2002
Posts: 12,176



Thanked 33 times

Dream Kudos: 25
My Contributions


Try this:
CODE

#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.
User is offlineProfile CardPM

Go to the top of the page

interrupt_00
post 20 Sep, 2005 - 03:40 PM
Post #5


New D.I.C Head

*
Joined: 20 Sep, 2005
Posts: 14


My Contributions


QUOTE(interrupt_00 @ Sep 20 2005, 05:30 PM)
QUOTE(Amadeus @ Sep 20 2005, 04:58 PM)
Try this:
CODE

#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
User is offlineProfile CardPM

Go to the top of the page

Amadeus
post 20 Sep, 2005 - 03:53 PM
Post #6


g++ -o drink whiskey.cpp

Group Icon
Joined: 12 Jul, 2002
Posts: 12,176



Thanked 33 times

Dream Kudos: 25
My Contributions


Did you specify the namespace as I changed it...to
CODE

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?
User is offlineProfile CardPM

Go to the top of the page

interrupt_00
post 20 Sep, 2005 - 07:39 PM
Post #7


New D.I.C Head

*
Joined: 20 Sep, 2005
Posts: 14


My Contributions


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.
User is offlineProfile CardPM

Go to the top of the page

Amadeus
post 21 Sep, 2005 - 04:48 AM
Post #8


g++ -o drink whiskey.cpp

Group Icon
Joined: 12 Jul, 2002
Posts: 12,176



Thanked 33 times

Dream Kudos: 25
My Contributions


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.
User is offlineProfile CardPM

Go to the top of the page

interrupt_00
post 26 Sep, 2005 - 03:29 PM
Post #9


New D.I.C Head

*
Joined: 20 Sep, 2005
Posts: 14


My Contributions


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?

CODE
// 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: 7 Sep, 2006 - 08:14 AM
User is offlineProfile CardPM

Go to the top of the page

Amadeus
post 26 Sep, 2005 - 03:36 PM
Post #10


g++ -o drink whiskey.cpp

Group Icon
Joined: 12 Jul, 2002
Posts: 12,176



Thanked 33 times

Dream Kudos: 25
My Contributions


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.
User is offlineProfile CardPM

Go to the top of the page

interrupt_00
post 27 Sep, 2005 - 08:29 AM
Post #11


New D.I.C Head

*
Joined: 20 Sep, 2005
Posts: 14


My Contributions


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.
User is offlineProfile CardPM

Go to the top of the page

Dark_Nexus
post 27 Sep, 2005 - 08:35 AM
Post #12


or something bad...real bad.

Group Icon
Joined: 2 May, 2004
Posts: 1,309



Thanked 2 times

Dream Kudos: 625
My Contributions


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

for example;

CODE

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

CODE

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
}
User is offlineProfile CardPM

Go to the top of the page

3 Pages V  1 2 3 >
Reply to this topicStart new topic
Time is now: 11/23/08 03:31AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month