我正在尝试用c ++创建一个if else路径。我想要发生的是当我选择Anakin,然后是bool" Anakin" will = true,然后"如果Anakin == true"它将输出所需的代码,当我选择Heather时,然后" Anakin == false"它也会这样做。除非我选择" Heather",否则输出代码就像我选择" Anakin":
bool anakin;
char Name[25];
string choice;
int GameState;
int iRoomCounter[4] = { 0, 0, 0 }; // Used to determine if a room has been
visited.int main()
{
cout << "" << endl;
cout << "\t\t\tNimbus Town" << endl;
cout << "What is your name?" << endl;
cout << "Name: ";
cin >> Name;
cout << "All right " << Name << ", I will let you choose a friend that will be with" << endl;
cout << "you along the way, okay? Great." << endl;
cout << "Now choose your friend:" << endl;
cout << "• Heather" << endl;
cout << "• Anakin" << endl;
cin >> choice;
if (choice == "Heather") {
anakin = false;
cout << "You chose Heather, good choice!" << endl;
GameState = START;
}
else if (choice == "Anakin") {
anakin = true;
cout << "Anakin, what a great choice!" << endl;
GameState = START;
}
else {
cout << "What?" << endl;
}
while (GameState != DEATH) {
switch (GameState) {
case START: {
if (iRoomCounter[0] == 0) {
if (anakin == true) {
cout << "\n'Hello, welcome to Nimbus town.'" << endl;
cout << "You jolted awake as an older, white-haired" << endl;
cout << "man greeted you. He was in a white tuxedo." << endl;
cout << "'My name is Kieran. What about you?'" << endl;
cout << "My name is " << Name << "." << endl;
cout << "Well " << Name << ", it's nice to meet you." << endl;
cout << "\nsomething doesn't feel right about him, but it's probably" << endl;
cout << "because I'm new here, you thought." << endl;
iRoomCounter[0] = 1;
if (anakin == false) {
cout << "Anakin is falseo." << endl;
}
}
}
}
}
}
}
如果有人告诉我我应该做什么或提供建议/代码来帮助我,我会很感激。 谢谢!
答案 0 :(得分:1)
在C ++中,==
用于测试相等性,而=
用于分配值。在您的情况下,您尝试将值true
分配给Anakin
,但您使用的是==
运算符。我怀疑将Anakin == false;
和Anakin == true;
行更改为Anakin = false;
和Anakin = true;
可能会解决您的问题。
答案 1 :(得分:0)
现在您正在if语句中进行分配。 在if语句中使用==,因为您要检查值是否匹配。 分配新值时使用= 所以举个例子 分配时
anakin = true;
和
if (anakin == false)
比较时