Help with House Program

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Mar 2009
Posts: 51
Reputation: songweaver is an unknown quantity at this point 
Solved Threads: 0
songweaver songweaver is offline Offline
Junior Poster in Training

Help with House Program

 
0
  #1
Mar 30th, 2009
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!

  1. #include <iostream>
  2. #include <cmath>
  3. using namespace std;
  4.  
  5. float totalHouseCost (float initial, float fuel, float taxRate)
  6. {
  7. float totalFuelCost = fuel * 5; // Fuel for 5 years
  8. float totalTaxes = taxRate * initial * 5; // Taxes for 5 years
  9. float totalCost = initial + totalFuelCost + totalTaxes; /*Initial House cost
  10.   + fuel cost for 5 years + Tax Rate for 5 years */
  11.  
  12.  
  13. }
  14.  
  15.  
  16. void displayInstructions () //defining instructions
  17. {
  18. float initial, fuel, taxRate;
  19.  
  20. cout << "The following program will display 3 homes." << endl;
  21. cout << "Determine which is the best buy after 5 years of cost." << endl;
  22. cout << " Enter initial cost of house" << endl;
  23. cin >> initial;
  24. cout << " Enter fuel cost of house for 5 years" << endl;
  25. cin >> fuel;
  26. cout << " Enter tax rate of house for 5 years" << endl;
  27. cin >> taxRate;
  28.  
  29.  
  30. }
  31.  
  32. int main()
  33. {
  34.  
  35. float initial, fuel, taxRate;
  36. displayInstructions(); //call on instructions for user
  37.  
  38.  
  39.  
  40. cout << "House of intial cost of " << initial << ", annual fuel cost "
  41. << fuel << " and annual tax rate" << endl;
  42. cout << taxRate << " Has a 5 year cost of: " << totalHouseCost(initial, fuel, taxRate) << endl;
  43.  
  44.  
  45.  
  46. cout << "\n\n\n\n";
  47. cout << "\n\n\n\n";
  48. cout << "Jordan McGehee Lab2Pb8JM.cpp " << endl;
  49. cout << "Lab 2 Problem 8 pg. 159 Due 03-03-08 " << endl;
  50.  
  51.  
  52. system ("pause");
  53.  
  54.  
  55. return 0;
  56. }//end main
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,819
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: Help with House Program

 
0
  #2
Mar 30th, 2009
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;
}
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 44
Reputation: zalezog is an unknown quantity at this point 
Solved Threads: 11
zalezog zalezog is offline Offline
Light Poster

Re: Help with House Program

 
0
  #3
Mar 30th, 2009
I think in the function
  1. void displayInstructions ()
  2. //You should be passing
  3. //these parameters by reference
  4. //float initial, fuel, taxRate;
  5. //since you seem to use those values in
  6. // float totalHouseCost (float initial, float fuel, float taxRate)
  7.  
  8. //and in
  9.  
  10. float totalHouseCost (float initial, float fuel, float taxRate)
  11.  
  12. //you must return totalCost
Last edited by zalezog; Mar 30th, 2009 at 3:13 pm.
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 51
Reputation: songweaver is an unknown quantity at this point 
Solved Threads: 0
songweaver songweaver is offline Offline
Junior Poster in Training

Re: Help with House Program

 
0
  #4
Mar 30th, 2009
So returning totalCost in the function. Whay am I still getting garbage for the answer?

  1. #include <iostream>
  2. #include <cmath>
  3. using namespace std;
  4.  
  5. float totalHouseCost (float initial, float fuel, float taxRate)
  6. {
  7. float totalFuelCost = fuel * 5; // Fuel for 5 years
  8. float totalTaxes = taxRate * initial * 5; // Taxes for 5 years
  9. float totalCost = initial + totalFuelCost + totalTaxes; /*Initial House cost
  10.   + fuel cost for 5 years + Tax Rate for 5 years */
  11. return totalCost;
  12.  
  13. }
  14.  
  15.  
  16. void displayInstructions () //defining instructions
  17. {
  18. float initial, fuel, taxRate;
  19.  
  20. cout << "The following program will display 3 homes." << endl;
  21. cout << "Determine which is the best buy after 5 years of cost." << endl;
  22. cout << " Enter initial cost of house" << endl;
  23. cin >> initial;
  24. cout << " Enter fuel cost of house for 5 years" << endl;
  25. cin >> fuel;
  26. cout << " Enter tax rate of house for 5 years" << endl;
  27. cin >> taxRate;
  28.  
  29.  
  30.  
  31. }
  32.  
  33. int main()
  34. {
  35.  
  36. float initial, fuel, taxRate;
  37. displayInstructions(); //call on instructions for user
  38.  
  39.  
  40.  
  41. cout << "House of intial cost of " << initial << ", annual fuel cost "
  42. << fuel << " and annual tax rate" << endl;
  43. cout << taxRate << " Has a 5 year cost of: " << totalHouseCost( initial, fuel, taxRate) << endl;
  44.  
  45. system ("pause");
  46.  
  47.  
  48. return 0;
  49. }//end main
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 44
Reputation: zalezog is an unknown quantity at this point 
Solved Threads: 11
zalezog zalezog is offline Offline
Light Poster

