Stuck on while loop

Output should be:

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 got so far:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>
using namespace std;

int main()
{ 

//initialize variables
int miles;
int gallons;
int totalMiles;
int totalGallons;
int mpg;

while (int gallons != -1)
{
	mpg = miles / gallons;
	cout << "Enter miles or -1 to quit: ";
	cin >> miles;
}
	cout << "Enter the initial miles.";
	cin >> miles;





	cout << "Enter the amount of gallons used.";
	cin >> gallons;

return 0;
}




As you can see I have no idea how to get started on my while loop body. or the calculation. I know its got to be totalMiles / totalGallons for MPG, but I dont know how to make that into C++. I did my pseudocode if you want to see it.

Last edited on
Let me see if I can help you out with this. I think I remember this exercise from
a book I read, C++ How to Program maybe? Anyways, there is several errors in your
code. The first one I noticed, you can't declare and initialize a variable in a
while statment, only for loops. I'm just gonna write out the code and add some
comments...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60

#include <iostream>
using namespace std;

int main()
{
	// variable for total MPG
	float totalMPG = 0;
	// loop counter to keep track of # of trips
	int iCounter = 0;
	// average MPG for all trips
	float averageMPG = 0;

	// create a variable to store the miles used and initialize it
	int milesUsed = 0;

	// prompt the user for input (-1 is called a sentinel value)
	cout << "Enter the miles used (-1 to quit): ";
	// get the users input
	cin >> milesUsed;
	
	// create our while loop and check if user entered the sentinel value
	while(milesUsed != -1)	// while mileUsed doesn't equal -1, loop...
	{
		// loop counter
		iCounter++;

		// create a variable for the mpg on this trip
		float MPG = 0;

		// create a variable to store the gallons used and initialize it
		int gallonsUsed = 0;
		
		// prompt to see how many gallons were used
		cout << "\nEnter gallons: ";
		// get users input
		cin >> gallonsUsed;

		// lets calculate the mpg (we have to cast gallonsUsed so expression is evaluated
		// as a float)
		MPG = static_cast<float>gallonsUsed / milesUsed;
		
		// add MPG for this trip to the total MPG
		totalMPG =+ MPG; 
		
		// print how many mpg user got for trip
		cout << "\nMPG this tankful: " << MPG << endl;

		// calculate total mpg
		averageMPG = totalMPG / static_cast<float>iCounter;
		
		// print total mpg so far...
		cout << "Total MPG: " << totalMPG << endl;

		// lets prompt the user for the next iteration of this loop
		cout << "Enter the miles used (-1 to quit): ";
		// get the users input
		cin >> milesUsed;
	}
}


I didn't test this code but it should point you in the right direction... If you have any questions about it please let me know...
Also, a do { ... } while() loop would be better for this sort of thing
Thanks for taking the time! I appreciate it. Yes, that is the book.

I've read your comments and they very well written, but I'm still confused on a few parts.

averageMPG = totalMPG / static_cast<float>iCounter;

What does static_cast do? Why is there a <float> in there? and is the loop counter supposed to be at the end? If so, why? Is there an alternative in using the static_cast?

1
2
// loop counter to keep track of # of trips
	int iCounter = 0;

Do I need this? The output doesn't show I need to keep track of # of trips. Just current MPG and averageMPG.


On the downside there were some syntax errors and didn't run, but the ; look like they are there so that's out of my knowledge and not sure how to fix it.
. C++ can sometimes be so frustrating! I appreciate it so much for you taking the time. Hopefully one day I can help others too.
No problem, you'll get the hang of it! I'm a beginner myself... Take your time to fix the syntax errors and understand how the program works.

I'll answer the 2nd question first. I put iCounter to keep track of how many time the loop runs
so we can get the average MPG of all the trips. average = total / number of trips.

for the 1st question
averageMPG is data type float and so is totalMPG
iCounter is an int data type so we need to *cast* it to a float data type which means that it's
still an int data type but it's evaluated as a float in the expression.

Hope this helps, have a little fun with it. You gotta enjoy programming :)
Thanks!!

So if averageMPG and totalMPG are ints (which my teacher said is fine) I wouldn't need the static_cast? (because I didn't learn that yet)

I simply remove it and change change the float data types to ints?
Yep, just change all the float values to ints and you're good to go...
Curious, why cant gallons used or the counter be floats? Why must you cast it as floats? Counter doesn't have decimals numbers right? so isn't it fine being an int?


By the way I finally got it to work!! Thanks DangerD!
Topic archived. No new replies allowed.