<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 循环到无穷大

        时间:2023-08-28
          <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 循环到无穷大的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:我要求用户输入大小和数组,但是当我打印矢量时,它仅显示“0"作为输出 下一篇:非终止 while 循环

                  相关文章

                    <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>