Function help

Hi I am having a lot of trouble with this code. The object is:
In shopping for a new house, you must consider several factors. In this problem the initial cost of the house, estimated annual fuel costs, and annual tax rate are available. Write a program that will determine the total cost after a five-year period for each set of house data below. You should be able to inspect your program output to determine the “best buy”. Your program must run through three houses for comparison, each time it is run (do not run it three times for three houses).

To calculate the house cost, add the fuel cost for five years to the initial cost, then add the taxes for five years. Taxes for one year are computed by multiplying the tax rate by the initial cost and dividing by 100.

Write and call a function for each of the following tasks:
• Display instructions to the user (call it disp( ) )
• Compute and return the tax amount for one year (call it calcTax( ) )
• Compute and return the fuel cost for a five-year period (call it calcFuel( ) )
• Compute and return the total house cost given the initial cost, the five-year fuel cost, and the annual tax amount (call it calcTot( ) )
• Print the output (call it printResult( ) )

What I have is:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
 #include <iostream>
using namespace std;
int main()
{
  char h;
  double initialcost, fuelcost, taxrate;
  void disp(char&, double&, double&);  //Display instruction                                                                                                                                     
  void calcTax(double, double); //Compute and Return Tax Amount                                                                                                                                  
  void calcFuel(double, double); //Compute and Return Fuel Cost for 5 Years                                                                                                                      
  void calcTot(double, double); //Compute and Return Total House Cost                                                                                                                            
  //void printResult(); //Print the Output                                                                                                                                                       

}

void disp(double& n1 , double& n2, double& n3)
{
  cout<< "Comparing House Prices" <<endl;
  cout<<"Enter Initial House Cost: " ;
  cin>> n1;
  cout<<"Enter Initial Fuel Cost: " <<endl;
  cin>> n2;
  cout<<"Enter Annual Tax Rate: " << "%" << endl;
  cin>>n3;
}
void calcTax (double n1, double n3)
{
  calcTax=(n3*n1)/100;
  cout << "Tax Amount (for 1 year): " <<calcTax <<endl;
}
void calcFuel (double n1, double n2)
{
  calcFuel= (n1 * n2)* 5;
  cout << "Fuel Cost (for 5 years): " <<calcFuel <<endl;
}
void calcTot(double n1, double n3)
{
  calcTot= (calcFuel+n1) + n3;
  cout << "Total House Cost: " <<calcTot << endl;
}




and I keep getting these errors:

program4.cpp:37:10: error: non-object type 'void (double, double)' is not assignable
calcTax=(n3*n1)/100;
~~~~~~~^
program4.cpp:38:41: warning: address of function 'calcTax' will always evaluate to 'true' [-Wpointer-bool-conversion]
cout << "Tax Amount (for 1 year): " <<calcTax <<endl;
~~^~~~~~~
program4.cpp:38:41: note: prefix with the address-of operator to silence this warning
cout << "Tax Amount (for 1 year): " <<calcTax <<endl;
^
&
program4.cpp:42:11: error: non-object type 'void (double, double)' is not assignable
calcFuel= (n1 * n2)* 5;
~~~~~~~~^
program4.cpp:43:41: warning: address of function 'calcFuel' will always evaluate to 'true' [-Wpointer-bool-conversion]
cout << "Fuel Cost (for 5 years): " <<calcFuel <<endl;
~~^~~~~~~~
program4.cpp:43:41: note: prefix with the address-of operator to silence this warning
cout << "Fuel Cost (for 5 years): " <<calcFuel <<endl;
^
&
program4.cpp:47:21: error: invalid operands to binary expression ('void (*)(double, double)' and 'double')
calcTot= (calcFuel+n1) + n3;
~~~~~~~~^~~
program4.cpp:48:34: warning: address of function 'calcTot' will always evaluate to 'true' [-Wpointer-bool-conversion]
cout << "Total House Cost: " <<calcTot << endl;
~~^~~~~~~
program4.cpp:48:34: note: prefix with the address-of operator to silence this warning
cout << "Total House Cost: " <<calcTot << endl;


Please help!!!
Here's my advise. Try writing a line of code and test it before you write the entire program.
Add comments.

So lets start with the first function. Add a cout and test until it works.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
using namespace std;

int main()
{
  char h;
  double initialcost, fuelcost, taxrate;
  void disp(char&, double&, double&);  //Display instruction                                                                                                                                     
}

void disp(double& n1 , double& n2, double& n3)
{
  cout<< "Comparing House Prices" <<endl;
  cout<<"Enter Initial House Cost: " ;
  cin>> n1;
  cout<<"Enter Initial Fuel Cost: " <<endl;
  cin>> n2;
  cout<<"Enter Annual Tax Rate: " << "%" << endl;
  cin>>n3;
  cout << n1 << ":" << n2<< ":" << n3 << endl;
}
At line 27, calcTax is the name of a function, not a variable. You can't just assign a value to it as if it were a variable.

At lines 7 - 10, you're declaring the functions inside your main function. That means those declarations only exist within the scope of the main function. You should have those declarations at file scope instead, near the top of your file.

At lines 5 - 6, you declare a bunch of variables, but I don't see them used anywhere.

Right now, nothing in your code actually calls your disp, calcTax, calcFuel and calcTot functions.

It looks as though you haven't really understood how to write, and call, functions. I'd recommend re-reading that chapter in your textbook to get a better understanding.
Last edited on
Topic archived. No new replies allowed.