如何在c++中添加循环
how to add loop in c++
有人能帮我在程序中添加一个循环吗?
这是代码:
cout << "What do you do?" "n";
cout << "n";
cout << "(Press Enter...)";
cin.ignore();
cout << "n";
cout << "Stay in bed?" "n";
cout << "Go to the bathroom?" "n";
cout << "Go downstairs?" "n";
cout << "n";
string answer;
getline(cin, answer);
if (answer == "stay in bed", "Stay in bed")
{
cout << "You lay there, motionless. Silent.";
}
else if (answer == "go to the bathroom", "Go to the bathroom")
{
cout << "You get up and walk across the hall to the bathroom";
}
else if (answer == "go downstairs", "Go downstairs")
{
cout << "You get up and walk downstairs to the kitchen.";
}
else
{
cout << "That is not a valid answer...";
}
cin.ignore();
我应该如何添加一个循环,当用户输入处于"else"条件下的内容时,循环会返回询问"What do you do?">
有不止一种方法可以做你想做的事情(或者我认为你想做什么(。一种方法是将整个事情放入一个无休止的循环中,并使用break
退出:
while(1)
{
cout << "What do you do?n";
getline(cin, answer);
if (answer == "stay in bed")
{
cout << "You lay there, motionless. Silent.";
break;
}
else if (answer == "go to the bathroom")
{
cout << "You get up and walk across the hall to the bathroom";
break;
}
}
bool repeatInput = false;
do
{
cout << "What do you do?" "n";
cout << "n";
cout << "(Press Enter...)";
cin.ignore();
cout << "n";
cout << "Stay in bed?" "n";
cout << "Go to the bathroom?" "n";
cout << "Go downstairs?" "n";
cout << "n";
repeatInput = false;
getline(cin, answer);
if (answer == "stay in bed" || answer == "Stay in bed")
{
cout << "You lay there, motionless. Silent.";
}
else if (answer == "go to the bathroom" || answer == "Go to the bathroom")
{
cout << "You get up and walk across the hall to the bathroom";
}
else if (answer == "go downstairs" || answer == "Go downstairs")
{
cout << "You get up and walk downstairs to the kitchen.";
}
else
{
cout << "That is not a valid answer...";
repeatInput = true;
}
cin.ignore();
}while(repeatInput == true);
使用while
,您可以在程序中添加循环
#include <iostream>
using namespace std;
int main()
{
while (true) {
cout << "What do you do?" "n";
cout << "n";
cout << "(Press Enter...)";
cout << "n";
cout << "Stay in bed?" "n";
cout << "Go to the bathroom?" "n";
cout << "Go downstairs?" "n";
cout << "n";
string answer;
getline(cin, answer);
if (answer == "stay in bed", "Stay in bed")
{
cout << "You lay there, motionless. Silent." << endl;
}
else if (answer == "go to the bathroom", "Go to the bathroom")
{
cout << "You get up and walk across the hall to the bathroom" << endl;
}
else if (answer == "go downstairs", "Go downstairs")
{
cout << "You get up and walk downstairs to the kitchen." << endl;
}
else
{
cout << "That is not a valid answer..." << endl;
}
cout << endl;
}
return 0;
}
该代码while(true)
如果条件是true
或1
,则括号内的代码将执行,如果条件是false
或0
,则代码将退出循环
这个答案不需要在其他if函数中插入break。我使用了"continue",希望你能探索c++语言并学习更多!
bool check_if_true=true;
while(check_if_true){
cout << "What do you do?" "n";
cout << "n";
cout << "(Press Enter...)";
cin.ignore();
cout << "n";
cout << "Stay in bed?" "n";
cout << "Go to the bathroom?" "n";
cout << "Go downstairs?" "n";
cout << "n";
string answer;
getline(cin, answer);
if (answer == "stay in bed", "Stay in bed")
{
cout << "You lay there, motionless. Silent.";
}
else if (answer == "go to the bathroom", "Go to the bathroom")
{
cout << "You get up and walk across the hall to the bathroom";
}
else if (answer == "go downstairs", "Go downstairs")
{
cout << "You get up and walk downstairs to the kitchen.";
}
else
{
cout << "That is not a valid answer...";
continue;//return to the start of the while loop
}
cin.ignore();
check_if_true=false;
}
在为用户提供选项时,应该使用整数标识符。
#include <iostream>
using namespace std;
int main()
{
int id;
cout << "What do you wanna do?n" << endl;
cout << "(0) Quit program" << endl;
cout << "(1) Stay in bed" << endl;
cout << "(2) Go to the bathroom" << endl;
cout << "(3) Go downstairsn" << endl;
while (id != 0)
{
cout << "Choose your option: ";
cin >> id;
switch (id)
{
case 1:
cout << "You lay there, motionless. Silent." << endl;
break;
case 2:
cout << "You get up and walk across the hall to the bathroom." << endl;
break;
case 3:
cout << "You get up and walk downstairs to the kitchen." << endl;
break;
case 0:
cout << "Exitting application!" << endl;
break;
default:
cout << "Error: Invalid option!" << endl;
break;
}
}
return 0;
}
代码要干净得多,而且易于扩展。在表示选项时,用户永远不应该输入字符串。唯一的例外是,如果你有一个底层程序来扫描句子,并从句子中的"关键字"派生出一个动作过程。这是算法密集型的,可能不是你想要的,假设你是C++的新手,我会用我的代码来证明这是一个在你的情况下更实用的解决方案。代码经过测试,运行良好。
相关文章:
- 如何在C++中从两个单独的for循环中添加两个数组
- 如何通过替换顺序代码的while循环来添加OpenMP for循环
- C++ 在循环中添加计数器变量并再次初始化其值
- 如何添加循环期间分配的变量?
- 是否可以在 for 循环中添加两个浮点数?
- 需要循环帮助以迭代方式添加到程序集中的总和变量
- 添加#pragma循环后出现Segfault
- 在 C++ 中向 rand() 添加模数会影响 for 循环之外的执行,为什么以及如何补救?
- C++ 循环逐行写入文件还是添加到数组并在循环后写入文件?
- 如何从 while 循环中添加数字
- 循环以使用设置模板类将所有 26 个字母添加到 s 中
- C++在 while 循环后添加一行代码会导致错误
- 为什么if语句和变量声明要比循环中的添加更快
- C++:如何在"for"循环中动态地向文本行添加后缀以创建先前声明的变量
- 为什么可以添加填充使您的循环更快
- 当循环崩溃时,不向数组添加单词
- 不知道如何向我的程序添加循环...?
- 如何在加密++库的AES中找到添加循环密钥和混合列代码
- 如何在c++中添加循环
- 如何添加循环输入文件的结果