Alright so I have the code pretty much done except for some final value outputs. Right now the code is supposed to add up total mileage driven for each interval as well as the gas used, then calculate the mpg for each interval. At the end of the program, when the signal input (-1) is entered, it's supposed to output the total mileage over the entire trip, then add up the total gallons of gas purchased over the entire trip, and then calculate the mpg over the entire trip. The problem I have is I can't seem to get it to hold the values from different intervals because when it loops through, it changes the variable to a new value. I just need to figure out how to save the previous value and add it to the new value, now that I just typed that I feel like I'm going to be able to solve it real quick but I'm still going to enter the question. So I need to figure out how to add up all the input miles driven then subtract that from the initial odometer reading which is outside the loop but inside the loop I change it. Then I need to add up the total amount of gallons of gas purchased. From there I can finish but I just can't figure out how to store the previous gallons or previous mileage. Here's my code so far:
#include <iostream>
#include <iomanip>
usingnamespace std;
int main()
{
cout << "This program calculates mileage." << endl;
cout << "Type in -1 for the odometer reading to signify the end of the program." << endl;
cout << "Enter gallons at least once." << endl;
float ior, cor, gas, mpg, tmiles, ctmiles, ctgas, tgas, iior; //floating variables so that I can control them with "setprecision".
cout << "Initial odometer reading: "; //Ask for initial odometer reading.
cin >> ior;
cout << endl;
while( true ) //awaiting a break in the loop.
{
cout << "Current odometer reading: "; //Ask for current odometer reading.
cin >> cor;
if(cor == -1) //When -1 is set as input for Current Odometer Reading, then the loop will halt and output below.
break;
cout << "Gallons of gas purchased: "; //Ask for how much gas was purchased aftering traveling (cor - ior) miles.
cin >> gas;
mpg = (cor - ior)/gas; //Calculates mpg
tmiles = cor - ior; //Calculates the total miles driven on 1 interval
ior = tmiles; //Resets odometer at previous miles driven(just like resetting to 0).
cout << "You drove " << fixed << setprecision(0) << tmiles << " miles averaging " << setprecision(2) << mpg << " mpg." << endl << endl;
}
cout << "All together, you drove " << fixed << setprecision(0) << ctmiles << " miles on ";
cout << setprecision(2) << ctgas << " gallons of gas, averaging " << setprecision(2) << (ctmiles / ctgas) << " mpg.";
}
It is double precision floating point variable, as opposed to single precision: float.
float has about 7 significant digits, when double has about 15.
You can change doubles to floats, if you wish.
I have not learned what "double" is yet
I must warn you, that whatever you use to study, it will tell you about 10% of C++. You should learn yourself too.
#include <iostream>
#include <iomanip>
usingnamespace std;
int main()
{
cout << "This program calculates mileage." << endl;
cout << "Type in -1 for the odometer reading to signify the end of the program." << endl;
cout << "Enter gallons at least once." << endl;
float ior, cor, gas, mpg, tmiles, ctmiles, ctgas, tgas, iior; //floating variables so that I can control them with "setprecision".
cout << "Initial odometer reading: "; //Ask for initial odometer reading.
cin >> ior;
cout << endl;
ctmiles = 0;
ctgas = 0;
while( true ) //awaiting a break in the loop.
{
cout << "Current odometer reading: "; //Ask for current odometer reading.
cin >> cor;
if(cor == -1) //When -1 is set as input for Current Odometer Reading, then the loop will halt and output below.
break;
cout << "Gallons of gas purchased: "; //Ask for how much gas was purchased aftering traveling (cor - ior) miles.
cin >> gas;
mpg = (cor - ior)/gas; //Calculates mpg
tmiles = cor - ior; //Calculates the total miles driven on 1 interval
ior = cor; //Resets odometer at previous miles driven(just like resetting to 0).
ctmiles += tmiles;
ctgas += gas;
cout << "You drove " << fixed << setprecision(0) << tmiles << " miles averaging " << setprecision(2) << mpg << " mpg." << endl << endl;
}
cout << "All together, you drove " << fixed << setprecision(0) << ctmiles << " miles on ";
cout << setprecision(2) << ctgas << " gallons of gas, averaging " << setprecision(2) << (ctmiles / ctgas) << " mpg.";
}
For now I'm going to stick with float because 15 sig digits is more than I need...so is 7 but whatever.
What I noticed from what you did was that you declared that the total mileage of the whole trip starts at 0, which is true. Then you declared that the total gas for the trip starts at 0, which is also true. You also turned ior(initial odometer reading) into the current odometer reading instead of turning ior into tmiles(miles driven for that interval). Is what I said correct about what you changed? I just want to make sure I know how to do this for later purposes.\
*Also what I checked was how you calculated mpg. When you changed mpg to the new ior(initial odometer reading) then if someone were to say that their initial odometer reading was 50 and the current odometer reading was 500, your code would have taken 500 and set that as ior, without subtracting what the odometer was initially. So I just left my mpg calculation the way it was. Basically you were assuming, whether accidentally or on purpose, that the Initial reading was going to be 0. I don't want to call you out or anything I'm just saying. I feel good that I caught that being how I'm like 3 weeks in :).
You also turned ior(initial odometer reading) into the current odometer reading instead of turning ior into tmiles
initial reading: 12
current reading: 15
miles travelled on that interval: 15 - 12 = 3
next reading: 20
miles travelled on that interval: 20 - 15 or 20 - 3?
When you changed mpg to the new ior
I didn't, I used equivalent of tmiles for that:
1 2
tmiles = cor - ior;
mpg = (cor - ior)/gas; //What the difference between tmiles and cor - ior now?
I did not use previous_odometer after I changed it on same iteration, so it was cool.
your code would have taken 500 and set that as ior
If you mean what I think, it is logical: you saw 50 on odometer, then 500: you travelled 450 miles. Then you see 600 on odometer. What you will take for previous reading 500 (100 miles travelled) or 500 - 50 (150 miles travelled)?
If something is incorrect, I would like data, where it fails to get correct results.
I'm sorry, you were right. I must have been trying to run through yours too fast and messed up the variables. You're right there is nothing different about from tmiles and cor - ior, I must have misread. Sorry for accusing your code. This is what I had misread with your code:
1 2 3
tmiles = cor - ior; //Calculates the total miles driven on 1 interval
ior = cor; //Resets odometer at previous miles driven(just like resetting to 0).
mpg = ior / gas; //Calculates mpg
I don't know why that's how I read it but I realize how dumb it was for me to do that...I'm a beginner after all lol. I must have been trying to go too fast.