my program reads om tje number of liters of gasoline per 2 cars and the number of miles traveled by a car and then its supose to output the number of miles per gallon the car delivered. I used .264179 as my conversion of liters to gallons. Everything seems fine but my number returns as a infinitive.

here's the code

#include <iostream>
#include <cmath>
using namespace std;

    double liters;
    double gallons = liters * .264179;
    char ans;
    double find_mpg( double miles, double liters);

int main()
{
    do  
    {   
        double miles;

        cout << "enter the number of liters used in the first car\n";
        cin >> liters;
        cout << "how many miles did you travel in the first car\n";
        cin >> miles;

        double mpg = find_mpg(miles, liters);

        cout << "this is your fuel eficiency: \n";
        cout <<  mpg << " Miles per gallon\n";


        cout << "enter the number of liters used in the second car\n";
        cin >> liters;
        cout << "how many miles did you travel in the second car\n";
        cin >> miles;

        cout << "this is your fuel eficiency for second car: \n";
        cout <<  mpg << " Miles per gallon\n";
        cout << "do you want to calculate again\n?";
        cout << "press Y to recalculate and N to stop";
        cout << "and then press return: ";
        cin >> ans;
    } while (ans == 'y' || ans == 'Y');



}



double find_mpg(double miles, double liters)
{ 


    double ans = miles / gallons;
    return ans;

}

Please help me out, I'm guessing my math is wrong somewhere,
thanks for any help.

Recommended Answers

All 2 Replies

Do your liters to gallons conversion _after_ you've read in the liters. As of now you're doing that calculation on the 6th line with whatever garbage is in memory. (so in other words convert the value down in your function)

thanks so much

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.