首页 > 解决方案 > 有没有办法让这些变量从真到假,或者我做错了

问题描述

我正在尝试构建一个程序,以简化 Smash 中的阶段选择程序。我试图让用户禁止阶段的主要方式是设置一系列布尔函数,他们设置为真或假,这样非法阶段就永远不会出现。问题是,我在函数中拥有的布尔变量不会从真变为假。

“Aban1”函数应该只接受第一个禁令,并将与该阶段关联的变量设为假。(这不起作用和问题的根源)

“stagespitter 函数将布尔变量与用户已经设置的另一组进行比较。它仅在预设阶段为真且可被禁止的阶段为真时输出阶段。

我尝试过枚举数据类型,尽管我认为这是错误的尝试方式,因为 Visual Studio 说枚举不是可修改的变量。

void Aban1(string ban1, bool PS2, bool PS1, bool BF, bool FD, bool SMASHVILLE, bool KALOS, bool TOWN, bool LYLAT, bool STORY, bool ISLAND);

void stagespitter(bool LYLAT, char chLylat, bool chPS1, bool PS1, bool PS2, char chPS2, char chBF, bool BF, char chFD, bool FD, char chIsland, bool ISLAND, char chStory, bool STORY, char chTown, bool TOWN, char chKalos, bool KALOS, char chSmashville, bool SMASHVILLE); //spits out the stages that are available


void Aban1(string ban1, bool PS2, bool PS1, bool BF, bool FD, bool SMASHVILLE, bool KALOS, bool TOWN, bool LYLAT, bool STORY, bool ISLAND) { //this should run the s
if (ban1 == "PS2" || ban1 == "ps2" ) { 
    PS2 = false;
    cout << "Banned ps2" << endl;
}
else if (ban1 == "PS1" || ban1 == "ps1") {
    PS1 = false;
}
else if (ban1 == "BF" || ban1 == "bf" || ban1 == "bF") {
    BF = false;
}
else if (ban1 == "Smash" || ban1 == "smash") {   //remember to set direction for this, as you have to type specifically
    SMASHVILLE = false;
}
else if (ban1 == "Kalos" || ban1 == "kalos") {
    KALOS = false;
}
else if (ban1 == "Town" || ban1 == "town") {
    TOWN = false;
}
else if (ban1 == "FD" || ban1 == "fd" || ban1 == "fd") {
    FD = false;
}
else if (ban1 == "story" || ban1 == "Story") {
    STORY = false;
}
else if (ban1 == "Lylat" || ban1 == "lylat") {
    LYLAT = false;
} //make the final else loop it back
else (ban1 == "Island" || ban1 == "island"); { //instead of running this over and over, make a function that compares the state of true and false of the bools and the functions associated with the stage
    ISLAND = false; //and compares them only returning if both they accepted a stage, and it aint false, or not banned yet
}}


void stagespitter(bool LYLAT, char chLylat, bool chPS1, bool PS1, bool PS2, char chPS2, char chBF, bool BF, char chFD, bool FD, char chIsland, bool ISLAND, char chStory, bool STORY, char chTown, bool TOWN, char chKalos, bool KALOS, char chSmashville, bool SMASHVILLE) {
cout << "the Available stages are" << endl;

if (lylat(chLylat) == LYLAT && LYLAT == true) //set the bools equal to the function
    cout << "Lylat Cruise" << endl;

if ((ps1(chPS1) == PS1) && (PS1 == true))
    cout << "Pokemon Stadium 1" << endl;

if ((ps2(chPS2) == PS2) && (PS2 == true))
    cout << "Pokemon Stadium 2" << endl;

if (battlefield(chBF) == BF && BF == true)
    cout << "Battlefield" << endl;

if (finalDestination(chFD) == FD && FD == true) //something wrong here not adding up
    cout << "Final Destination" << endl;

//not correctly orienting what is false, and giving stages that have already been delcared not valid
if (yoshisIsland(chIsland) == ISLAND && ISLAND == true)//check the set equal parameters
    cout << "YoShi's Island" << endl;

if (yoshisStory(chStory) == STORY && STORY == true)
    cout << "Yoshi's Story" << endl;

if (townAndCity(chTown) == TOWN && TOWN == true)
    cout << "Town and City" << endl;

if (kalos(chKalos) == KALOS && KALOS == true)
    cout << "Kalos" << endl;

if (smashville(chSmashville) == SMASHVILLE && SMASHVILLE == true)
    cout << "Smashville" << endl;}

我希望布尔变量能够设置为假,现在它们没有被设置为假。稍后我还需要将变量重置为 true,但我还没有到那个时候。

标签: c++enumsboolean

解决方案


我将假设您在某个地方一个接一个地调用 ABan1() 然后 stageSplitter()。在 C++ 中,当您调用函数时,参数会生成自己的所有新副本,然后当函数结束时,它们都会消失。这称为按值调用。布尔值将在函数中设置,但一旦函数结束,它们将不再设置。要解决此问题,您可以通过在所有参数之前放置 & 来使用调用作为引用。这将更改您在函数中用作参数的原始值。这是一个可能有助于更好地解释它的链接: https ://www.javatpoint.com/call-by-value-and-call-by-reference-in-cpp 希望这会有所帮助!


推荐阅读