Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 10-22-2004, 11:11 AM   #1 (permalink)
verdell32
Registered User
 
verdell32's Avatar
 
Join Date: Oct 2004
Posts: 35
verdell32 is on a distinguished road
Talking Problem with Mortgage.cpp

I am trying to figure out what I am missing or doing wrong here are the spec's

Write Program that calculates and displays this information about a home mortgage.

This is how the finished product should look in the "Command Prompt"(Input Information)

Enter Price of house
Enter down payment
Annual Interest Rate
Number of years to pay




Code:
// Program Documentation //////////////////////////////////////////////////////
//
// Project Name  : MORTGAGE
//
// Source File   : mortgage.cpp
//
// Programmed By :
//
// Last Revision : 10/21/2004
//
// Version Number: 0.1
//
// Program Description ////////////////////////////////////////////////////////
//
// This program
//
///////////////////////////////////////////////////////////////////////////////

// Include Files //////////////////////////////////////////////////////////////
#include <iostream.h>
#include <conio.h>
#include <math.h>

// Function Prototypes ////////////////////////////////////////////////////////

// Program Mainline ///////////////////////////////////////////////////////////

int main( )
{
  double homePrice;
  double downPayment;
  double yearlyInterestRate;
  int termInYears;
  double monthlyInterestRate;
  double termInMonths;
  double tempTerm;
  double loanAmount;
  double monthlyPayment;
  double loanCost;


  //get input
  cout << "Enter Price of house:";
  cin >> homePrice;
  cout << "Enter down payment:";
  cin >> downPayment;
  cout << "Enter annual interest rate:";
  cin >> yearlyInterestRate;
  cout << "Enter pay off in years:";
  cin >> termInYears;


  // Calculate values
    monthlyInterestRate = yearlyInterestRate / 100;
    monthlyPayment = termInYears * 12;
    loanCost = (loanAmount *
               pow(monthlyInterestRate + 1, tempTerm)
               * monthlyInterestRate)/( pow(monthlyInterestRate + 1,
               tempTerm) - 1 );

    // Output results 
    		cout << homePrice << endl;
         cout<< loanAmount  << " with an interest rate of " << endl;
         cout<< yearlyInterestRate << " and a " << termInYears
         << " year mortgage, " << endl;          
    cout << " your monthly payments are $" << endl;

   getch();
   return 0;
}//endmn
"And the output should look like this in the (Console application)

==============================
Price fo house
Down Payment
Amount of loan
Annual Interest Rate
Pay off in years
Number of payments
Monthly Payment
Cost of Loan
===============================

Last edited by Valmont; 10-22-2004 at 11:42 AM.
verdell32 is offline   Reply With Quote
Old 10-22-2004, 01:34 PM   #2 (permalink)
Kernel_Killer
Regular Contributor
 
Kernel_Killer's Avatar
 
Join Date: Feb 2003
Location: indisclosed
Posts: 210
Kernel_Killer is on a distinguished road
Well, on your entries, why not throw some "\n"'s in front of each one except the first? Same with the output. It looks like everything else looks ok.
__________________
Network Synapse
Screaming Electron
Kernel_Killer is offline   Reply With Quote
Old 10-22-2004, 02:15 PM   #3 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,545
Valmont is on a distinguished road
Quote:
Originally posted by Kernel_Killer
Well, on your entries, why not throw some "\n"'s in front of each one except the first? Same with the output. It looks like everything else looks ok.
You gotta be kidding.

Code:
#include <iostream>
#include <string>
#include <climits>

using std::cout;
using std::endl;
using std::cin;

//Optional functions: depends on IDE.
void wait_for_enter();

//Additional helper: might be needed by IDE.
void reset_istream();

