【问题标题】:How to repeat input command if user input is invalid with a string variable - C++如果用户输入对字符串变量无效,如何重复输入命令 - C++
【发布时间】:2020-06-07 04:35:03
【问题描述】:

所以我的编码经验很少,我写的代码有这个问题,如果第一次正确选择“是”,要求用户再次输入。如果用户输入“否”或用户输入无效选项,则它可以正常工作,则下一组问题将起作用。我还没有找到任何不使用数组来处理字符串变量的示例。谢谢 - 附言我知道它的糟糕形式,但我只是想让它发挥作用。

#include<string>
#include<iostream>

using namespace std;

int main() {
    string choice;

    cout<<"Do you choose to go fight in the war??\n\n";
    cout << "choose yes or no\n";
    cin >> choice;

    while(choice != "yes" || choice != "no")
    {
        cout << "pls enter again\n";
        cin >> choice;
        if(choice == "no") 
        {
            cout << "you live";
            break;
        }
        else(choice == "yes");
        {
            cout << "you die";
            break;
        }
    }
}

【问题讨论】:

    标签: c++ string loops if-statement user-input


    【解决方案1】:

    你需要else if,而不是else

    else if (choice == "yes") {
      cout << "you die";
      break;
    }
    

    【讨论】:

      【解决方案2】:

      一种方法是使用无限循环来处理输入。如果给出了有效的输入,则中断循环。

      using namespace std;
      
      int main()
      {
          string choice;
      
          cout << "Do you choose to go fight in the war??\n\n";
          cout << "choose yes or no\n";
      
          while (true) {
              cin >> choice;
              if (choice == "no") {
                  cout << "you live";
                  break;
              }
              else if (choice == "yes")
              {
                  cout << "you die";
                  break;
              }
              else {
                  cout << "pls enter again\n";
              }
          }
      
          return 0;
      }
      

      【讨论】:

        【解决方案3】:

        当我开始学习编码时,我遇到了同样的逻辑问题,就像你现在所遇到的一样。我只是认为您在语法和编码逻辑方面遇到问题。希望我的代码能有所帮助!

         #include <iostream>
         #include <string>
         using namespace std;
        
         int main() {
           string choice;
        
           do {
               cout << "Do you choose to go fight in the war??\n";
               cout << "Choose yes or no\n";
               cin >> choice;
        
               if (choice == "no") {
        
                cout << "you live\n";
                break;
        
               } else if (choice == "yes") {
        
                cout << "you die\n";
                break;
               }
        
           } while (choice != "yes" && choice != "no");
        
           return 0;
         }
        

        【讨论】:

          【解决方案4】:

          对字符串输入使用 do-while 循环并在循环后应用条件

          #include<iostream>
          using namespace std;
          main()
          {
              String choice;
              cout << "Do you choose to go fight in the war??\n\n";
              cout << "choose yes or no\n";
              do
              {
                  cin >> choice;
                  If(choice != "yes" || choice != "no") 
                        Cout<<"please enter again";
               }
               while (choice != "yes" || choice != "no");
               If (choice == "no")
              {
                    cout << "you live";
               } 
              else
              {
                    cout << "you die";
               }
          
          }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2021-08-25
            • 2021-10-13
            • 2014-09-30
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多