好的,这是一个简单的代码示例:
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.";
}
显然我可以用一段时间拉出我的头发(回答!='Y'||回答!= ...)但是在执行默认情况后,是否有一种更优雅的方式简单地回到第一种情况?因此,如果用户输入了错误的字母,我只需再问他们问题,直到他们输入可接受的答案为止?
不,这不是家庭作业或任何事情。我正在研究道森的C ++游戏编程书,我希望通过允许用户保留或交易项目来使程序示例稍微高兴。我把所有这些工作都很漂亮,但是如果输入了错误的响应,它只显示库存的内容并退出。我想要做对了。强制用户输入正确的响应,然后显示更新的库存。
感谢帮助!
更新<!/强> 你们都给了我很多不同的方法 - 我真的很感激!我承认我可能没有正确设计这个开关语句,我为这个矛盾道歉。我会尝试你的每一个建议并在这里回复,选择一个作为答案。谢谢!
好的,我刚刚完成了你的所有答案,用我的代码尝试了大部分答案。我选择了最简单,最优雅的解决方案作为我问题的答案。但是你们都帮助我看到了不同的看待方式,现在我对switch语句了解得更多。实际上使用它代替教程中的while循环我现在正在YouTube上关注用户什么是Creel?
我真的很感谢你的帮助!我觉得我今天的编程实践中确实取得了很多成就。你们(和女孩们)都很棒!
更新和完整的代码:
#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;
}
答案 0 :(得分:2)
好吧,添加一个循环,它会“循环”到你想要的任何地方。
请注意,switch
的整个主体只是一个带有标签的长语句。一旦您通过其中一个标签输入,它就像任何其他声明一样工作。就像一个普通的C ++语句本身不会为你“循环”,除非你让它成为一个循环或使用goto
,switch
的主体也不会为你自己循环回来。
因此,如果您想要转回控制权,请使用相应的语言结构。您可以将goto
注入该语句的正文中,它将照常工作。
switch(answer)
{
case 'y':
case 'Y':
FIRST_OPTION:
...
break;
default :
...;
goto FIRST_OPTION; // Jump to first option
}
您可能还想查看Duff's device,了解switch
语句中更复杂的控制转移示例。
然而,你的问题似乎与自相矛盾。如果答案无效,则表明您要再次询问用户输入。但是在switch
之外请求并接受用户输入。那么为什么你要回到switch
???
答案 1 :(得分:2)
你可以使用最后破解的一次性循环并使用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
}
答案 2 :(得分:2)
bool valid;
do
{
char answer;
cin >> answer;
switch (answer)
{
case 'y':
case 'Y':
inventory[0] = "Short Sword";
cout << "\nYou place the Rusty Battle Axe in the chest.";
valid = true;
break;
case 'n':
case 'N':
inventory[0] = "Rusty Battle Axe";
cout << "\nYou leave the Short Sword in the chest.";
valid = true;
break;
default :
cout << "\nThat was an invalid response.";
valid = false;
break;
}
}
while (!valid);
答案 3 :(得分:1)
使用默认部分中的goto语句返回输入部分
答案 4 :(得分:1)
这是一种方法:
bool done = false;
while (!done) {
char answer;
cin >> answer;
done = true;
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.";
done = false;
}
}
答案 5 :(得分:1)
使用while
或do while
循环。
例如:
char answer;
bool loopback = true;
do
{
cin >> answer;
switch(answer)
{
case 'y':
case 'Y':
inventory[0] = "Short Sword";
cout << "\nYou place the Rusty Battle Axe in the chest.";
loopback = false;
break;
case 'n':
case 'N':
inventory[0] = "Rusty Battle Axe";
cout << "\nYou leave the Short Sword in the chest.";
loopback = false;
break;
default :
cout << "\nThat was an invalid response.";
loopback = true;
}
}
while (loopback);
答案 6 :(得分:0)
您可以使用label
和goto
声明。标记要求用户输入的语句,并在goto
情况下添加default
语句。
实施例::
AskQuestion:
cout << "Press 'Y' for Short Sword Or 'N' for Rusty Battle Axe" << endl;
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.";
goto AskQuestion ;
}
替代方法是使用条件do-while
的{{1}}循环,因为您已经在问题中发表了评论。实施例::
while(answer != 'Y' || answer !=...)