patterncppMajor
Isn't this Interest-ing?
Viewed 0 times
thisisninteresting
Problem
This is the fifth project in my CS1 class. It's a bit more drab than my past projects, so my titles are getting worse unfortunately.
Write a program that computes the annual after-tax cost of a new house
for the first year of ownership. The cost is computed as the annual
mortgage cost minus the tax savings. The input should be the price of
the house and the down payment. The annual mortgage cost can be
estimated as \$ 3\% \$ of the initial loan balance credited toward
paying of the loan principal plus \$ 6\% \$ of the initial loan
balance in interest. The initial loan balance is the price minus the
down payment. Assume a \$ 35\% \$ marginal tax rate and assume that
interest payments are tax deductible. So, the tax savings is \$ 35\% \$
of the interest payment. Your program should use at least two
function definitions and should allow the user to repeat this
calculation as often as the user wishes.
I may have cheated a bit with my required two function definitions...
```
/**
* @file mortgage.cpp
* @brief Computes the annual after-tax cost of a new house
* @author syb0rg
* @date 10/9/14
*/
#include
#include
#include
/**
* Makes sure data isn't malicious, and signals user to re-enter proper data if invalid
*/
long double getSanitizedDouble()
{
long double input = 0.0L;
while(!(std::cin >> input) || input ::max(), '\n');
std::cout << "Invalid input. Please enter a positive number: ";
}
return input;
}
/**
* Safetly grabs and returns a lowercase version of the character (if the lowercase exists)
*/
char32_t getSanitizedChar()
{
// absorb newline character (if existant) from previous input
if(std::cin.peek() == '\n') std::cin.ignore();
return std::tolower(std::cin.get());
}
int main()
{
do
{
long double housePrice = 0.0L;
long double downPayment = 0.0L;
// get input for house price, re-read input if not a positive number
s
:(Write a program that computes the annual after-tax cost of a new house
for the first year of ownership. The cost is computed as the annual
mortgage cost minus the tax savings. The input should be the price of
the house and the down payment. The annual mortgage cost can be
estimated as \$ 3\% \$ of the initial loan balance credited toward
paying of the loan principal plus \$ 6\% \$ of the initial loan
balance in interest. The initial loan balance is the price minus the
down payment. Assume a \$ 35\% \$ marginal tax rate and assume that
interest payments are tax deductible. So, the tax savings is \$ 35\% \$
of the interest payment. Your program should use at least two
function definitions and should allow the user to repeat this
calculation as often as the user wishes.
I may have cheated a bit with my required two function definitions...
mortgage.cpp:```
/**
* @file mortgage.cpp
* @brief Computes the annual after-tax cost of a new house
* @author syb0rg
* @date 10/9/14
*/
#include
#include
#include
/**
* Makes sure data isn't malicious, and signals user to re-enter proper data if invalid
*/
long double getSanitizedDouble()
{
long double input = 0.0L;
while(!(std::cin >> input) || input ::max(), '\n');
std::cout << "Invalid input. Please enter a positive number: ";
}
return input;
}
/**
* Safetly grabs and returns a lowercase version of the character (if the lowercase exists)
*/
char32_t getSanitizedChar()
{
// absorb newline character (if existant) from previous input
if(std::cin.peek() == '\n') std::cin.ignore();
return std::tolower(std::cin.get());
}
int main()
{
do
{
long double housePrice = 0.0L;
long double downPayment = 0.0L;
// get input for house price, re-read input if not a positive number
s
Solution
Some minor changes I would recommend:
-
Replace the magic numbers with symbolic constants. It will be easier to
modify your program when these rates change.
-
Instead of doing the calculations inside
putting it into a function.
-
Replace the magic numbers with symbolic constants. It will be easier to
modify your program when these rates change.
constexpr long double ANNUAL_MORTGAGE_COST = 0.03;
constexpr long double LOAN_PRINCIPAL_PLUS = 0.06;
constexpr long double MARGINAL_TAX_RATE = 0.35;-
Instead of doing the calculations inside
main() I would suggestputting it into a function.
long double totalPayment(long double housePrice, long double downPayment){
long double loanBalance = housePrice - downPayment;
long double interest = LOAN_PRINCIPAL_PLUS * loanBalance;
long double annualMortgage = (ANNUAL_MORTGAGE_COST * loanBalance) + interest;
long double savings = MARGINAL_TAX_RATE * interest;
long double totalCost = annualMortgage - savings;
return totalCost;
}Code Snippets
constexpr long double ANNUAL_MORTGAGE_COST = 0.03;
constexpr long double LOAN_PRINCIPAL_PLUS = 0.06;
constexpr long double MARGINAL_TAX_RATE = 0.35;long double totalPayment(long double housePrice, long double downPayment){
long double loanBalance = housePrice - downPayment;
long double interest = LOAN_PRINCIPAL_PLUS * loanBalance;
long double annualMortgage = (ANNUAL_MORTGAGE_COST * loanBalance) + interest;
long double savings = MARGINAL_TAX_RATE * interest;
long double totalCost = annualMortgage - savings;
return totalCost;
}Context
StackExchange Code Review Q#65005, answer score: 20
Revisions (0)
No revisions yet.