Isn't this Interest-ing? 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 &lt;cctype&gt;
#include &lt;iostream&gt;
#include &lt;limits&gt;

/**
 * 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 &gt;&gt; input) || input &lt; 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&lt;std::streamsize&gt;::max(), '\n');
        std::cout &lt;&lt; "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 &lt;&lt; "Enter the price of the house: $";
        housePrice = getSanitizedDouble();

        // get input for down payment, re-read input if not a positive number
        std::cout &lt;&lt; "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 &lt;&lt; "The annual after-tax cost of the house is $" &lt;&lt; totalCost &lt;&lt; std::endl;

        std::cout &lt;&lt; "Run the program again (y/N): ";  // signify n as default with capital letter
    } while ('y' == getSanitizedChar());
}
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. 

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 suggest
putting 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;
}