<small id='zrY3k'></small><noframes id='zrY3k'>

  • <tfoot id='zrY3k'></tfoot>

      <bdo id='zrY3k'></bdo><ul id='zrY3k'></ul>
    <i id='zrY3k'><tr id='zrY3k'><dt id='zrY3k'><q id='zrY3k'><span id='zrY3k'><b id='zrY3k'><form id='zrY3k'><ins id='zrY3k'></ins><ul id='zrY3k'></ul><sub id='zrY3k'></sub></form><legend id='zrY3k'></legend><bdo id='zrY3k'><pre id='zrY3k'><center id='zrY3k'></center></pre></bdo></b><th id='zrY3k'></th></span></q></dt></tr></i><div id='zrY3k'><tfoot id='zrY3k'></tfoot><dl id='zrY3k'><fieldset id='zrY3k'></fieldset></dl></div>
  • <legend id='zrY3k'><style id='zrY3k'><dir id='zrY3k'><q id='zrY3k'></q></dir></style></legend>

        如果我输入一个字母,我的程序怎么会卡在它的 while 循环中?

        时间:2023-08-27
        • <i id='JIi5L'><tr id='JIi5L'><dt id='JIi5L'><q id='JIi5L'><span id='JIi5L'><b id='JIi5L'><form id='JIi5L'><ins id='JIi5L'></ins><ul id='JIi5L'></ul><sub id='JIi5L'></sub></form><legend id='JIi5L'></legend><bdo id='JIi5L'><pre id='JIi5L'><center id='JIi5L'></center></pre></bdo></b><th id='JIi5L'></th></span></q></dt></tr></i><div id='JIi5L'><tfoot id='JIi5L'></tfoot><dl id='JIi5L'><fieldset id='JIi5L'></fieldset></dl></div>

          <legend id='JIi5L'><style id='JIi5L'><dir id='JIi5L'><q id='JIi5L'></q></dir></style></legend>

              <bdo id='JIi5L'></bdo><ul id='JIi5L'></ul>

                  <small id='JIi5L'></small><noframes id='JIi5L'>

                  <tfoot id='JIi5L'></tfoot>
                    <tbody id='JIi5L'></tbody>
                  本文介绍了如果我输入一个字母,我的程序怎么会卡在它的 while 循环中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  对不起,如果我说得不够清楚或犯了任何错误,这是我第一次发帖.

                  Sorry if I fail to be clear enough or make any mistakes, this is my first time posting.

                  我的代码在编译时运行没有错误,但第一个 while 循环(在 int main 中)在用户输入字母(如 "a")时卡住循环cin>>select; 而不是必需的 123.

                  My code runs without errors when complied but the first while loop (in int main) gets stuck looping whenever a user types a letter (like "a") for cin >> select; instead of the required 1, 2, or 3.

                  但是,当我输入 "4" 或任何其他随机数字字符串时,它运行良好并像它应该的那样转到我的错误消息.

                  However, when I input "4" or any other random string of numbers, it runs fine and goes to my error message like it should.

                  这是为什么,我该怎么做才能让它正常运行?(运行错误消息以响应输入的字母,就好像它们是数字一样).

                  Why is this and what can I do to make it run normally? (run the error message in response to letters entered as if they were numbers).

                  我的代码:

                  #include <iostream>
                  #include <string>
                  using namespace std;
                  
                  void calculator();
                  void unavailableitem();
                  
                  int main()
                  {
                      string select;
                      while (true)
                      {
                          cout << "	[Main Menu]
                  ";
                          cout << " 1. Calculator
                  ";
                          cout << " 2. [unavailable]
                  ";
                          cout << " 3. [unavailable]
                  ";
                          cout << "
                   Enter the number of your selection: ";
                          cin >> select;
                  
                          if (select == "1")
                          {
                              cout << endl;
                              calculator();
                              break;
                          }
                          else if (select == "2")
                          {
                              unavailableitem();
                              break;
                          }
                          else if (select == "3")
                          {
                              unavailableitem();
                              break;
                          }
                          else
                              cout << "
                  Invalid response.
                  ";
                      }
                  }
                  
                  void unavailableitem()
                  {
                      string react;
                      cout << "
                   	 [ITEM UNAVAILABLE]
                  ";
                      while (true)
                      {
                          cout << "
                  Enter 'menu' to return to main menu: ";
                          cin >> react;
                  
                          if (react == "menu")
                          {
                              cout << endl;
                              main();
                              break;
                          }
                          else
                              cout << "
                  Invalid response.
                  ";
                      }
                  }
                  
                  void calculator()
                  {
                      int choice;
                      double num1;
                      double num2;
                      double answer;
                      string choicesymbol;
                  
                      cout << "List of operations:
                  ";
                      cout << " 1. Addition
                  ";
                      cout << " 2. Subtraction
                  ";
                      cout << " 3. Multiplication
                  ";
                      cout << " 4. Division
                  ";
                      cout << "Enter the number on the left to pick an operation: ";
                      cin >> choice;
                      cout << "
                  Enter number 1: ";
                      cin >> num1;
                      cout << "
                  Enter number 2: ";
                      cin >> num2;
                  
                      if (choice == 1)
                      {
                          answer = num1 + num2;
                          choicesymbol = " + ";
                      }
                  
                      if (choice == 2)
                      {
                          answer = num1 - num2;
                          choicesymbol = " - ";
                      }
                  
                      if (choice == 3)
                      {
                          answer = num1 * num2;
                          choicesymbol = " * ";
                      }
                  
                      if (choice == 4)
                      {
                          answer = num1 / num2;
                          choicesymbol = " / ";
                      }
                  
                      cout << endl;
                      cout << num1 << choicesymbol << num2 << " = " << answer;
                  }
                  

                  新代码:

                  #include <iostream>
                  #include <string>
                  using namespace std;
                  
                  void calculator();
                  void unavailableitem();
                  
                  
                  int main()
                  {
                      int select;
                      while (true)
                      {
                          cout << "	[Main Menu]
                  ";
                          cout << " 1. Calculator
                  ";
                          cout << " 2. [unavailable]
                  ";
                          cout << " 3. [unavailable]
                  ";
                          cout << "
                   Enter the number of your selection: ";
                          cin >> select;
                  
                          if(!(cin >> select))
                          {
                          cout << "Input must be an integer.
                  ";
                          cin.clear();
                          continue;
                          }
                          else if (select == 1)
                          {
                              cout << endl;
                              calculator();
                              break;
                          }
                          else if (select == 2)
                          {
                              unavailableitem();
                              break;
                          }
                          else if (select == 3)
                          {
                              unavailableitem();
                              break;
                          }
                      }
                  }
                  
                  
                  
                  void unavailableitem()
                  {
                      string react;
                      cout << "
                   	 [ITEM UNAVAILABLE]
                  ";
                      while (true)
                      {
                          cout << "
                  Enter 'menu' to return to main menu: ";
                          cin >> react;
                  
                          if (react == "menu")
                          {
                              cout << endl;
                              return;
                              break;
                          }
                          else
                              cout << "
                  Invalid response.
                  ";
                      }
                  }
                  
                  void calculator()
                  {
                      int choice;
                      double num1;
                      double num2;
                      double answer;
                      string choicesymbol;
                  
                      cout << "List of operations:
                  ";
                      cout << " 1. Addition
                  ";
                      cout << " 2. Subtraction
                  ";
                      cout << " 3. Multiplication
                  ";
                      cout << " 4. Division
                  ";
                      cout << "Enter the number on the left to pick an operation: ";
                      cin >> choice;
                      cout << "
                  Enter number 1: ";
                      cin >> num1;
                      cout << "
                  Enter number 2: ";
                      cin >> num2;
                  
                      if (choice == 1)
                      {
                          answer = num1 + num2;
                          choicesymbol = " + ";
                      }
                  
                      if (choice == 2)
                      {
                          answer = num1 - num2;
                          choicesymbol = " - ";
                      }
                  
                      if (choice == 3)
                      {
                          answer = num1 * num2;
                          choicesymbol = " * ";
                      }
                  
                      if (choice == 4)
                      {
                          answer = num1 / num2;
                          choicesymbol = " / ";
                      }
                  
                      cout << endl;
                      cout << num1 << choicesymbol << num2 << " = " << answer;
                  }
                  

                  推荐答案

                  总的来说,我是编程的新手,但是使用您的代码和查找内容让我找到了某种解决方案.cin.clear 只清除输入的错误日志,相信还是保留了字母的值.

                  I am a newbie to programming in general, but playing with your code and looking up stuff made me find some sort of solution. The cin.clear only clears the error log of the input, and I believe that it still retains the value of the letter.

                  你应该在后面添加一个 cin.ignore(#,' ')(# 是一个非常非常大的数字)让它避开该行并直接跳过它.

                  What you should add right after is a cin.ignore(#,' ') (# being a very, very large number) to have it avoid the line and skip right through it.

                  在另一个中找到了解决方案 问题解释了两个cin命令的使用.

                  Found the solution in another question that explains the use of both cin commands.

                  这篇关于如果我输入一个字母,我的程序怎么会卡在它的 while 循环中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:使用while循环计算数字 下一篇:可能有多个 while (cin>>input)

                  相关文章

                  <small id='ltVjJ'></small><noframes id='ltVjJ'>

                  • <bdo id='ltVjJ'></bdo><ul id='ltVjJ'></ul>

                  <i id='ltVjJ'><tr id='ltVjJ'><dt id='ltVjJ'><q id='ltVjJ'><span id='ltVjJ'><b id='ltVjJ'><form id='ltVjJ'><ins id='ltVjJ'></ins><ul id='ltVjJ'></ul><sub id='ltVjJ'></sub></form><legend id='ltVjJ'></legend><bdo id='ltVjJ'><pre id='ltVjJ'><center id='ltVjJ'></center></pre></bdo></b><th id='ltVjJ'></th></span></q></dt></tr></i><div id='ltVjJ'><tfoot id='ltVjJ'></tfoot><dl id='ltVjJ'><fieldset id='ltVjJ'></fieldset></dl></div>

                  <tfoot id='ltVjJ'></tfoot>
                  <legend id='ltVjJ'><style id='ltVjJ'><dir id='ltVjJ'><q id='ltVjJ'></q></dir></style></legend>