943,657 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 795
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Mar 30th, 2009
0

Help with House Program

Expand Post »
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!

C++ Syntax (Toggle Plain Text)
  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
Similar Threads
Reputation Points: 17
Solved Threads: 0
Junior Poster in Training
songweaver is offline Offline
80 posts
since Mar 2009
Mar 30th, 2009
0

Re: Help with House Program

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;
}
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,372 posts
since Jan 2008
Mar 30th, 2009
0

Re: Help with House Program

I think in the function
C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 53
Solved Threads: 13
Light Poster
zalezog is offline Offline
47 posts
since Oct 2008
Mar 30th, 2009
0

Re: Help with House Program

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

C++ Syntax (Toggle Plain Text)
  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
Reputation Points: 17
Solved Threads: 0
Junior Poster in Training
songweaver is offline Offline
80 posts
since Mar 2009
Mar 30th, 2009
0

Re: Help with House Program

Quote ...
Whay am I still getting garbage for the answer?
Because you did not initialize your variables
Suppose you change
void displayInstructions ()
as
C++ Syntax (Toggle Plain Text)
  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::
C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 53
Solved Threads: 13
Light Poster
zalezog is offline Offline
47 posts
since Oct 2008
Mar 30th, 2009
0

Re: Help with House Program

I am still getting garbage.

C++ Syntax (Toggle Plain Text)
  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
Reputation Points: 17
Solved Threads: 0
Junior Poster in Training
songweaver is offline Offline
80 posts
since Mar 2009
Mar 30th, 2009
0

Re: Help with House Program

C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 53
Solved Threads: 13
Light Poster
zalezog is offline Offline
47 posts
since Oct 2008
Mar 30th, 2009
0

Re: Help with House Program

so are you saying I need to include an ampersand?
Click to Expand / Collapse  Quote originally posted by zalezog ...
C++ Syntax (Toggle Plain Text)
  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. }
Reputation Points: 17
Solved Threads: 0
Junior Poster in Training
songweaver is offline Offline
80 posts
since Mar 2009
Mar 30th, 2009
0

Re: Help with House Program

Sorry, quote didn't work, so are you saying I need to use an ampersand?
Reputation Points: 17
Solved Threads: 0
Junior Poster in Training
songweaver is offline Offline
80 posts
since Mar 2009
Mar 30th, 2009
0

Re: Help with House Program

the ampersand worked, but why was that necessary?
Reputation Points: 17
Solved Threads: 0
Junior Poster in Training
songweaver is offline Offline
80 posts
since Mar 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Help with error C2228:
Next Thread in C++ Forum Timeline: Dynamic Memory Allocation





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC