View Single Post
Old 10-22-2004, 02:15 PM   #3 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
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