gpt4 book ai didi

c++ - 汽车模拟器 - 无限循环

转载 作者:行者123 更新时间:2023-11-28 00:09:24 26 4
gpt4 key购买 nike

我正在创建一个汽车模拟器程序,当我运行它时出现无限循环。我加了一个休息时间;在 while 循环结束时,但随后它只显示里程:1 - 燃油油位:15。我还注意到在无限循环中,燃油油位没有改变,它只是保持在 15。我查看了我的代码并尝试调试它,但我似乎找不到它。任何帮助是极大的赞赏。谢谢

更新:它现在运行良好。燃油油位从 15 开始下降,但里程从 1 开始增加到 15,这是不正确的,知道为什么吗?
里程表可存储的最大里程数为 999,999 英里。当超过此数量时,里程表会将当前里程重置为 0。
每行驶 **24 英里
,它应该将 FuelGauge 对象的当前燃油量减少 1 加仑。 (汽车的燃油经济性为每加仑 24 英里。)
模拟给汽车加满油,然后运行一个增加里程表的循环,直到汽车耗尽燃料。在每次循环迭代期间,打印汽车的当前里程和燃料量。

        //While loop and display 
while (fuelG.getCurrentAmtFuel() > 0)
{
odm.incrementCurrentMileage();
cout << "Mileage: " << odm.getCurrentAmtMiles() << endl;
cout << "Fuel Level: " << fuelG.getCurrentAmtFuel() << " gallons" << endl;
break;
}

While 循环现在是:

while (fuelG.getCurrentAmtFuel() > 0)
{
odm.incrementCurrentMileage();
cout << "Mileage: " << odm.getCurrentAmtMiles() << endl;
cout << "Fuel Level: " << fuelG.getCurrentAmtFuel() << " gallons" << endl;
fuelG.decrementFuelTank();
}

    //FuelGauge.h
#ifndef FUELGAUGE_H
#define FUELGAUGE_H
using namespace std;
class FuelGauge
{
private:
int currentAmtFuel; //Holds current amount of fuel

public:
FuelGauge (int gallons) // Current amount of fuel in gallons
{
currentAmtFuel = gallons;
}

FuelGauge(); //Constructor

int getCurrentAmtFuel () //Gets the current amount of fuel and returns it
{
return currentAmtFuel;
}

void incrementFuelTank() //Increments the fuel tank
{
if (currentAmtFuel < 15)
{
currentAmtFuel++;
}
}

void decrementFuelTank() //Decrements the fuel tank
{
if (currentAmtFuel > 0)
{
currentAmtFuel--;
}
}
};

#endif

//Odometer.h
#include "FuelGauge.h"


#ifndef ODOMETER_H
#define ODOMETER_H
class Odometer
{
private:
int currentAmtMiles; //Holds current amount of miles
FuelGauge *fuelG; //Creates fuelG under FuelGauge
//FuelGauge *f;

public:
Odometer(int miles, FuelGauge *f) //odometer function
{
currentAmtMiles = miles;
fuelG = f;
}

int getCurrentAmtMiles () //Function to get the amount of miles
{
return currentAmtMiles;
}

void incrementCurrentMileage() //Increment mileage function
{
if(currentAmtMiles < 999999)
{
currentAmtMiles++;
}
else
currentAmtMiles = 0;
}


};

#endif

最佳答案

Odometer::incrementCurrentMileage() 函数不会减少 FuelGuage 中的燃料。我假设您希望它调用 fuelG->decrementFuelTank)

关于c++ - 汽车模拟器 - 无限循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33878182/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com