[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 704
  • Last Modified:

Program computes the annual after_tax cost of a new house

Topic:  Write a program that computes the annual after_tax cost of a new house for the first year of ownership. The cost is computed aa 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 off 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 interst payments are tax deductible. So the tax savings is 35%  of the interst payment.

This is my program.
#include <iostream.h>
#include <stdlib.h>
#define DEBUG true

double initial(double price, double down_payment);
double mortgage(double initial);
double taxrate(double price);
double housecost(double mortgageCost, double taxrate);

int main()
{
   char ans;
   double price, down_payment;
   double initial, mortgageCost, tax, cost;

   cout.setf(ios::fixed);
   cout.setf(ios::showpoint);
   cout.precision(2);

   do
   {
        //Get input from the user
      cout << "Enter the price of the house: ";
      cin >> price;
      cout << "Enter your down payment): ";
      cin >>down_payment;

      //Output result
      cout <<"The initial loan balance is "<<initial(price, down_payment)<<endl;
      cout <<"The annual mortgage cost is "<<mortgage(initial)<<endl;
      cout <<"The tax saving is "<<taxrate(price)<<endl;
      cout <<"The cost of a new house for the first year of ownership is "<<housecost(mortgage, taxrate);

      //Ask user if wanting to calculate again
      cout <<"Start it again?\n"
           <<"Press y for yes, no for no,\n"
           <<"and then press return: ";
      cin >>ans;
      cout <<endl;

      } while (ans == 'y' || ans == 'Y');

      system("PAUSE");
    return 0;
}

double initial(double price, double down_payment)
/*********************************************************************
Function Name:  initial
Purpose:   Calculate the initial loan
Arguments:   price, down_payment
Returns:  initial

*********************************************************************/
{
    if (DEBUG)
       {
    cout <<"The initial loan balance is: "<<initial;
    cout <<endl;
    }
    return (price - down_payment);
}

double mortgageCost (double initial)
/*********************************************************************
Function Name: mortgage_cost
Purpose:   Calculate the mortgae cost
Arguments: initial_loan
Returns:  mortgage cost

*********************************************************************/
{
    if (DEBUG)
    {
    cout <<"The annual mortgage cost is: "<<mortgage(initial);
    cout <<endl;
    }
    const double loan_balance = 0.03; //3% of initial loan balance
    const double loan_interest = 0.06; //6% of the initial loan balance in intersest
    double total;
    loan_balance = (initial_loan * 0.03);
    loan_interest = (initial_loan * 0.06);
    total = loan_balance + loan_interest;
    return (total);

}


double taxrate(double price)
/*********************************************************************
Function Name:  tax
Purpose:   Calculate the tax
Arguments:  price
Returns:   tax

*********************************************************************/
{
    if (DEBUG)
    {
    cout <<"The tax saving is "<<taxrate;
    cout <<endl;
    }

    const double interest_payment = 0.35; //35% of the interest payment
    return (housecost * interest_payment);

}

double housecost(double mortgage, double taxrate)
/*********************************************************************
Function Name: cost
Purpose:   Calculate the cost of the house for the first year of ownership
Arguments: mortgage, taxreate
Returns:  cost of the house for first year of ownership

*********************************************************************/
{
    if (DEBUG)
       {
    cout <<"The cost of a new house for the first year of ownership is "
         <<housecost<<endl;
    }
    return (mortgage - taxrate);

}

I dont know what wrong with this program, can someone help???
0
cuong5985
Asked:
cuong5985
  • 2
1 Solution
 
r-kCommented:
Have you really tried compiling and running this program? Please post the exact errors and problems. The question is too vague otherwise.
0
 
itsmeandnobodyelseCommented:
>>>> I dont know what wrong with this program,

Some function argument names are different from the variables you are using in the body of the function, e. g.

>>>> double taxrate(double price)   // argument is named 'price'
>>>> {
>>>>    ....
>>>>       return (housecost * interest_payment;  // here you are using 'housecost' instead

BTW, the tax saving is 35% of the total interest paying and *not* 35% of house costs.

You have to check your program that argument names are matching to the variables used for calculation. Then the prog should compile and link (if not, post the errors here with wrong code lines) and you could find out if your calculations give correct result.

Cuong, I have doubts that you wrote that prog yourself. That must not be wrong - as most code in the world isn't an original - *BUT* you need to try to understand each statement you copied or you definitively will fail to maintain or enhance or even compile the program.

Regards, Alex
0
 
cuong5985Author Commented:
Here i change it

#include <iostream.h>
#include <stdlib.h>
#define DEBUG true

double initial(double price, double down_payment);
double mortgage(double initial);
double taxrate(double housecost, double interest_payment);
double housecost(double mortgageCost, double taxrate);

int main()
{
   char ans;
   double price, down_payment;
   double initial, mortgageCost, tax, cost;

   cout.setf(ios::fixed);
   cout.setf(ios::showpoint);
   cout.precision(2);

   do
   {
        //Get input from the user
      cout << "Enter the price of the house: ";
      cin >> price;
      cout << "Enter your down payment): ";
      cin >>down_payment;

      //Output result
      cout <<"The initial loan balance is "<<initial(price, down_payment)<<endl;
      cout <<"The annual mortgage cost is "<<mortgage(initial)<<endl;
      cout <<"The tax saving is "<<taxrate(price)<<endl;
      cout <<"The cost of a new house for the first year of ownership is "<<housecost(mortgage, taxrate);

      //Ask user if wanting to calculate again
      cout <<"Start it again?\n"
           <<"Press y for yes, no for no,\n"
           <<"and then press return: ";
      cin >>ans;
      cout <<endl;

      } while (ans == 'y' || ans == 'Y');

      system("PAUSE");
    return 0;
}

double initial(double price, double down_payment)
/*********************************************************************
Function Name:  initial
Purpose:   Calculate the initial loan
Arguments:   price, down_payment
Returns:  initial

*********************************************************************/
{
    if (DEBUG)
       {
    cout <<"The initial loan balance is: "<<initial;
    cout <<endl;
    }
    return (price - down_payment);
}

double mortgageCost (double initial)
/*********************************************************************
Function Name: mortgage_cost
Purpose:   Calculate the mortgae cost
Arguments: initial_loan
Returns:  mortgage cost

*********************************************************************/
{
    if (DEBUG)
    {
    cout <<"The annual mortgage cost is: "<<mortgage(initial);
    cout <<endl;
    }
    const double loan_balance = 0.03; //3% of initial loan balance
    const double loan_interest = 0.06; //6% of the initial loan balance in intersest
    double total;
    loan_balance = (initial_loan * 0.03);
    loan_interest = (initial_loan * 0.06);
    total = loan_balance + loan_interest;
    return (total);

}


double taxrate(double housecost, double interest_payment)
/*********************************************************************
Function Name:  tax
Purpose:   Calculate the tax
Arguments:  price
Returns:   tax

*********************************************************************/
{
    if (DEBUG)
    {
    cout <<"The tax saving is "<<taxrate;
    cout <<endl;
    }

    const double interest_payment = 0.35; //35% of the interest payment
    return (housecost * interest_payment);

}

double housecost(double mortgage, double taxrate)
/*********************************************************************
Function Name: cost
Purpose:   Calculate the cost of the house for the first year of ownership
Arguments: mortgage, taxreate
Returns:  cost of the house for first year of ownership

*********************************************************************/
{
    if (DEBUG)
       {
    cout <<"The cost of a new house for the first year of ownership is "
         <<housecost<<endl;
    }
    return (mortgage - taxrate);

}

Here is errors
36 h:\cs2\csfunc~1\projec~1.cpp       `initial' cannot be used as a function
14 h:\cs2\csfunc~1\projec~1.cpp        too few arguments to function `double taxrate(double, double)'
38 h:\cs2\csfunc~1\projec~1.cpp        at this point in file
39 h:\cs2\csfunc~1\projec~1.cpp       argument passing to `double' from `double (*)(double)'
88 h:\cs2\csfunc~1\projec~1.cpp       `initial_loan' undeclared (first use this function)
88 h:\cs2\csfunc~1\projec~1.cpp        (Each undeclared identifier is reported only once
88 h:\cs2\csfunc~1\projec~1.cp          for each function it appears in.)
111 h:\cs2\csfunc~1\projec~1.cpp      declaration of `interest_payment' shadows a parameter





0
 
itsmeandnobodyelseCommented:
>>> `initial' cannot be used as a function

There is a function and a variable both called initial. Better use different names.

>>> too few arguments to function `double taxrate(double, double)'

The function taxRate is defined with two arguments:  double housecost, double interest_payment. But you are passing only one argument at

                                                             |
                                                             v
     cout <<"The tax saving is "<<taxrate(price)<<endl;

Note, the price/housecost issue I already mentioned in my last comment.
 
>>>>   at this point in file

That belongs to the error before.

>>>>  argument passing to `double' from `double (*)(double)'

You are calling function housecost and pass 'taxrate' as second argument. However taxrate is a function and *not* a variable. So actually you are trying to pass a function pointer (that is of type double(*)(double)).  You either need to save taxrate value in a separate variable or call it again with all arguments:

     cout <<"The cost of a new house for the first year of ownership is "<<housecost(mortgage, taxrate(price, 0.06));

>>>> `initial_loan' undeclared (first use this function)

The argument of function mortgageCost is 'initial' and *not* 'initial_loan'

>>>>  (Each undeclared identifier is reported only once  for each function it appears in.)

That belongs to the same error as you are using 'initial_loan' more than once.

>>>> declaration of `interest_payment' shadows a parameter

variable interest_payment is defined as argument *and* as const int variable. You have to choose a different name.



Cuong, none of these errors really was difficult. At least the double names (or wrong names) you should be able to find yourself.

Note, the calculations of taxrate and housecost are wrong. Worse, taxrate is a constant (35%) and not a function. Maybe you mean taxsavings ? taxsavings is 35% of interest and interest is 6% of credit (price - down_payment).

Regards, Alex





0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now