如何在 if 语句条件 C++ 中使用字符串变量?
How to use a string variable in an if statements condition C++?
我正在用 C++ 为我的计算机编写我的最终项目 class。我们必须制作一个文字冒险游戏,所以我需要一些基础知识:
我这样做是为了让用户必须从 3 个学习科目中进行选择:数学、英语和科学。一旦发生这种情况,他们就会进入主题选择的功能。他们必须为房间里的 3 名不同学生解决 5 个问题。通过房间,然后他们进入该主题的另外 2 个房间。完成所有房间后,他们将对该主题进行“最终测试”。
所以我为 subject_choice
创建了一个字符串变量,我想在 if
语句条件中使用该变量,以便它将用户发送到相应的主题房间。我把它编码为:
std::string subject_choice;
cout << "Which subject would you like to choose?" << endl;
cout << "You can choose between: math, english, or science." << endl;
cin >> subject_choice;
while (subject_choice != "math" && subject_choice != "english" && subject_choice != "science")
{
cout << "Error: Please enter math, english, or science." << endl;
cin >> subject_choice;
if(std:: string subject_choice = "math")
{
bool pOf1 = math_room1();
if (pOf1 = true)
{
cout << "Congratulations! You passed the first room!" << endl;
cout << "Now let's move to the next room!" << endl;
bool pOf2 = math_room2();
if (pOf2 = true)
{
cout << "Congratulations! You passed the second room!" << endl;
cout << "Now let's move to the next room!" << endl;
bool pOf3 = math_room3();
if (pOf3 = true)
{
cout << "Congratulations! You passed the final room!" << endl;
cout << "Now let's move on to your final test!" << endl;
bool pOf4 = math_final();
cout << "Congratulations! You passed your test! You are now a teacher!" << endl;
cout << "You win the game!" << endl;
按原样使用代码,我得到一个错误,突出显示第一个 if 语句
if(std:: string subject_choice = "math")
错误说表达式必须有 bool 类型(或可转换为 bool)。
我的问题是:如果我把
if (bool subject_choice = "math")
它会改变我想要做的事情吗?它还会工作吗?我有点困惑。
这是因为您在语句中创建了一个变量。 std::string 是一种类型表示法(您只需要一次)。当你声明它时。尝试
if (your_choice == "math") {
您将赋值 (=
) 与比较 (==
) 混淆了。尝试将该行更改为
if (subject_choice == "math") {
.
也许我可以帮助你。尝试将 if(std:: string subject_choice = "math")
更改为 if(std:: string subject_choice == "math")
我正在用 C++ 为我的计算机编写我的最终项目 class。我们必须制作一个文字冒险游戏,所以我需要一些基础知识:
我这样做是为了让用户必须从 3 个学习科目中进行选择:数学、英语和科学。一旦发生这种情况,他们就会进入主题选择的功能。他们必须为房间里的 3 名不同学生解决 5 个问题。通过房间,然后他们进入该主题的另外 2 个房间。完成所有房间后,他们将对该主题进行“最终测试”。
所以我为 subject_choice
创建了一个字符串变量,我想在 if
语句条件中使用该变量,以便它将用户发送到相应的主题房间。我把它编码为:
std::string subject_choice;
cout << "Which subject would you like to choose?" << endl;
cout << "You can choose between: math, english, or science." << endl;
cin >> subject_choice;
while (subject_choice != "math" && subject_choice != "english" && subject_choice != "science")
{
cout << "Error: Please enter math, english, or science." << endl;
cin >> subject_choice;
if(std:: string subject_choice = "math")
{
bool pOf1 = math_room1();
if (pOf1 = true)
{
cout << "Congratulations! You passed the first room!" << endl;
cout << "Now let's move to the next room!" << endl;
bool pOf2 = math_room2();
if (pOf2 = true)
{
cout << "Congratulations! You passed the second room!" << endl;
cout << "Now let's move to the next room!" << endl;
bool pOf3 = math_room3();
if (pOf3 = true)
{
cout << "Congratulations! You passed the final room!" << endl;
cout << "Now let's move on to your final test!" << endl;
bool pOf4 = math_final();
cout << "Congratulations! You passed your test! You are now a teacher!" << endl;
cout << "You win the game!" << endl;
按原样使用代码,我得到一个错误,突出显示第一个 if 语句
if(std:: string subject_choice = "math")
错误说表达式必须有 bool 类型(或可转换为 bool)。
我的问题是:如果我把
if (bool subject_choice = "math")
它会改变我想要做的事情吗?它还会工作吗?我有点困惑。
这是因为您在语句中创建了一个变量。 std::string 是一种类型表示法(您只需要一次)。当你声明它时。尝试
if (your_choice == "math") {
您将赋值 (=
) 与比较 (==
) 混淆了。尝试将该行更改为
if (subject_choice == "math") {
.
也许我可以帮助你。尝试将 if(std:: string subject_choice = "math")
更改为 if(std:: string subject_choice == "math")