gpt4 book ai didi

c++ - 如何使用 else if 语句来比较字母并使其不区分大小写?

转载 作者:太空宇宙 更新时间:2023-11-03 10:45:02 24 4
gpt4 key购买 nike

所以我这里有这段代码:

string answer1;

answerQues1:
cout << "So, A, B, C or D?";
cin >> answer1;

if (answer1 == "A" | answer1 == "a")
{
cout << "It's the right answer, you have " << char(156) << "100!" << endl;
PlaySound(TEXT("C:/Users/user/Downloads/Millionaire/£100correct.wav"), NULL, SND_FILENAME | SND_ASYNC );
Sleep(2000);
}

else if (answer1 >= "E" | answer1 >= "e") //FIX THIS, IT'S ONLY ACCEPTING CAPITALS NOT LOWERCASES.
{
cout << "Whoops, you've entered an incorrect option! Remember, only A, B, C or D are acceptable." << endl;
answer1.erase();
goto answerQues1;
}

else
{
cout << "I'm very sorry, it's an early exit for you - it's the wrong answer! You leave with nothing!" << endl;
PlaySound(TEXT("C:/Users/user/Downloads/Millionaire/£100and£64000lose.wav"), NULL, SND_FILENAME | SND_ASYNC);
Sleep (4000);
PlaySound(TEXT("C:/Users/user/Downloads/Millionaire/gameOver.wav"), NULL, SND_FILENAME | SND_ASYNC);
cout << "Thanks for playing! Press any key to exit the game" << endl;
system("pause");
return 0;
}

else if 语句检查答案是否大于或等于字母 E,因为只有 A、B、C 或 D 是可接受的。然后它显示一条消息告诉用户,删除 answer1 的值,然后返回到输入部分,本质上是一个循环。但是,当我输入一个不正确的选项(例如 E)并立即输入一个可接受的选项(例如 B)时,它不会接受 B 作为错误答案,而是不会转到 else 语句,而是转到 else if ONLY IF THE ANSWER IS LOWERCASE .但是如果我用大写字母输入 B,它会认为它是错误的,并按照它应该的方式转到 else 语句。无论如何我可以解决这个问题以便它检测大写和小写字母吗?非常感谢。

最佳答案

问题是小写字母大于大写字母。请参阅 ASCII 表:http://www.asciitable.com/ .你可能想要:

else if ((answer1 >= "E" && answer1 <= "Z") || answer1 >= "e")

关于c++ - 如何使用 else if 语句来比较字母并使其不区分大小写?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24085483/

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