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...
mortgage.cpp
:
/**
* @file mortgage.cpp
* @brief Computes the annual after-tax cost of a new house
* @author syb0rg
* @date 10/9/14
*/
#include <cctype>
#include <iostream>
#include <limits>
/**
* 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 < 0)
{
// clear the error flag that was set so that future I/O operations will work correctly
std::cin.clear();
// skips to the next newline
std::cin.ignore(std::numeric_limits<std::streamsize>::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
std::cout << "Enter the price of the house: $";
housePrice = getSanitizedDouble();
// get input for down payment, re-read input if not a positive number
std::cout << "Enter the down-payment: $";
downPayment = getSanitizedDouble();
long double loanBalance = housePrice - downPayment;
long double interest = .06 * loanBalance;
long double annualMortgage = (.03 * loanBalance) + interest;
long double savings = .35 * interest;
long double totalCost = annualMortgage - savings;
std::cout << "The annual after-tax cost of the house is $" << totalCost << std::endl;
std::cout << "Run the program again (y/N): "; // signify n as default with capital letter
} while ('y' == getSanitizedChar());
}