Need help in calculating a car’s gas mileage

Hello everyone, my question is that I have to write a program that calculates a car’s gas mileage. The program should ask the user to enter the number of gallons of gas the car can hold and the number of miles it can be driven on a full tank.
It should then calculate and display the number of miles per gallon the car gets.

Here is what I got from my understanding from the question.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>

using namespace std;

int main()
{
    int galGas, miFulTnk, cal;
    double average;

    cout << "Please enter the number of gallons of gas your car can hold: ";
    cin >> galGas;
    cout << "Please enter the number of miles your car can be driven on a full tank: ";
    cin >> miFulTnk;


    cal = miFulTnk / galGas;

    cout << "\nThe number of miles per gallon your car gets: " << cal << "." << endl << endl;



    return 0;
}
closed account (j3Rz8vqX)
What is your question? What do you need help with?

The distance a car can travel is miles per gallon; miles/gallon.

You have stated:
 
cal = miFulTnk / galGas;

Which does gallons per mile; gallons/miles.

Maybe swap them?

Code is read right to left, calculation are still done normally.

Other than that possibly rephrase the exact problem and what you're not satisfied with.

Edit:
Thought miles and gallon were given, but it was gallons and max miles on full tank.

Is your issue precision? as Jaybob66 has stated.
Last edited on
You didn't actually ask a question Hassan, so i'm presuming your problem is accuracy.

@7 double galGas, miFulTnk, cal;

or use float, either works.
Dput, my question is how can I get the number of miles per gallon that the car gets?
Should I divide the number of gallons of gas the car can hold with the number of miles it can be driven on a full tank?
Jaybob66, could you please give me an example of how to use float?
Dput, yes the precision is my issue. That is what confusing me.

@7 int galGas, miFulTnk, cal;

this can only give integer answers. change the int to float/double.


@7 float galGas, miFulTnk, cal;

or

@7 double galGas, miFulTnk, cal;

double is more accurate, but takes up 2x the memory to store.
Thank you Jaybob66 and Dput. Jaybob66, I will try the float and I will let you know if it works well or not.
Topic archived. No new replies allowed.