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;

}
``````

thanks for any help.

Edited by mike_2000_17: Fixed formatting

2
Contributors
2
Replies
3
Views
8 Years
Discussion Span
Last Post by trzypak

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)

Edited by jonsca: n/a

thanks so much

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.