在C ++程序中没有减少气体

时间:2018-03-03 19:30:31

标签: class header operator-overloading do-while

好的,所以我有这个程序,假设模拟一个“优步”司机接送客户并通知他他的行驶距离和他应该感受到的气量。我基本上已经完成了所有工作,但对于我的生活,我不能让气体下降它保持静态数字。我知道这可能是愚蠢的事情,但是我看不到它。

在我的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 */

在我的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 */

最后是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

我相信问题出在我的FuelGauge.h中,但我没有看到它。如果有人会如此友善地看一遍并告诉我他们是否看到了什么我会非常感激。

0 个答案:

没有答案