#include <iostream>
usingnamespace 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.
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...
#include <iostream>
usingnamespace 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
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 :)
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!