汽车仪表模拟器

时间:2019-11-30 02:47:17

标签: c++

因此,我正在编写用于对“汽车模拟器”进行编程的代码,并且应该为菜单使用switch语句:显示仪表,询问距行驶距离,要添加多少汽油,并退出。我得到它来显示菜单,但是当我按下其中一个选项时,它什么也没做。我希望有人能帮助我弄清我所缺少的内容,而不是说要帮助我做到这一点,因为我想学习,所以下次我不会遇到同样的问题,现在已经研究了一段时间,甚至查找了一些有关switch语句的视频,但我只是听不懂。

#include <iostream>
#include <string>
using namespace std;

void printmenu();

class FuelGauge
{
private:
    int CurrentFuel;

public:
    FuelGauge();
    ~FuelGauge();
    FuelGauge(int g)
    {
        CurrentFuel = g;
    }

    int getCurrentFuel()
    {
        return CurrentFuel;
    }

    void IncrementFuel()
    {
        int gas;
        cout << "How much fuel are you putting in? " << endl;
        cin >> gas;
        for (int fuel = gas; gas > 0; gas--) {
            CurrentFuel++;
        }
    }

    void DecrementFuel()
    {
        if (CurrentFuel > 0)
        CurrentFuel--;
    }

};

FuelGauge::FuelGauge()
{

}

FuelGauge::~FuelGauge()
{

}

class Odometer
{
private:
    int CurrentMileage;
    FuelGauge* fuel;

public:
    Odometer();
    ~Odometer();
    Odometer(int miles, FuelGauge* f)
    {
        CurrentMileage = miles;
        fuel = f;
    }

    int getCurrentMileage()
    {
        return CurrentMileage;
    }
    void incrementCurrentMileage()
    {
        if (CurrentMileage < 999999)
            CurrentMileage++;
        else
             CurrentMileage = 0;
    }

    void decrementCurrentMileage()
    {
        if (CurrentMileage > 24)
            CurrentMileage--;
        (*fuel).DecrementFuel();
    }

};

Odometer::Odometer()
{

}

Odometer::~Odometer()
{

}

int main()
{
    FuelGauge fuel(15);
    Odometer odo(0, &fuel);
    int n = 0;

    while (n != 5) {

        printmenu();
        cin >> n;

        switch (n) {
        case 1:
            fuel.getCurrentFuel();
            break;
        case 2:
            odo.getCurrentMileage();
            break;
        case 3:
            break;
        case 4:
            fuel.IncrementFuel();
            break;
        default:
            break;
        }

        return 0;
    }
}

void printmenu() {
    cout << "1. Show current fuel " << endl;
    cout << "2. Show current status of the odometer " << endl;
    cout << "3. How far are you going? " << endl;
    cout << "4. How much gass are you putting in? " << endl;
}

1 个答案:

答案 0 :(得分:0)

您打算观察什么?您调用一些返回没有任何副作用的值的方法,并且对这些值不做任何事情:您会立即丢失它们。您可能应该输出它们:

cout << fuel.getCurrentFuel();