gpt4 book ai didi

c++ - 对是或否陈述的无效回答

转载 作者:行者123 更新时间:2023-11-30 03:47:48 26 4
gpt4 key购买 nike

#include <iostream>
#include <string>
using namespace std;

string mood;

int main()
{
cout << "Are you feeling happy today?" << endl;
cin >> mood;
if (mood == "yes" || mood == "Yes")
{
cout << "Great! Glad you're happy!" << endl;
}
if (mood == "no" || mood == "No")
{
cout << "That's unfortunate, hope you feel better." << endl;
}
if (mood == "unsure" || mood == "Unsure")
{
cout << "At least you're alive!" << endl;
}
else
cout << "Please answer with 'yes', 'no' or 'unsure'" << endl;

// How would I make this loop back to asking the user the
// "Are you feeling happy today" again?

return 0;
}

我想知道在"is"或“否”问题中,如果用户输入"is"或“否”以外的任何内容,我是否能够循环回到向用户询问初始问题。我需要一个 while 循环吗?如果是这样,有人可以解释一下吗?

最佳答案

一个简单的解决方案:制作一个基本上可以永远循环的循环,一次又一次地问同样的问题,但在出现有效答案时打破它:

int main()
{
string mood;

while(1)
{
cout << "Are you feeling happy today?" << endl;
cin >> mood;
if (mood == "yes" || mood == "Yes")
{
cout << "Great! Glad you're happy!" << endl;
break;
}
if (mood == "no" || mood == "No")
{
cout << "That's unfortunate, hope you feel better." << endl;
break;
}
if (mood == "unsure" || mood == "Unsure")
{
cout << "At least you're alive!" << endl;
break;
}

cout << "Please answer with 'yes', 'no' or 'unsure'" << endl;
}

return 0;
}

关于c++ - 对是或否陈述的无效回答,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33516074/

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