0

Ok, so I have this program that is suppose to simulate an "Uber" driver picking up and dropping off customers and informing him of how far he has driven and how much gas he should have felt. I basically have it all worked out but for the life of me I can't get the gas to go down it stays at a static number. I know it is probably something so simple its dumb but I can't see it.

in my FuelGauge.h

#ifndef FuelGauge_h
#define FuelGauge_h
#define MAXG 15

// Class FuelGauge definition
class FuelGauge
{
    // Data member
    static int gallons;
public:
    // Overloading -- operator
    void operator --()
    {
        --gallons;
    }// End of function

    // Function to return gallons
    int getGallons()
    {
        return gallons;
    }// End of function
};// End of class

// Initializes static data member
int FuelGauge::gallons = MAXG;

#endif /* FuelGauge_hpp */

in my Car.h

#ifndef Car_h
#define Car_h
#include "FuelGauge.h"
#include "Odometer.h"

// Class car definition
class Car
{
public:
    // Declares object as data member using deligation
    FuelGauge *fg;
    Odometer *om;
    // Overloads >> operator
    friend std::istream & operator >>(std::istream &is, Car &c)
    {
        int no;
        // Loops till valid mileage entered by the user
        do
        {
            // Accepts mileage
            std::cin>>no;
            // Checks if the mileage is zero or negative show error message
            if(no <= 0)
                std::cout<<"\n Invalid response, mileage should be greater than 0. \n Please reenter data.";
            // Otherwise come out of the loop
            else
                break;
        }while(1);
        // Loops till no
        for(int x = 0; x < no; x++)
            // Increase the mileage by one
            ++*c.om;
        // Checks if the current mileage is greater than or equals to 24 and entered mileage is less than 24
        if(c.om->getCurrentMileage() >= 24 && no < 24)
            // Decrement by one
            --*c.fg;
        // Otherwise
        else
        {
            // Calculate the remainder
            int rem = (no / 24);
            // Loops till remainder
            for(int x = 0; x < rem; x++)
                // Decrease by one
                --*c.fg;
        }// End of else
        // return istream object
        return is;
    }// End of function

    // Overload << operator
    friend std::ostream & operator <<(std::ostream &os, Car &c)
    {
        // Checks if the current gallon is less than or equals to zero
        if(c.fg->getGallons() <= 0)
        {
            // Display message and stop
            std::cout<<"\n Drove "<<c.om->getCurrentMileage()<<" miles I'm out of gas.";
            exit(0);
        }// End of if condition
        // Otherwise display total mileage traveled and fuel left
        else
            std::cout<<"\n Drove "<<c.om->getCurrentMileage()<<" miles now I have "<<c.fg->getGallons()<<" gallons left.";
        return os;
    }// End of function
};// End of class
#endif /* Car_hpp */

and lastly Main.cpp

#include<iostream>
#include<stdlib.h>
#include "FuelGauge.h"
#include "Odometer.h"
#include "Car.h"

using namespace std;

// main function definition
int main()
{
    int customer = 1;
    // Creates car object
    Car cc;
    // Loops till = 0 fuel available
    do
    {
        // Accepts data and displays data using object
        cout<<"\n How far away is customer #"<<customer<<"? ";
        cin>>cc;
        cout<<cc;
        cout<<"\n How far does customer #"<<customer<<" need to go?";
        cin>>cc;
        cout<<cc;
    }while(static_cast<void>(customer++),1);// End of do - while

}// End of main function

I believe that problem is in my FuelGauge.h but I'm not seeing it. If someone would be so kind to look it over and let me know if they see anything I would greatly appreciate it.

csiket715
  • 51
  • 7
  • Where do you assign values to `fg` and `om`? They seem to be pointers that are never made to point *to* anything. Why is `gallons` static? – David Schwartz Mar 03 '18 at 19:35
  • Yes, but where do you assign them *values*? If I do `int i;`, I still can't use `i` until I give it a value, right? You create pointers and then try to use the things they point to, but you never make them point to anything. For example, `c.om->getCurrentMileage()` calls `getCurrentMileage` on the object `c.om` points to -- but it doesn't point to anything. – David Schwartz Mar 03 '18 at 19:42

0 Answers0