在C++的while循环中陷入无限循环,为什么会发生这种情况?

huangapple go评论84阅读模式
英文:

Getting infinite loop in C++ while-loop, why is this happening?

问题

以下是您提供的代码的翻译部分:

#include <iostream>
using namespace std;

int main()
{
    int val1 = 0, val2 = 0;
    bool a = true;

    while (a)
    {
        cout << "Enter 2 numbers: " << endl;
        cin >> val1 >> val2;
        if (val1 == '|' || val2 == '|')
            a = false; // 终止循环
        else
            cout << val1 << " " << val2 << endl;
    }
}

请注意,此程序中的问题在于条件 if (val1 == '|' || val2 == '|')。这里使用了单引号而不是双引号,应该使用双引号来表示字符 '|'。修正后的条件应该是 if (val1 == '|' || val2 == '|')。这样才能正确地检测到输入的 '|' 字符以终止循环。

英文:
#include&lt;iostream&gt;
using namespace std;

int main()
{

 int val1=0, val2=0;
 bool a=true;
 
 while(a)
 {
  cout &lt;&lt; &quot;Enter 2 numbers: &quot; &lt;&lt; endl;
  cin &gt;&gt; val1 &gt;&gt; val2;
  if( val1==&#39;|&#39; || val2==&#39;|&#39; ) a=false;	//terminate loop
  else cout &lt;&lt; val1 &lt;&lt; &quot; &quot; &lt;&lt; val2 &lt;&lt; endl;
 }

}

Write a program that consists of a while-loop that (each time around the loop) reads in two ints and then prints them.
Exit the program when a terminating '|' is entered.

This is what i want to program, code works well until entering non digit charachter. even '|' makes it to go in infinite loop. I just want to know why? Could not figure it out

答案1

得分: 3

The &gt;&gt; overload for a std::istream expects to read an integer number on input. An integer number is, well, an integer number. It would be 0, 1, -5, or 42. These are integer numbers.

The | character is not an integer number.

Because when an input operation fails, in this manner, the input stream is set to a failed state. When an input stream is in a failed state all further attempted formatted input operations immediately fail without setting the target of the formatted input operation.

So, entering | on input leaves val1 and val2 unchanged, and the stream in a failed state. The input stream remains in a failed state, and the same thing happens on the next iteration of the loop.

And that's why you get an infinite loop.

This line does not do what you think it does. val1 and val2 are integers. A character is also an integer, interpreted as an ASCII code, so what this really does is compare val1 and val2 to the code for the ASCII character |. But, for the reasons explained above, inputting | does not set an int value to its ASCII code, &gt;&gt; does not work this way. It leaves the input stream in a failed state, &gt;&gt; on an int expects to read an actual, real number, something that would correspond to the number of fingers you have, if you had a corresponding number of fingers (or short the requisite amount in case of a 0 or a negative number, I suppose...)

英文:
cin &gt;&gt; val1 &gt;&gt; val2;

The &gt;&gt; overload for a std::istream expects to read an integer number on input. An integer number is, well, an integer number. It would be 0, 1, -5, or 42. These are integer numbers.

The | character is not an integer number.

> I just want to know why?

Because when an input operation fails, in this manner, the input stream is set to a failed state. When an input stream is in a failed state all further attempted formatted input operations immediately fail without setting the target of the formatted input operation.

So, entering | on input leaves val1 and val2 unchanged, and the stream in a failed state. The input stream remains in a failed state, and the same thing happens on the next iteration of the loop.

And that's why you get an infinite loop.

if( val1==&#39;|&#39; || val2==&#39;|&#39; )

This line does not do what you think it does. val1 and val2 are integers. A character is also an integer, interpreted as an ASCII code, so what this really does is compare val1 and val2 to the code for the Ascii character |. But, for the reasons explained above, inputting | does not set an int value to its ASCII code, &gt;&gt; does not work this way. It leaves the input stream in a failed state, &gt;&gt; on an int expects to read an actual, real number, something that would correspond to the number of fingers you have, if you had a corresponding number of fingers (or short the requisite amount in case of a 0 or a negative number, I suppose...)

huangapple
  • 本文由 发表于 2023年5月26日 09:47:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/76337180.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定