int main(int argc, char *argv[])
{
   double homePrice;
   cout << "Enter Price of house:";
   cin >> homePrice;
   
   double downPayment;
   cout << "Enter down payment:";
   cin >> downPayment;
   
   double yearlyInterestRate;
   cout << "Enter annual interest rate:";
   cin >> yearlyInterestRate;
   
   int termInYears;
   cout << "Enter pay off in years:";
   cin >> termInYears;

   // Calculate values for the formula.
   double monthlyInterestRate = (yearlyInterestRate / 12)/100 ;
   int numberOfPayments = termInYears * 12;
   double loanAmount = homePrice-downPayment;
   
   //The formula.
   double monthlyPayment =
   (loanAmount * pow(monthlyInterestRate + 1, numberOfPayments)* monthlyInterestRate)
   / ( pow(monthlyInterestRate + 1, numberOfPayments) - 1 );

   // Output results
   cout<<endl;
   cout <<"Home price: "<< homePrice << endl;
   cout<<"Down payment: "<<downPayment<<endl;
   cout<<"Loan amount "<<loanAmount<<endl;
   cout<< "Yearly interest rate: " <<yearlyInterestRate<<endl;
   cout<<"Payoff of years: "<< termInYears<<endl;
   cout<< "Monthly payment: $"<<monthlyPayment<<endl;
   cout<<"Cost of loan: "<<monthlyPayment*numberOfPayments<<endl;

   reset_istream();
   wait_for_enter();
   return 0;
}

//---------------------------------------------------

void reset_istream()
{
   if(cin.eof())
   {
      cin.clear();
   }
   else
   {
      cin.clear();
      std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
   }
}

//---------------------------------------------------

void wait_for_enter()
{
  cout << "press <enter> to continue...";
  // Reset failstate, just in case.
  cin.clear();
  std::string line;
  std::getline( cin, line);
}
__________________
Valmont is offline   Reply With Quote
Old 10-22-2004, 04:48 PM   #4 (permalink)
joe_bruin
LOAD "*",8,1
 
Join Date: Feb 2003
Location: la.ca.us
Posts: 254
joe_bruin is on a distinguished road
Code:
#include <iostream>
#include <string>
#include <climits>

using std::cout;
using std::endl;
using std::cin;
what was wrong with his headers? i know it is your opinion that everyone should use the namespace format, but his code is legal and valid. why change it? i don't mean to be a jackass, i'm just trying to make the point that you don't need to rewrite someone's code if they are doing things mostly correctly and just don't fit in with your style/view of the right way to do things.

Code:
//Optional functions: depends on IDE.
void wait_for_enter();
not optional, it is required by c and c++ to provide a function declaration before using that function (except std* functions in c). compilers that don't enforce this are non-compliant, and code that does not include these is incorrect.

Code:
int main(int argc, char *argv[])
his main declaration was correct, why fix it?.

