Question:

Need help with my first function call?

by  |  earlier

0 LIKES UnLike

// this program converts gallons to liters using function calls

#include <iostream>

using namespace std;

double mpg(double liters, double gallons);

int main ()

{

int liters, gallons;

double miles;

cout << "How many liters did your car consume?\n";

cin >> liters;

miles = mpg(liters, gallons);

cout << miles << endl;

return 0;

}

double mpg(double liters, double gallons)

{

const double GALLONS = .264179; // Liter in one gallon

double mpg;

mpg = liters * gallons;

return (mpg);

}

getting an error once i compile using Microsoft Visual saying the variable gallons is being sued without being defined, its my first time using a function call and i have no idea how to fix this, the program runs but once i enter the number of liters that error just pops up

 Tags:

   Report

2 ANSWERS


  1. You never defined gallons in main(). You should at least give it a default like this:

    int liters = 0, gallons = 0;

    This code doesn&#039;t make much sense to me, so I can&#039;t be sure what you are trying to do here. What does mpg have to do with metric conversion?

    If you want to convert you could use something like this:

    double litersToGallons(double liters)

    {

    const double GALLONS = .264179;

    return liters * GALLONS;

    }


  2. int liters, gallons;  GALLONS = .264179; // Liter in one gallon to be honest this is a stb in the dark but if i read it right this looks like where the problem is try the above to define

Question Stats

Latest activity: earlier.
This question has 2 answers.

BECOME A GUIDE

Share your knowledge and help people by answering questions.