gpt4 book ai didi

c++ - 有没有办法让 C++ Switch 语句循环回到第一种情况?

转载 作者:塔克拉玛干 更新时间:2023-11-03 08:27:02 24 4
gpt4 key购买 nike

好的,这是一个简单的代码示例:

    char answer;
cin >> answer;
switch(answer)
{
case 'y':
case 'Y':
inventory[0] = "Short Sword";
cout << "\nYou place the Rusty Battle Axe in the chest.";
break;

case 'n':
case 'N':
inventory[0] = "Rusty Battle Axe";
cout << "\nYou leave the Short Sword in the chest.";
break;

default :
cout << "\nThat was an invalid response.";
}

显然,我可以用一会儿把我的头发拉出来(answer != 'Y' || answer !=...) 但是有没有更优雅的方法在执行默认情况后简单地返回到第一个情况?因此,如果用户输入了错误的字母,我只需再次询问他们这个问题,直到他们输入可接受的答复为止?

不,这不是家庭作业或其他任何东西。我正在研究 Dawson 的 C++ 游戏编程书,我想通过允许用户保留或交易项目来使程序示例更加生动。我把所有这些都做得很好,但是如果输入了错误的响应,它只会显示库存的内容并退出。我想做对。强制用户输入正确的响应,然后显示更新后的 list 。

感谢您的帮助!

更新!你们都给了我这么多不同的方法 - 我真的很感激!我承认我可能没有正确设计这个 switch 语句,我为矛盾之处道歉。我会尝试你的每一个建议并发回这里,选择一个作为答案。谢谢!

好的,我刚刚浏览了您的所有答案,并用我的代码尝试了大部分答案。我选择了最简单、最优雅的解决方案作为我的问题的答案。但是你们都帮助我看到了看待这个问题的不同方式,现在我对 switch 语句有了更多的了解。实际上,在用户 What's A Creel 正在关注的 YouTube 教程中使用它代替 while 循环?

非常感谢您的帮助!感觉今天的编程实践真的收获很大。你们(和女孩)都很棒!

更新和完整的代码:

#include <iostream>
#include <string>

using namespace std;

// This program displays a hero's inventory

int main()
{
const int MAX_ITEMS = 4;
string inventory[MAX_ITEMS];

int numItems = 0;
inventory[numItems++] = "Rusty Battle Axe";
inventory[numItems++] = "Leather Armor";
inventory[numItems++] = "Wooden Shield";

cout << "Inventory:\n";
for(int i = 0; i < numItems; ++i)
{
cout << inventory[i] << endl;
}

cout << "\nYou open a chest and find a Lesser Healing Potion.";
inventory[numItems++] = "Lesser Healing Potion";

cout << "\nInventory\n";
for(int i = 0; i < numItems; ++i)
{
cout << inventory[i] << endl;
}

cout << "\nYou also find a Short Sword.";
if(numItems < MAX_ITEMS)
{
inventory[numItems++] = "Short Sword";
}
else
{
cout << "\nYou have too many items and can't carry another.";
cout << "\nWould you like to trade the " << inventory[0] << " for the Short Sword? ";
}

while (true)
{
char answer;
cin >> answer;
switch(answer)
{
case 'y':
case 'Y':
inventory[0] = "Short Sword";
cout << "\nYou place the Rusty Battle Axe in the chest." << endl;
break;

case 'n':
case 'N':
inventory[0] = "Rusty Battle Axe";
cout << "\nYou leave the Short Sword in the chest." << endl;
break;

default:
cout << "\nThat was an invalid response!";
cout << "\nWould you like to trade the " << inventory[0] << " for the Short Sword? ";
continue;
}
break;
}

cout << "\nInventory:\n";
for(int i = 0; i < numItems; ++i)
{
cout << inventory[i] << endl;
}
return 0;
}

最佳答案

您可以使用在最后中断的一次性循环,然后使用 continue 跳回到顶部:

while(true)
{
switch(...) {
//...
default:
continue;
}
break;
};

也许更好的方法是定义一组有效的字母,特别是如果您将在代码中的任何地方做这种事情:

char GetChoice( const string & prompt, const string & valid_choices )
{
while( cin.good() )
{
cout << prompt << " " << flush;

char c;
if( !cin.get(c) ) break;

size_t pos = valid_choices.find(toupper(c));
if( pos != string::npos ) return valid_choices[pos];
}
return 0; // Error condition.
}

然后像这样使用:

switch( GetChoice("Do you want cake?", "YN") )
{
case 'Y':
cout << "Cake for you.\n";
break;
case 'N':
cout << "No cake for you.\n";
break;
case 0:
exit(1); // Error occurred
}

关于c++ - 有没有办法让 C++ Switch 语句循环回到第一种情况?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17182944/

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