| | |
Help with House Program
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Mar 2009
Posts: 51
Reputation:
Solved Threads: 0
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!
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!
C++ Syntax (Toggle Plain Text)
#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
•
•
Join Date: Jan 2008
Posts: 3,819
Reputation:
Solved Threads: 501
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 */ return totalCost; }
•
•
Join Date: Oct 2008
Posts: 44
Reputation:
Solved Threads: 11
I think in the function
C++ Syntax (Toggle Plain Text)
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) //you must return totalCost
Last edited by zalezog; Mar 30th, 2009 at 3:13 pm.
•
•
Join Date: Mar 2009
Posts: 51
Reputation:
Solved Threads: 0
So returning totalCost in the function. Whay am I still getting garbage for the answer?
C++ Syntax (Toggle Plain Text)
#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 */ return totalCost; } 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
•
•
Join Date: Oct 2008
Posts: 44
Reputation:
Solved Threads: 11
•
•
•
•
Whay am I still getting garbage for the answer?
Suppose you change
void displayInstructions ()
as
C++ Syntax (Toggle Plain Text)
void displayInstructions (float &initial, float &fuel, float &taxRate) //and delete this line(below) //float initial, fuel, taxRate;
call it as::
C++ Syntax (Toggle Plain Text)
int main() { float initial, fuel, taxRate; displayInstructions(initial,fuel,taxRate); //call on instructions for user . . .
Last edited by zalezog; Mar 30th, 2009 at 3:49 pm.
•
•
Join Date: Mar 2009
Posts: 51
Reputation:
Solved Threads: 0
I am still getting garbage.
C++ Syntax (Toggle Plain Text)
#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 */ return totalCost; } 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
•
•
Join Date: Oct 2008
Posts: 44
Reputation:
Solved Threads: 11
C++ Syntax (Toggle Plain Text)
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; }
Last edited by zalezog; Mar 30th, 2009 at 4:08 pm.
•
•
Join Date: Mar 2009
Posts: 51
Reputation:
Solved Threads: 0
so are you saying I need to include an ampersand?
•
•
•
•
C++ Syntax (Toggle Plain Text)
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; }
![]() |
Similar Threads
- What's the HARDEST program you've written? (Computer Science)
- Need help with the 5 mouse click house program (Python)
- program error for cd burner program (Windows NT / 2000 / XP)
- SLOT MACHINE PROGRAM... Help plsss! (C)
- TROJ STILEN.A, ADW RULEDOR.C, BKDR SANDBOX.S detected using Trend Micro House Call (Viruses, Spyware and other Nasties)
Other Threads in the C++ Forum
- Previous Thread: factorial using a for loop
- Next Thread: Dynamic Memory Allocation
| Thread Tools | Search this Thread |
api array arrays based beginner binary c++ c/c++ calculator char char* class classes code compile compiler console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news number numbertoword output parameter pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






