如何在c++中添加循环

how to add loop in c++

本文关键字:添加 循环 c++      更新时间:2023-10-16
有人能帮我在程序中添加一个循环吗?

这是代码:

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)如果条件是true1,则括号内的代码将执行,如果条件是false0,则代码将退出循环

这个答案不需要在其他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++的新手,我会用我的代码来证明这是一个在你的情况下更实用的解决方案。代码经过测试,运行良好。