Program that tells how many more gallons are needed to reach the destination

I'm making a program that tells the user how many galoons of fuel do you have to add to reach the destination but I don't know to do it because the values of the car tank, the mpg and the miles that the user wants to travel are all entered by the user.

This is the part that I need a few hints.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
cout << "How many gallons your car have? ";
cin >> CarTank;

cout << "How many mile per hour your car can travel? ";
cin >> mpg;

cout << "How many miles you are planning to travel? ";
cin >> miles;

if (CarTank * mpg < miles) {

		cout << "You are out of fuel! You need: " << Gallon << "more gallons" << endl;

	}

Last edited on
That is a math problem.

You have CarTank gallons.

You can drive CarTank*mpg miles with that. That is cars Range.
      CarTank * mpg == Range
   // mpg != 0.  Lets divide both sides of the equation with mpg
<=>
CarTank * mpg / mpg == Range / mpg
    // mpg/mpg==1. Simplify left side
<=>
            CarTank == Range / mpg

Volume (gallons) == Distance (miles) / mpg (miles/gallons)

Program will know the Distance. It is miles miles.
Program will know the mpg.

Program can therefore calculate the Volume that is required to drive miles miles.

If Volume is known and CarTank is known, then their difference is Gallon gallons.
Ok, I think that I understand but what happens if the reverse is true? The program is supposed to show the fuel remaining after the drive. It is more or less the same problem or I need to do something different this time? And thanks for helping me to understand the previous one!

1
2
3
4
5
if (CarTank * mpg > miles) {

		cout << "You reached your destination! You have now: " << Gallon << " gallons remaining." << endl;

	}
Last edited on
It is still the same:
If Volume is known and CarTank is known, then their difference is Gallon gallons.

In one case Volume - CarTank is positive and in the other CarTank - Volume is positive.
Topic archived. No new replies allowed.