gpt4 book ai didi

c++ - 我的代码没有声明一个 int 到我选择它在 if else 循环中的代码是否有修复?

转载 作者:行者123 更新时间:2023-11-30 01:35:21 24 4
gpt4 key购买 nike

当我运行和测试代码时,我为第二个选项选择了 2,但是当我这样做时,它会将其恢复为 1 并在我的 if-then 循环中选择第一个选项。

我试过将声明移到控制台输入的正下方,我试过使用除 2 之外的其他数字。我移动了控制台输出以便我知道它选择了什么,但这对任何事情都没有帮助。

这是我的完整代码:

#include <iostream>
#include <conio.h>
using namespace std;

int main()
{
std::cout << "HELLOW THERE!\n";
char enter[2];
int help2 = 0;
char help[20];
std::cin >> help;
std::cout << "hello, " << help <<" this was a test, thanks\n";
std::cout << "lol jk no it wasnt\n";
std::cout << "come on, " << help << "\n";
char name[20] = "Crilbus";
std::cout << "press any key + enter to continue...\n";
std::cin >> enter;
std::cout <<"Your name is " << name << " now haha gotem\n";
std::cout << "so, " << name << ", you were walking to " << help <<" and you bought a cherry.\n";
std::cout << "JUST ONE CHERRY, FORGET ABOUT THE OTHER 4.\n";
std::cout <<"so what do you do?\n";
std::cout <<"do you eat it or throw it at someone?\n";
std::cout <<"press 1 to eat it and 2 to throw it.\n";
int henlep;
std::cin >> henlep;

if (int henlep = 1) {
std::cout <<"you chose " << henlep << ",\n";
std::cout <<"you fool!\n";
std::cout << "honestly if you ate it you ruined the fun. compile this again and press 2\n";
}
else if (int henlep = 2){
std::cout <<"ok so you through it at my boy gavin and he decided to 1v1 you.\n";
std::cout << "do you press 1 to fight, or press 2 to run away?\n";
std::cin >> henlep;
}
}

当我在控制台输入中选择 2 时,我希望控制台输出为 2,但我得到的控制台输出为 1

最佳答案

那是因为 int henlep = 1 将 henlep 声明为 int 将 1 赋给它。但是,您似乎想要的是将它与一个进行比较。为此,您必须像这样使用比较运算符 ==:

if (henlep == 1) {
std::cout <<"you chose " << henlep << ",\n";
std::cout <<"you fool!\n";
std::cout << "honestly if you ate it you ruined the fun. compile this again and press 2\n";
}

关于c++ - 我的代码没有声明一个 int 到我选择它在 if else 循环中的代码是否有修复?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54334460/

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