汽车仪表模拟器

Car Instrument Simulator

本文关键字:模拟器 仪表 汽车      更新时间:2023-10-16

所以我正在编写一个代码来编写汽车仪表模拟器,我应该有一个菜单的开关语句:显示仪表,询问它们到驾驶有多远,询问要添加多少汽油,然后退出。我让它显示菜单,但是当我按下其中一个选项时,它什么也没做。我希望有人帮助我弄清楚我错过了什么,而不是说帮助我做到这一点,因为我想学习,所以下次我不会遇到同样的问题,已经工作了一段时间,甚至查找了一些关于 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;
}

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

cout << fuel.getCurrentFuel();