Code:
void wait_for_enter()
{
  ...
i'm glad to see this function. it's nice to have a non-platform dependent way of doing this.
joe_bruin is offline   Reply With Quote
Old 10-22-2004, 06:03 PM   #5 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,545
Valmont is on a distinguished road
- conio
That is not standard whatsoever.

- iostream.h / math.h
He is using "cin", "cout" and uses "double" as default precision variables. Note that the standard (C++) assumes "double" as the default precision type. All of this gives me the impression that he want's to use C++ instead of C.
In that case, the above headers do not exist in standard C++. Old.

Quote:
code:--------------------------------------------------------------------------------
//Optional functions: depends on IDE.
void wait_for_enter();
--------------------------------------------------------------------------------


not optional, it is required by c and c++ to provide a function declaration before using that function...
The comment never said that the declaration is optional in case it is being used. It said that the function is optional.
Quote:
code:--------------------------------------------------------------------------------
int main(int argc, char *argv[])
--------------------------------------------------------------------------------


his main declaration was correct, why fix it?.
I didn't fix it. I had/have a script running for my own needs on startup.

I hope this cleared up a few things. Your comments are good topics to discuss. However, note that I only support ISO C++ (I use structs as intermediate types as an exception). I stay away from "C" or anything else, as you most likely noticed. I think it is important to teach students to code according the standards. Not because the standards are sacred to me, but because it makes people think more about what they are doing (with their language), from scratch.

Besides, this is the standard C/C++ forum. Let's use it that way.
__________________
Valmont is offline   Reply With Quote
Old 10-22-2004, 06:14 PM   #6 (permalink)
joe_bruin
LOAD "*",8,1
 
Join Date: Feb 2003
Location: la.ca.us
Posts: 254
joe_bruin is on a distinguished road
Quote:
Originally posted by Valmont
[b]- conio
That is not standard whatsoever.
absolutely. i meant the ".h" (old style) includes.

Quote:
- iostream.h / math.h
In that case, the above headers do not exist in standard C++. Old.
sure they do. backwards compatibility is maintained with older versions of c++ and with c, and they are legitimate to use.

Quote:
The comment never said that the declaration is optional in case it is being used. It said that the function is optional.
my mistake.
joe_bruin is offline   Reply With Quote
Old 10-22-2004, 06:44 PM   #7 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,545
Valmont is on a distinguished road
Quote:
quote:
--------------------------------------------------------------------------------

- iostream.h / math.h
In that case, the above headers do not exist in standard C++. Old.

--------------------------------------------------------------------------------



sure they do. backwards compatibility is maintained with older versions of c++ and with c, and they are legitimate to use.
True that, but that's not what I mean lol.
C++ actually requires one to use the new header file names. I've got some info for you from the ISO C++ Handbook:
Quote:
Chapter 2: standard briefing
Standard C Headers in the form <name.h>
For compatibility with the Standard C library, C++ still supports the naming convention of C headers in the form
<xxx.h> -- but this naming convention is now deprecated (this is discussed in more detail in Chapter 8).

Chapter 8: namespaces
Standard Headers Names
All Standard C++ header files now have to be included as follows:
#include <iostream> //note: no ".h" extension
Here is from the current ISO-IEC C++ standard:
Quote:
17.4.1.2 Headers
1 The elements of the C++ Standard Library are declared or defined (as appropriate) in a header.158)
2 The C++ Standard Library provides 32 C++ headers, as shown in Table 11:
Table 11—C++ Library Headers
_ __________________________________________________ ____________________
<algorithm> <iomanip> <list> <ostream> <streambuf>
<bitset> <ios> <locale> <queue> <string>
<complex> <iosfwd> <map> <set> <typeinfo>
<deque> <iostream> <memory> <sstream> <utility>
<exception> <istream> <new> <stack> <valarray>
<fstream> <iterator> <numeric> <stdexcept> <vector>
<functional> <limits> _ __________________________________________________ ____________________ ç ç
ç
ç
ç
ç
ç
ç
ç
ç ç ç ç ç ç ç ç ç
3 The facilities of the Standard C Library are provided in 18 additional headers, as shown in Table 12:
Table 12—C++ Headers for C Library Facilities
_ __________________________________________________ ___
<cassert> <ciso646> <csetjmp> <cstdio> <ctime>
<cctype> <climits> <csignal> <cstdlib> <cwchar>
<cerrno> <clocale> <cstdarg> <cstring> <cwctype>
<cfloat> <cmath> <cstddef> _ __________________________________________________ ___ ç
ç
ç
ç
ç
ç ç ç ç ç
4 Except as noted in clauses 18 through 27, the contents of each header cname shall be the same as that of the
corresponding header name.h, as specified in ISO/IEC 9899:1990 Programming Languages C (Clause 7),
or ISO/IEC:1990 Programming Languages—C AMENDMENT 1: C Integrity, (Clause 7), as appropriate,
as if by inclusion. In the C++ Standard Library, however, the declarations and definitions (except for
names which are defined as macros in C) are within namespace scope (3.3.5) of the namespace std.
Quote:
17.4.4.1 Headers
1 A C++ header may include other C++ headers.170)
2 Certain types and macros are defined in more than one header. For such an entity, a second or subsequent
header that also defines it may be included after the header that provides its initial definition (3.2).
3 Header inclusion is limited as follows:
— The C headers ( .h form, described in Annex D, D.5) shall include only their corresponding C++
header, as described above (17.4.1.2).
Basically, the .h extension is deprecated because the .h headers are at global space. Exactly the thing that the standard tries to avoid.
I'd thought you'd like to read this a lil. By they way, you know you can order the standard(s) right? It's just under $20. Just a heads up .
__________________
Valmont is offline   Reply With Quote
Old 10-23-2004, 06:50 AM   #8 (permalink)
verdell32
Registered User
 
verdell32's Avatar
 
Join Date: Oct 2004
Posts: 35
verdell32 is on a distinguished road
I thank everyone very much

I am taking my first programming class in C++ and we are using Borland C++ 5; I have been fighting with this for a week and its do this monday.

So thanks again.
verdell32 is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
c simple question problem with switch case if13121 Standard C, C++ 1 10-24-2004 09:43 PM
problem with editing profile cheawick Feedback 0 04-28-2004 10:26 PM
Help debugging a power problem Belisarius Lounge 0 10-25-2003 04:44 PM
structure problem Goshi Standard C, C++ 5 04-21-2003 12:19 AM
This is a windows/C problem UnderWing Standard C, C++ 6 03-28-2003 06:17 AM


All times are GMT -8. The time now is 05:11 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting