Click here to Skip to main content
15,883,957 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
HERE IS THE QUESTION.

Enter the miles used (-1 to quit): 287
Enter gallons: 13
MPG this tankful: 22.076923
Total MPG: 22.076923
Enter the miles used (-1 to quit): 200
Enter gallons: 10
MPG this tankful: 20.000000
Total MPG: 21.173913
Enter the miles used (-1 to quit): 120
Enter gallons: 5
MPG this tankful: 24.000000
Total MPG: 21.678571
Enter miles (-1 to quit): -1


What I have tried:

C++
#include <iostream>
#include "conio.h"
using namespace std;
void main()

{
	cout.setf(ios::fixed);
	cout.setf(ios::showpoint);
	cout.precision(6);

	float miles = 0; float gallons = 0; float milage = 0; float TMG = 0; float TM = 0; float TG = 0;

	cout << "Enter Miles(-1 to quit): ";
	cin >> miles;
	while (miles != -1)
	{
		
		cout << "Enter gallons: ";
		cin >> gallons;
		milage = miles / gallons;
		cout << "Milage this tankful is: " << milage << endl;
		
		if (TMG == 0)
		{
			cout << "Total Milage is: " << milage << endl;
		}
		else
		{
			cout << "Total Milage is: " << TMG << endl;
		}
		
		TM = TM + miles;
		TG = TG + gallons;
		TMG = TM / TG;
		cout << "Enter Miles(-1 to quit): ";
		cin >> miles;
	}

	_getche();
}
Posted
Updated 22-Oct-17 2:48am
v2

Store your miles and gallons only: and store them as integers - that's what the user inputs them as.
So you need four variables: tripMiles, tripGallons, totalMiles, totalGallons.
Each time the user enters trip values, print the MPG from them:
C++
cout << "MPG this tankful is: " << (float) tripMiles / (float(tripGallons) << endl;
And add the trip values to the totals. Output the total MPG in a similar way:
C++
cout << "Total MPG: " << (float) totalMiles / (float(totalGallons) << endl;


Other than that, you need to re-read the miles each time round the loop, not just outside it.
 
Share this answer
 
v2
Comments
Member 13478360 22-Oct-17 6:21am    
I'm sorry I didn't get it. Can you please edit it in the code and then paste it.
OriginalGriff 22-Oct-17 6:37am    
No, because that would be doing your homework for you, and we don't do that.
You have all the clues you need, just a little thinking will get it together.
Member 13478360 22-Oct-17 6:43am    
can you then please elaborate on my error?
CPallini 22-Oct-17 8:55am    
5.
Quote:
What are my mistakes. Since I can't get the correct answer.

Lets guess, you get second answer after third input, fourth answer after fifth input.
Things are misplaced in your code. Use the debugger to see what is done and when.

There is a tool that allow you to see what your code is doing, its name is debugger. It is also a great learning tool because it show you reality and you can see which expectation match reality.
When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.
 
Share this answer
 
Comments
Member 13478360 22-Oct-17 6:28am    
No that is not the issue. If i enter the same values as in the question, the answer i get is not the same
Patrice T 22-Oct-17 6:40am    
'not the same' is not informative.
Show the results you get. Not everyone have a c++ compiler ready to run.
CPallini 22-Oct-17 8:55am    
5.
Patrice T 22-Oct-17 9:16am    
thank you

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900