<tfoot id='ZtLJe'></tfoot>

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

        <legend id='ZtLJe'><style id='ZtLJe'><dir id='ZtLJe'><q id='ZtLJe'></q></dir></style></legend>
          <bdo id='ZtLJe'></bdo><ul id='ZtLJe'></ul>
        <i id='ZtLJe'><tr id='ZtLJe'><dt id='ZtLJe'><q id='ZtLJe'><span id='ZtLJe'><b id='ZtLJe'><form id='ZtLJe'><ins id='ZtLJe'></ins><ul id='ZtLJe'></ul><sub id='ZtLJe'></sub></form><legend id='ZtLJe'></legend><bdo id='ZtLJe'><pre id='ZtLJe'><center id='ZtLJe'></center></pre></bdo></b><th id='ZtLJe'></th></span></q></dt></tr></i><div id='ZtLJe'><tfoot id='ZtLJe'></tfoot><dl id='ZtLJe'><fieldset id='ZtLJe'></fieldset></dl></div>
      1. C++,得到一个无限循环

        C++, getting a infinite loop(C++,得到一个无限循环)
        <legend id='dQe0A'><style id='dQe0A'><dir id='dQe0A'><q id='dQe0A'></q></dir></style></legend>

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

            <tbody id='dQe0A'></tbody>

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

                <tfoot id='dQe0A'></tfoot>

                1. 本文介绍了C++,得到一个无限循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我尝试使用 switch 做一个简单的菜单.我还想检查用户是否进行了有效输入(仅限 1 到 4 的整数).在此检查中输入 -4 或 44 可以正常工作.但是如果我输入类似w"的东西,它会给我一个无限循环.我猜我需要另一个 if/else with if (!cin) blabla else 继续切换.但我不确定我是怎么做的,否则其他人正在启动开关.

                  i try to do a simple menu using switch. I also want to do a check if the user made a valid input (Only int from 1 to 4). Entering -4 or 44 is working fine with this check. But if i enter something like "w" it gives me a infinite loop. I'm guessing i need another if / else with if (!cin) blabla else go ahead with switch. But i'm not sure how i do that the else is starting the switch.

                   int menu() {
                          int enter;
                          bool exit = false;
                          do {
                              cout << "Wie soll angefangen werden: " << endl; //Enter your choice
                              cout << "1 - Spiel starten" << endl; // do game();
                              cout << "2 - Highscore " << endl; //do score();
                              cout << "3 - Quiz starten " << endl; //do quiz();
                              cout << "4 - Ende " << endl; //end the programm
                  
                          cin >> enter; 
                  
                          switch (enter) {
                              case 1:
                                  game();
                                  break;
                              case 2:
                                  score();
                                  break;
                              case 3:
                                  showQuizDialog();
                                  break;
                              case 4:
                                  exit = true;
                                  break;
                              default:
                                  cout << "Keine gültige Eingabe, nochmal: " << endl; //invalid input, again
                                  void flushCin();
                          } //end of switch
                      } while (exit == false); 
                  
                  }//end of menu();
                  

                  推荐答案

                  这是因为输入试图获取一个整数.当输入不是整数时,输入会留在缓冲区中,所以下次循环中相同的输入仍然存在.

                  It's because the input is trying to get an integer. When the input is not an integer, the input is left in the buffer, so next time around in the loop the same input is still there.

                  此外,在默认情况下,您不是调用flushCin 函数,而是声明它.您可能想要删除 void 关键字.我猜它做正确的事?(即调用 std::cin.ignore()std::cin::clear().)

                  Also, you are not calling the flushCin function in the default case, you are declaring it. You might want to remove the void keyword. I guess it does the correct thing? (I.e. calling std::cin.ignore() and std::cin::clear().)

                  这篇关于C++,得到一个无限循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  C++ stl unordered_map implementation, reference validity(C++ stl unordered_map 实现,参考有效性)
                  C++: Is it possible to use a reference as the value in a map?(C++:是否可以使用引用作为映射中的值?)
                  Where ampersand quot;amp;quot; can be put when passing argument by reference?(其中符号“amp;通过引用传递参数时可以放置吗?)
                  Why can a non-const reference parameter be bound to a temporary object?(为什么可以将非常量引用参数绑定到临时对象?)
                  What is a dangling reference?(什么是悬空引用?)
                  C++ reference changes when push_back new element to std::vector(当 push_back 新元素到 std::vector 时,C++ 引用发生变化)

                2. <small id='anGW0'></small><noframes id='anGW0'>

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

                          • <bdo id='anGW0'></bdo><ul id='anGW0'></ul>
                          • <legend id='anGW0'><style id='anGW0'><dir id='anGW0'><q id='anGW0'></q></dir></style></legend>