Re: Help with House Program

 
0
  #5
Mar 30th, 2009
Whay am I still getting garbage for the answer?
Because you did not initialize your variables
Suppose you change
void displayInstructions ()
as
  1. void displayInstructions (float &initial, float &fuel, float &taxRate)
  2. //and delete this line(below)
  3. //float initial, fuel, taxRate;
and in your main()
call it as::
  1. int main()
  2. {
  3.  
  4. float initial, fuel, taxRate;
  5. displayInstructions(initial,fuel,taxRate); //call on instructions for user
  6. .
  7. .
  8. .
Does that work ?
Last edited by zalezog; Mar 30th, 2009 at 3:49 pm.
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 51
Reputation: songweaver is an unknown quantity at this point 
Solved Threads: 0
songweaver songweaver is offline Offline
Junior Poster in Training

Re: Help with House Program

 
0
  #6
Mar 30th, 2009
I am still getting garbage.

  1. #include <iostream>
  2. #include <cmath>
  3. using namespace std;
  4.  
  5. float totalHouseCost (float initial, float fuel, float taxRate)
  6. {
  7. float totalFuelCost = fuel * 5; // Fuel for 5 years
  8. float totalTaxes = taxRate * initial * 5; // Taxes for 5 years
  9. float totalCost = initial + totalFuelCost + totalTaxes; /*Initial House cost
  10.   + fuel cost for 5 years + Tax Rate for 5 years */
  11. return totalCost;
  12.  
  13. }
  14.  
  15.  
  16. void displayInstructions (float initial, float fuel, float taxRate) //defining instructions
  17.  
  18. {
  19.  
  20.  
  21. cout << "The following program will display 3 homes." << endl;
  22. cout << "Determine which is the best buy after 5 years of cost." << endl;
  23. cout << " Enter initial cost of house" << endl;
  24. cin >> initial;
  25. cout << " Enter fuel cost of house for 5 years" << endl;
  26. cin >> fuel;
  27. cout << " Enter tax rate of house for 5 years" << endl;
  28. cin >> taxRate;
  29.  
  30.  
  31.  
  32. }
  33.  
  34. int main()
  35. {
  36.  
  37. float initial, fuel, taxRate;
  38. displayInstructions(initial,fuel,taxRate); //call on instructions for user
  39.  
  40.  
  41.  
  42. cout << "House of intial cost of " << initial << ", annual fuel cost "
  43. << fuel << " and annual tax rate" << endl;
  44. cout << taxRate << " Has a 5 year cost of: " << totalHouseCost( initial, fuel, taxRate) << endl;
  45.  
  46. system ("pause");
  47.  
  48.  
  49. return 0;
  50. }//end main
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 44
Reputation: zalezog is an unknown quantity at this point 
Solved Threads: 11
zalezog zalezog is offline Offline
Light Poster

Re: Help with House Program

 
0
  #7
Mar 30th, 2009
  1. void displayInstructions (float &initial, float &fuel, float &taxRate)
  2. //there is an ampersand
  3.  
  4. {
  5.  
  6.  
  7. cout << "The following program will display 3 homes." << endl;
  8. cout << "Determine which is the best buy after 5 years of cost." << endl;
  9. cout << " Enter initial cost of house" << endl;
  10. cin >> initial;
  11. cout << " Enter fuel cost of house for 5 years" << endl;
  12. cin >> fuel;
  13. cout << " Enter tax rate of house for 5 years" << endl;
  14. cin >> taxRate;
  15.  
  16.  
  17.  
  18. }
Last edited by zalezog; Mar 30th, 2009 at 4:08 pm.
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 51
Reputation: songweaver is an unknown quantity at this point 
Solved Threads: 0
songweaver songweaver is offline Offline
Junior Poster in Training

Re: Help with House Program

 
0
  #8
Mar 30th, 2009
so are you saying I need to include an ampersand?
Originally Posted by zalezog View Post
  1. void displayInstructions (float &initial, float &fuel, float &taxRate)
  2. //there is an ampersand
  3.  
  4. {
  5.  
  6.  
  7. cout << "The following program will display 3 homes." << endl;
  8. cout << "Determine which is the best buy after 5 years of cost." << endl;
  9. cout << " Enter initial cost of house" << endl;
  10. cin >> initial;
  11. cout << " Enter fuel cost of house for 5 years" << endl;
  12. cin >> fuel;
  13. cout << " Enter tax rate of house for 5 years" << endl;
  14. cin >> taxRate;
  15.  
  16.  
  17.  
  18. }
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 51
Reputation: songweaver is an unknown quantity at this point 
Solved Threads: 0
songweaver songweaver is offline Offline
Junior Poster in Training

Re: Help with House Program

 
0
  #9
Mar 30th, 2009
Sorry, quote didn't work, so are you saying I need to use an ampersand?
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 51
Reputation: songweaver is an unknown quantity at this point 
Solved Threads: 0
songweaver songweaver is offline Offline
Junior Poster in Training

Re: Help with House Program

 
0
  #10
Mar 30th, 2009
the ampersand worked, but why was that necessary?
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC