Calculation problems..

Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Oct 2009
Posts: 5
Reputation: BenJammin89 is an unknown quantity at this point 
Solved Threads: 0
BenJammin89 BenJammin89 is offline Offline
Newbie Poster

Calculation problems..

 
0
  #1
Nov 4th, 2009
This program is suppose to display MPG's (total), and the combined MPG of all the total MPG's you input.
  1. // Exercise 4. 12: ex04_12.cpp
  2. #include <iostream>
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7. double mpg;
  8. double gallons;
  9. double combinedMpg = 1;
  10. double total;
  11.  
  12.  
  13. while ( mpg != -1 )
  14. {
  15. cout << "\nEnter miles (Enter -1 to quit): ";
  16. cin >> mpg;
  17.  
  18. if ( mpg != -1)
  19. {
  20. cout << "Enter gallons: ";
  21. cin >> gallons;
  22. }
  23. cout << "\nMPG this tankful: " << (total = (mpg / gallons)) << endl;
  24. cout << "Total MPG: " << (combinedMpg = total / combinedMpg ) << endl;
  25. }
  26. }

Any help?
Output should be along these lines...I just switched some of the words.

Enter the miles used (-1 to quit): 287
Enter gal l ons: 13
MPG this tankful : 22. 076923
Total MPG: 22. 076923

Enter the miles used (-1 to quit): 200
Enter gal l ons: 10
MPG this tankful : 20. 000000
Total MPG: 21. 173913

Enter the miles used (-1 to quit): 120
Enter gal l ons: 5
MPG this tankful : 24. 000000
Total MPG: 21. 678571
Last edited by BenJammin89; Nov 4th, 2009 at 1:19 am.
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 1,679
Reputation: vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold 
Solved Threads: 193
vmanes's Avatar
vmanes vmanes is offline Offline
Posting Virtuoso
 
0
  #2
Nov 4th, 2009
First, the code would be clearer if your variable names better expressed what they were.

What's "total"? The input of distance driven should be "miles", not "mpg". mpg = miles / gallons better expresses what you're doing.

The calculation of the mpg for a given tankful should occur inside the if condition - when the miles input is -1, you don't want to do a calculation, as would happen in your code.

For the overall mpg value, simply averaging the individual mpg calculations will not be accurate. And you're not doing that correctly. (Look at your code, you're saying overall_mpg = current_mpg / overall_mpg - does that make any sense?) You should keep a running total of miles driven and gallons used, and calculate the overall mpg with those totals.

Is it in the assignment to display the running overall mpg after each entry, or are you to display the summary after all input is concluded? If the latter case, the calculation and display of overall mpg should occur outside the loop.
Everyone's gotta believe in something. I believe I'll have another drink.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 5
Reputation: BenJammin89 is an unknown quantity at this point 
Solved Threads: 0
BenJammin89 BenJammin89 is offline Offline
Newbie Poster
 
0
  #3
Nov 4th, 2009
Originally Posted by vmanes View Post
First, the code would be clearer if your variable names better expressed what they were.

What's "total"? The input of distance driven should be "miles", not "mpg". mpg = miles / gallons better expresses what you're doing.

The calculation of the mpg for a given tankful should occur inside the if condition - when the miles input is -1, you don't want to do a calculation, as would happen in your code.

For the overall mpg value, simply averaging the individual mpg calculations will not be accurate. And you're not doing that correctly. (Look at your code, you're saying overall_mpg = current_mpg / overall_mpg - does that make any sense?) You should keep a running total of miles driven and gallons used, and calculate the overall mpg with those totals.

Is it in the assignment to display the running overall mpg after each entry, or are you to display the summary after all input is concluded? If the latter case, the calculation and display of overall mpg should occur outside the loop.

haha yeah that was a dumb variable, since MPG is = miles per gallon.

ok well the assignment was to display the MPG of the entered tankful AND the average of all MPG's entered.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 5
Reputation: BenJammin89 is an unknown quantity at this point 
Solved Threads: 0
BenJammin89 BenJammin89 is offline Offline
Newbie Poster
 
0
  #4
Nov 4th, 2009
still couldnt get it. I revised it and this is where i'm at.
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main()
  5. {
  6. double miles;
  7. double gallons;
  8. double combinedMpg = 1;
  9. double mpg;
  10. double averageMpgs;
  11.  
  12. while ( mpg != -1 )
  13. {
  14. cout << "\nEnter miles (Enter -1 to quit): ";
  15. cin >> miles;
  16.  
  17. if ( miles != -1)
  18. {
  19. cout << "Enter gallons: ";
  20. cin >> gallons;
  21. cout << "\nMPG this tankful: " << (mpg = (miles / gallons)) << endl;
  22. cout << "Total MPG: " << (averageMpgs = mpg / combinedMpg ) << endl;
  23. }
  24. averageMpgs += combinedMpg;
  25. }
  26. }
Reply With Quote Quick reply to this message  
Reply

Message:




Views: 200 | Replies: 3
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC