有没有办法让这些变量从真到假切换,或者我做错了[重复]

问题描述 投票:0回答:1

这个问题在这里已有答案:

我正在尝试构建一个程序,允许简化Smash中的阶段选择过程。我尝试允许用户禁止阶段的主要方式是设置一系列布尔函数,它们设置为true或false,以便非法阶段永远不会出现。问题是,函数中的布尔变量不会从true变为false。

“Aban1”函数应该在第一次禁止时使用,并使与该阶段关联的变量为false。 (这不起作用,问题的根源)

“stagepitter函数将布尔变量与已经由用户设置的另一个集合进行比较。如果预设阶段都为真,则只输出阶段,并且能够被禁止的阶段为真。

我尝试过枚举数据类型,虽然我认为这是尝试这种方式的错误方法,因为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;}

我希望布尔变量能够设置为false,现在它们不被设置为false。我稍后还需要将变量重置为true,但我还没有。

c++ enums boolean
1个回答
0
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.