/* Homework Lab 4
- Using manipulators to format output
*/
//*****************************************************************
// Mortgage Payment Calculator program
// This program determines the monthly payments on a mortgage
given
// the loan amount, the yearly interest, and the number of
years.
//*****************************************************************
#include <iostream> // Access cout
#include <cmath> // Access power function
#include <iomanip> // Access manipulators
using namespace std;
const float LOAN_AMOUNT = 50000.00; // Amount of the loan
const float YEARLY_INTEREST = 0.0524; // Yearly interest rate
const int NUMBER_OF_YEARS = 7; // Number of years
int main()
{
// local variables
float monthlyInterest; // Monthly interest rate
int numberOfPayments; // Total number of
payments
float monthlyPayment; // Monthly payment
// Calculate values
monthlyInterest = YEARLY_INTEREST / 12;
numberOfPayments = NUMBER_OF_YEARS * 12;
monthlyPayment = (LOAN_AMOUNT * pow(monthlyInterest +
1, numberOfPayments)
* monthlyInterest)
/ (pow(monthlyInterest + 1,
numberOfPayments) - 1);
// Code to produce the Original Program Output
cout << "---------------- Original program output -----------------" << endl << endl;
cout << fixed << setprecision(2)
<< "For a loan amount of "
<< LOAN_AMOUNT << ", an
interest rate of " << YEARLY_INTEREST << "," <<
endl
<< "and " <<
NUMBER_OF_YEARS << " year mortgage, your monthly payments are
$"
<< monthlyPayment <<
"." << endl << endl;
/* TODO: Step 1
Write additional code to produce the output results
shown
in Step 1 Output below to right justify all
values.
Set the floating point format just once using the
fixed, showpoint,
and setprecision() manipulators. Use the setw()
manipulator
repeatedly on each line (once for each of the two
columns)
of output to adjust the column widths.
NOTE: You are NOT allowed to add blank spaces to
the beginning
or end of strings to make them line up properly. You
MUST use
'setw' properly to achieve the defined results.
ALSO, make sure you use the defined constants and
variables in
your output statements - DO NOT "hard code" the
numeric values
within the cout statements.
*/
cout << "----------------- Program Output Step 1 ------------------" << endl << endl;
// <Step 1 code here>
/* TODO: Step 2
Write additional code to produce the output results
shown
in Program Output Step 2 below to add 4-digit
precision
to interest rate (while also keeping Interest Rate and
Monthly
Payments as 2 digits), and line up all numeric values
by their
decimal points. Use setprecision() more than once and
adjust
field width (via setw) as necessary.
NOTE: You are NOT allowed to add blank spaces to
the beginning
or end of strings to make them line up properly. You
MUST use
'setw' properly to achieve the defined column
results.
*/
cout << "----------------- Program Output Step 2
------------------" << endl << endl;
// <Step 2 code here>
return 0;
}
/*
---------------- Original program output -----------------
For a loan amount of 50000.00, an interest rate of 0.05,
and 7 year mortgage, your monthly payments are $712.35.
----------------- Program Output Step 1 ------------------
Loan Amount: 50000.00
Interest Rate: 0.05
Years: 7
Monthly Payments: 712.35
----------------- Program Output Step 2 ------------------
Loan Amount: 50000.00
Interest Rate: 0.0524
Years: 7
Monthly Payments: 712.35
Press any key to continue . . .
*/
Program
/* Homework Lab 4
- Using manipulators to format output
*/
//*****************************************************************
// Mortgage Payment Calculator program
// This program determines the monthly payments on a mortgage
given
// the loan amount, the yearly interest, and the number of
years.
//*****************************************************************
#include <iostream> // Access cout
#include <cmath> // Access power function
#include <iomanip> // Access manipulators
using namespace std;
const float LOAN_AMOUNT = 50000.00; // Amount of the loan
const float YEARLY_INTEREST = 0.0524; // Yearly interest rate
const int NUMBER_OF_YEARS = 7; // Number of years
int main()
{
// local variables
float monthlyInterest; // Monthly interest rate
int numberOfPayments; // Total number of payments
float monthlyPayment; // Monthly payment
// Calculate values
monthlyInterest = YEARLY_INTEREST / 12;
numberOfPayments = NUMBER_OF_YEARS * 12;
monthlyPayment = (LOAN_AMOUNT * pow(monthlyInterest + 1,
numberOfPayments)
* monthlyInterest)
/ (pow(monthlyInterest + 1, numberOfPayments) - 1);
// Code to produce the Original Program Output
cout << "---------------- Original program output -----------------" << endl << endl;
cout << fixed << setprecision(2) << "For a
loan amount of "
<< LOAN_AMOUNT << ", an interest rate of " <<
YEARLY_INTEREST << "," << endl
<< "and " << NUMBER_OF_YEARS << " year mortgage,
your monthly payments are $"
<< monthlyPayment << "." << endl <<
endl;
/* TODO: Step 1
Write additional code to produce the output results shown
in Step 1 Output below to right justify all values.
Set the floating point format just once using the fixed,
showpoint,
and setprecision() manipulators. Use the setw() manipulator
repeatedly on each line (once for each of the two columns)
of output to adjust the column widths.
NOTE: You are NOT allowed to add blank spaces to the
beginning
or end of strings to make them line up properly. You MUST use
'setw' properly to achieve the defined results.
ALSO, make sure you use the defined constants and variables
in
your output statements - DO NOT "hard code" the numeric
values
within the cout statements.
*/
cout << "----------------- Program Output Step 1
------------------" << endl << endl;
// <Step 1 code here>
cout << left << setw(20) << "Loan Amount:
" << fixed << setprecision(2) << right <<
setw(10) << LOAN_AMOUNT << endl
<< left << setw(20) << "Interest Rate: " <<
setprecision(2) << right << setw(10) <<
YEARLY_INTEREST << endl
<< left << setw(20) << "Years: " << right
<< setw(7) << NUMBER_OF_YEARS << endl
<< left << setw(20) << "Monthly Payment: "
<< setprecision(2) << right << setw(10) <<
monthlyPayment << endl << endl;
/* TODO: Step 2
Write additional code to produce the output results shown
in Program Output Step 2 below to add 4-digit precision
to interest rate (while also keeping Interest Rate and
Monthly
Payments as 2 digits), and line up all numeric values by
their
decimal points. Use setprecision() more than once and adjust
field width (via setw) as necessary.
NOTE: You are NOT allowed to add blank spaces to the
beginning
or end of strings to make them line up properly. You MUST use
'setw' properly to achieve the defined column results.
*/
cout << "----------------- Program Output Step 2
------------------" << endl << endl;
// <Step 2 code here>
cout << left << setw(20) << "Loan Amount:
" << fixed << setprecision(2) << right <<
setw(10) << LOAN_AMOUNT << endl
<< left << setw(20) << "Interest Rate: " <<
setprecision(4) << right << setw(12) <<
YEARLY_INTEREST << endl
<< left << setw(20) << "Years: " << right
<< setw(7) << NUMBER_OF_YEARS << endl
<< left << setw(20) << "Monthly Payment: "
<< setprecision(2) << right << setw(10) <<
monthlyPayment << endl;
cout << "Press any key to continue . . ." <<
endl;
return 0;
}
Sample Output:
Get Answers For Free
Most questions answered within 1 hours.