| | |
Calculation problems..
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Oct 2009
Posts: 5
Reputation:
Solved Threads: 0
This program is suppose to display MPG's (total), and the combined MPG of all the total MPG's you input.
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
C++ Syntax (Toggle Plain Text)
// Exercise 4. 12: ex04_12.cpp #include <iostream> using namespace std; int main() { double mpg; double gallons; double combinedMpg = 1; double total; while ( mpg != -1 ) { cout << "\nEnter miles (Enter -1 to quit): "; cin >> mpg; if ( mpg != -1) { cout << "Enter gallons: "; cin >> gallons; } cout << "\nMPG this tankful: " << (total = (mpg / gallons)) << endl; cout << "Total MPG: " << (combinedMpg = total / combinedMpg ) << endl; } }
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.
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".
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
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.
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.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
•
•
Join Date: Oct 2009
Posts: 5
Reputation:
Solved Threads: 0
0
#3 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 / gallonsbetter 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 sayingoverall_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.
•
•
Join Date: Oct 2009
Posts: 5
Reputation:
Solved Threads: 0
0
#4 Nov 4th, 2009
still couldnt get it. I revised it and this is where i'm at.
C++ Syntax (Toggle Plain Text)
#include <iostream> using namespace std; int main() { double miles; double gallons; double combinedMpg = 1; double mpg; double averageMpgs; while ( mpg != -1 ) { cout << "\nEnter miles (Enter -1 to quit): "; cin >> miles; if ( miles != -1) { cout << "Enter gallons: "; cin >> gallons; cout << "\nMPG this tankful: " << (mpg = (miles / gallons)) << endl; cout << "Total MPG: " << (averageMpgs = mpg / combinedMpg ) << endl; } averageMpgs += combinedMpg; } }
![]() |
Similar Threads
- Wage / employee self generated list / calculation (C++)
- Problems with Pi and also Calculation Problem (Java)
- Demographic database design help (Database Design)
- Problems with reading from a sequential file (C++)
- I'm having problems (Java)
- PDB analysing (Perl)
- First JLIST, used with GUI Inventory (Java)
- No time... bitch load of problems (Windows NT / 2000 / XP)
Other Threads in the C++ Forum
- Previous Thread: operator= changes value just for scope of function
- Next Thread: How do I set shared ptr to NULL
Views: 200 | Replies: 3
| Thread Tools | Search this Thread |
Tag cloud for C++
6 api application array arrays based beginner binary c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete developer display dll dynamiccharacterarray email encryption error file format forms fstream function functions game generator givemetehcodez graph iamthwee ifstream image input int java lib list loop looping loops map math matrix memory multiple newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg search simple sort sorting spoonfeeding string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






