📄 carpayment.cpp
字号:
// Tutorial 20: CarPayment.cpp
// Calculate different billing plans for a car loan.
#include <iostream> // required to perform C++ stream I/O
#include <iomanip> // required for parameterized stream manipulators
#include <cmath> // required to use the C++ math library functions
#include <string> // required to access string functions
#include <exception> // required to use exceptions
using namespace std; // for accessing C++ Standard Library members
// function prototypes
double calculateMonthlyPayment( double, int, int );
void processData( int, int, double );
// function main begins program execution
int main()
{
// define variables
int price; // price of the car
int downPayment; // the down payment
double interest; // the annual interest rate
bool errorOccurred = false; // true if an input error occurred
// temporarily store one line of invalid input
string invalidInput;
// do while cin has not successfully read the car price
do
{
// try to retrieve the car price
try
{
// cause cin to throw exceptions when errors occur
cin.exceptions( ios_base::failbit );
// prompt user for and input the car price
cout << "\nEnter the car price: ";
cin >> price; // get the car price
errorOccurred = false; // value obtained successfully
} // end try
catch ( ios_base::failure &inputError )
{
cin.clear(); // clear fail flag
// remove all characters from cin
getline( cin, invalidInput );
errorOccurred = true; // indicate that an error occurred
cout << "\nError: Please enter an integer for the "
<< "car price." << endl;
} // end catch
}
while ( errorOccurred == true ); // end do...while
// prompt user for and input the down payment
// and the annual interest rate (no exception handling)
cout << "Enter the down payment: ";
cin >> downPayment; // get the down payment
cout << "Enter the annual interest rate: ";
cin >> interest; // get the annual interest rate
// create table of options over different periods of time
processData( price, downPayment, interest);
return 0; // indicate that program ended successfully
} // end function main
// function calculates payments for each time interval
void processData( int price, int downPayment, double interest )
{
// calculate loan amount and monthly interest
int loanAmount = price - downPayment;
double monthlyInterest = interest / 1200;
// set precision for output values
cout << fixed << setprecision( 2 );
// add header to output
cout << endl << left << setw( 10 ) << "Months"
<< "Monthly Payments" << endl;
// calculate and display payments
for ( int years = 2; years <= 5; years++ )
{
// convert payment period to months
int months = 12 * years;
// get monthlyPayment
double monthlyPayment = calculateMonthlyPayment(
monthlyInterest, months, loanAmount );
// insert result into output stream
cout << left << setw( 10 ) << months
<< "$" << monthlyPayment << endl;
} // end for
} // end function processData
// calculate monthlyPayment
double calculateMonthlyPayment( double monthlyInterest,
int months, int loanAmount )
{
double base = pow( 1 + monthlyInterest, months );
return loanAmount * monthlyInterest / ( 1 - ( 1 / base ) );
} // end function calculateMonthlyPayment
/**************************************************************************
* (C) Copyright 1992-2005 by Deitel & Associates, Inc. and *
* Pearson Education, Inc. All Rights Reserved. *
* DISCLAIMER: The authors and publisher of this book have used their *
* best efforts in preparing the book. These efforts include the *
* development, research, and testing of the theories and programs *
* to determine their effectiveness. The authors and publisher make *
* no warranty of any kind, expressed or implied, with regard to these *
* programs or to the documentation contained in these books. The authors *
* and publisher shall not be liable in any event for incidental or *
* consequential damages in connection with, or arising out of, the *
* furnishing, performance, or use of these programs. *
**************************************************************************/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -