Hey guys,

I need help with this program which I have a function that calculates this formula for total house cost and then have an instruction function that asks for the input, then in main has the output. Can anyone tell me waht I am doing wrong, I think I have to return something in the instructions function but I don't know what! Please help! Thanks!

``````#include <iostream>
#include <cmath>
using namespace std;

float totalHouseCost (float initial, float fuel, float taxRate)
{
float totalFuelCost = fuel * 5; // Fuel for 5 years
float totalTaxes = taxRate * initial * 5; // Taxes for 5 years
float totalCost = initial + totalFuelCost + totalTaxes; /*Initial House cost
+ fuel cost for 5 years + Tax Rate for 5 years */

}

void displayInstructions ()   //defining instructions
{
float initial, fuel, taxRate;

cout << "The following program will display 3 homes." << endl;
cout << "Determine which is the best buy after 5 years of cost." << endl;
cout << " Enter initial cost of house" << endl;
cin >> initial;
cout << " Enter fuel cost of house for 5 years" << endl;
cin >> fuel;
cout << " Enter tax rate of house for 5 years" << endl;
cin >> taxRate;

}

int main()
{

float initial, fuel, taxRate;
displayInstructions(); //call on instructions for user

cout << "House of intial cost of " << initial << ", annual fuel cost "
<< fuel << " and annual tax rate" << endl;
cout << taxRate << " Has a 5 year cost of: " << totalHouseCost(initial, fuel, taxRate) << endl;

cout << "\n\n\n\n";
cout << "\n\n\n\n";
cout << "Jordan McGehee Lab2Pb8JM.cpp  " << endl;
cout << "Lab 2 Problem 8 pg. 159 Due 03-03-08 " << endl;

system ("pause");

return 0;
}//end main``````
``````float totalHouseCost (float initial, float fuel, float taxRate)
{
float totalFuelCost = fuel * 5; // Fuel for 5 years
float totalTaxes = taxRate * initial * 5; // Taxes for 5 years
float totalCost = initial + totalFuelCost + totalTaxes; /*Initial House cost
+ fuel cost for 5 years + Tax Rate for 5 years */

}``````

You need to return a float in this function. You have a variable that you calculate (totalCost), but then do nothing with it, so presumably you are supposed to return it:

``````float totalHouseCost (float initial, float fuel, float taxRate)
{
float totalFuelCost = fuel * 5; // Fuel for 5 years
float totalTaxes = taxRate * initial * 5; // Taxes for 5 years
float totalCost = initial + totalFuelCost + totalTaxes; /*Initial House cost
+ fuel cost for 5 years + Tax Rate for 5 years */

}``````

I think in the function

``````void displayInstructions ()
//You should be passing
//these parameters by reference
//float initial, fuel, taxRate;
//since you seem to use those values in
// float totalHouseCost (float initial, float fuel, float taxRate)

//and in

float totalHouseCost (float initial, float fuel, float taxRate)

So returning totalCost in the function. Whay am I still getting garbage for the answer?

``````#include <iostream>
#include <cmath>
using namespace std;

float totalHouseCost (float initial, float fuel, float taxRate)
{
float totalFuelCost = fuel * 5; // Fuel for 5 years
float totalTaxes = taxRate * initial * 5; // Taxes for 5 years
float totalCost = initial + totalFuelCost + totalTaxes; /*Initial House cost
+ fuel cost for 5 years + Tax Rate for 5 years */

}

void displayInstructions ()   //defining instructions
{
float initial, fuel, taxRate;

cout << "The following program will display 3 homes." << endl;
cout << "Determine which is the best buy after 5 years of cost." << endl;
cout << " Enter initial cost of house" << endl;
cin >> initial;
cout << " Enter fuel cost of house for 5 years" << endl;
cin >> fuel;
cout << " Enter tax rate of house for 5 years" << endl;
cin >> taxRate;

}

int main()
{

float initial, fuel, taxRate;
displayInstructions(); //call on instructions for user

cout << "House of intial cost of " << initial << ", annual fuel cost "
<< fuel << " and annual tax rate" << endl;
cout << taxRate << " Has a 5 year cost of: " << totalHouseCost( initial,  fuel,  taxRate) << endl;

system ("pause");

return 0;
}//end main``````

Whay am I still getting garbage for the answer?

Because you did not initialize your variables
Suppose you change
void displayInstructions ()
as

``````void displayInstructions (float &initial, float &fuel, float &taxRate)
//and delete this line(below)
//float initial, fuel, taxRate;``````

call it as::

``````int main()
{

float initial, fuel, taxRate;
displayInstructions(initial,fuel,taxRate); //call on instructions for user
.
.
.``````

Does that work ?

I am still getting garbage.

``````#include <iostream>
#include <cmath>
using namespace std;

float totalHouseCost (float initial, float fuel, float taxRate)
{
float totalFuelCost = fuel * 5; // Fuel for 5 years
float totalTaxes = taxRate * initial * 5; // Taxes for 5 years
float totalCost = initial + totalFuelCost + totalTaxes; /*Initial House cost
+ fuel cost for 5 years + Tax Rate for 5 years */

}

void displayInstructions (float initial, float fuel, float taxRate)   //defining instructions

{

cout << "The following program will display 3 homes." << endl;
cout << "Determine which is the best buy after 5 years of cost." << endl;
cout << " Enter initial cost of house" << endl;
cin >> initial;
cout << " Enter fuel cost of house for 5 years" << endl;
cin >> fuel;
cout << " Enter tax rate of house for 5 years" << endl;
cin >> taxRate;

}

int main()
{

float initial, fuel, taxRate;
displayInstructions(initial,fuel,taxRate); //call on instructions for user

cout << "House of intial cost of " << initial << ", annual fuel cost "
<< fuel << " and annual tax rate" << endl;
cout << taxRate << " Has a 5 year cost of: " << totalHouseCost( initial,  fuel,  taxRate) << endl;

system ("pause");

return 0;
}//end main``````
``````void displayInstructions (float &initial, float &fuel, float &taxRate)
//there is an ampersand

{

cout << "The following program will display 3 homes." << endl;
cout << "Determine which is the best buy after 5 years of cost." << endl;
cout << " Enter initial cost of house" << endl;
cin >> initial;
cout << " Enter fuel cost of house for 5 years" << endl;
cin >> fuel;
cout << " Enter tax rate of house for 5 years" << endl;
cin >> taxRate;

}``````

so are you saying I need to include an ampersand?

``````void displayInstructions (float &initial, float &fuel, float &taxRate)
//there is an ampersand

{

cout << "The following program will display 3 homes." << endl;
cout << "Determine which is the best buy after 5 years of cost." << endl;
cout << " Enter initial cost of house" << endl;
cin >> initial;
cout << " Enter fuel cost of house for 5 years" << endl;
cin >> fuel;
cout << " Enter tax rate of house for 5 years" << endl;
cin >> taxRate;

}``````

Sorry, quote didn't work, so are you saying I need to use an ampersand?

the ampersand worked, but why was that necessary?

Yes you need to else you work with garbage values.

May be you didn't quite get it,by using an ampersand you actually give the function true memory addresses of

initial, fuel, taxRate

to directly make changes

If you don't use them,a COPY will be created and
initial, fuel, taxRate
won't have values which you originally entered in
void displayInstructions(..)

Since you enter these values in the function

void displayInstructions(..)
and then later use it in some other computation,don't you need the actual values which were entered in the

``void displayInstructions(.....) //??``

And yes in main()
it should be

``````int main()
{

float initial, fuel, taxRate;
displayInstructions(inintial,fuel,taxRate);//No ampesrand here``````
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.