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

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

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

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

      1. <small id='eytj5'></small><noframes id='eytj5'>

        当 cin 的输入是一个“点"时,while 循环到无穷大

        while loop to infinity when the input of a cin is a #39;dot#39;(当 cin 的输入是一个“点时,while 循环到无穷大)
          <tbody id='ZH7dX'></tbody>

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

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

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

                  本文介绍了当 cin 的输入是一个“点"时,while 循环到无穷大的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我在使用 cin 方法获取变量时遇到问题.当输入的是数字时没有问题,但是当输入的是像点[.]这样的特殊字符时,whileloop 循环到无穷大.我做错了什么?

                  I am having trouble using the cin method to acquire a variable. When the input is a number there is no problem, but when it is a special character like a dot [.], the whileloop loops into infinity. What am I doing wrong?

                  cout << "What is your race" <<endl<<"1.Human
                  2.troll
                  3.zombie"<<endl;
                      cin >> *race;
                      while(*race<1||*race>3)
                      {
                      system("cls");
                      cout << "Wrong choice"<<endl<< "What is your race" <<endl<<"1.Human
                  2.troll
                  3.zombie"<<endl;
                      cin >> *race;
                      }
                  

                  我搜索了答案,我应该刷新缓冲区,但我不知道该怎么做.我对 C++ 很陌生.谢谢

                  I searched for the answer and i should have to flush the buffer but i don"t get how to do it. I'm rather new with c++. Thanx

                  推荐答案

                  使 race 成为一个字符,那么你将能够做到:

                  Make race an char, then you will be able do to:

                  while (*race < '1' || *race > '3')
                  

                  这可能是您想要实现的目标.

                  which is probably what you want to achieve.

                  说明:

                  当您将 cin >> 转换为 int 时,它会将给定的 ASCII 字符串转换为整数值.. 没有整数意义,所以它不会被读入 race 并且 failbit 被设置 - 进一步的 >>s 不是 -操作,直到你清除它们.但是,如果您将 cin >> 转换为 char 并将其与其他 char 进行比较(好吧,实际上是它们的 ASCII 代码),您将能够毫无困难地检查它.

                  When you cin >> into an int, it converts given ASCII string to integer value. . doesn't have an integer meaning, so it isn't read into race and failbit is set - further >>s are no-op, until you clear them. However, if you cin >> into char and compare it with other chars (well, their ASCII codes, actually), you will be able to check it without troubles.

                  这篇关于当 cin 的输入是一个“点"时,while 循环到无穷大的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  What is inside .lib file of Static library, Statically linked dynamic library and dynamically linked dynamic library?(静态库、静态链接动态库和动态链接动态库的 .lib 文件里面是什么?)
                  How do I load a C DLL from the SXS in Python?(如何从 Python 中的 SXS 加载 C DLL?)
                  Can Cython code be compiled to a dll so C++ application can call it?(Cython 代码可以编译成 dll 以便 C++ 应用程序可以调用它吗?)
                  Delay Loading DLLs(延迟加载 DLL)
                  Throwing C++ exceptions across DLL boundaries(跨 DLL 边界抛出 C++ 异常)
                  Loading a dll from a dll?(从 dll 加载 dll?)
                    <bdo id='jOuC8'></bdo><ul id='jOuC8'></ul>

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

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

                      2. <legend id='jOuC8'><style id='jOuC8'><dir id='jOuC8'><q id='jOuC8'></q></dir></style></legend><tfoot id='jOuC8'></tfoot>
                              <tbody id='jOuC8'></tbody>