gpt4 book ai didi

C++ - 读取和比较用户输入?

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

我是编程新手。我想制作一个应用程序来询问您的信息并将它们保存到文本文件中(但稍后会出现)。

我被困在这里,我应该让程序读取用户输入的内容:

char nimi[20];
int aika;
int ika;
char juoma[3];

cout << "Hello!\nWhat's your name?\n";
cin >> nimi;
cout << "\n\nHi ";
cout << nimi;
cout << "!\n";
cout << "\nES or MF?";
cin >> juoma;

程序应该读取juoma 是“ES”还是“MF”,然后根据答案执行一些代码。

如果这样的事情可行,它会解决它,但它不会:

if(juoma==ES){
cout << "Nice choice!"
}

最佳答案

if(juoma=="ES")
{
cout << "Nice choice!"
}

你漏掉了双引号。您需要将变量 juoma 声明为 std::string 才能工作。在 C++ 中使用 char 数组简直就是折磨,不要那样做。

关于C++ - 读取和比较用户输入?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21626271/

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