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.

Dani AI

Generated

Two problems produce the "infinite" result: the gallons value is being computed before any input exists, and the program never recalculates mpg for the second car. was right — do the liters→gallons conversion after reading the liters. Computing a division by an uninitialized or zero value yields infinity or NaN, which is what was observed.

Keep all state local (no global gallons calculated from an uninitialized liters) and guard against zero input. A small, safe find_mpg that converts inside the function and checks for invalid input looks like this:

const double LITERS_TO_US_GALLON = 0.264172052;

double find_mpg(double miles, double liters) {
    if (liters <= 0.0) {
        // handle invalid input: return 0 or throw/log error
        return 0.0;
    }
    double gallons = liters * LITERS_TO_US_GALLON;
    return miles / gallons;
}

Call that function each time you read a new pair of values (so compute mpg again for the second car). Also validate cin (check if (!cin)) and clear the stream on bad input, and avoid reusing the same identifier for different purposes (for example, use char retry instead of ans if you already use ans as a numeric name elsewhere). Optionally use std::isfinite() to detect any remaining infinite/NaN results and std::fixed/std::setprecision() to format the output.

Finally, confirm which gallon you mean: US vs imperial gallons use different conversion factors. This change and the input checks will remove the "infinite" output and make results correct and robust.

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.