【问题标题】:infinite while loop with | operator c++无限while循环|运算符 c++
【发布时间】:2018-07-12 10:10:27
【问题描述】:

我正在阅读 Bjarne Stroustrup 的《使用 C++ 的原理和实践》,但我坚持进行一项特定的练习。确切的问题是:

“编写一个由 while 循环组成的程序,该循环(每次循环)读取两个整数,然后打印它们。当终止 '|' 时退出程序已输入。”

我认为下面的代码可以正常工作,但我不断得到一个永无止境的循环。

#include <iostream>

int main() {
  int i1 = 0;
  int i2 = 0;

  while ((i1 != '|') || (i2 != '|')) {
    std::cout << "Enter some nums YO (enter | to exit)\n";
    std::cin >> i1 >> i2;
    std::cout << i1 << i2;
  }

}

【问题讨论】:

  • 您误解了int 可以容纳什么。
  • @DrewDormann:int 几乎肯定可以容纳'|'。我想你们都误解了'|' 是什么。问题比这更微妙。

标签: c++ while-loop infinite


【解决方案1】:

int 存储一个数字。 '|' 是一个字符,然后使用char 读取/存储它。

char i1;
char i2;

示例:

int main() {

    char i1;
    char i2;

    while ((i1 != '|') || (i2 != '|')) {
        std::cout << "Enter some CHARACTERS (enter '|' to exit)\n";
        std::cin >> i1 >> i2;
        std::cout << i1 << i2;
    }
}

【讨论】:

  • 不,也不行。作业要求您输入数字,此代码不接受 42。
  • 他只是通过添加作业说明来编辑问题.___。
【解决方案2】:

循环继续进行,因为您正在对整数变量和字符值进行比较

while(i1!='|'||i2!='|)//does not work

因为 i1 和 i2 只能拥有数值 (0123456789) 而不能拥有字符 (a,b,c,!,ª,|) i1 和 i2 永远不能取值 '|'循环一遍又一遍地运行。 应该起作用的一件事:

    int main() {
  int i1 = 0;
  int i2 = 0;
  char c;//declare c
  while (c!='|') {//if the value of c is not equal to '|' run the loop
    cout << "Enter some 2 nums and a character YO (enter | to exit)\n";
    cin >> i1 >> i2 >> c;//give a value to i1,i2 & c
    cout << i1 << i2;
}

}

【讨论】:

    【解决方案3】:
    std::cin >> i1 >> i2;
    

    告诉程序读取两个整数,因此程序在流中查找数字。如果输入“|”,则找不到数字,流将进入失败状态。在可以接受更多输入之前的失败状态will need to be clearedbad input ignoreed 的其余部分。但是,如果您输入字符“|”的数字编码(124假设ASCII编码),流输入和条件都会满足,但现在不可能输入数字124和意思是124而不是'|'。

    改为阅读std::strings。如果两个字符串都不是“|”,则测试字符串是否真的是整数(可能通过尝试将它们转换为integers with std::stoi)。如果找到“|”,则退出循环。

    MSalter 的评论让另一种解决方案松动了,它比去strings 要简单得多。如果读取了 2 个数字,请打印这些数字并要求更多。否则,清除读取非整数的错误并读取char 以测试“|”。这是一个非常简单而丑陋的剪辑:

    #include <iostream>
    
    int main()
    {
        int i1 = 0;
        int i2 = 0;
        char ch = ' '; // can be anything but |
        do
        {
            std::cout << "Enter some nums YO (enter | to exit)\n";
            if (std::cin >> i1 >> i2) 
            { // we got nums, YO.
                std::cout << i1 << i2; // print the nums, YO
            }
            else
            { // not nums, YO. Let's see if we got a |
                std::cin.clear(); // clear the error
                std::cin >> ch; //read for a '|'
            }
        } while (ch != '|' && std::cin); // loop until | is read or cin can't read a char
    }
    

    这很难看,因为像“auoaioafksdjnfkm”这样的输入会为每个字符打印出一个提示,而“klasdjfahs12938 2093854sjfljzlkfjzlk”会打印出过多的提示,并且在中间某处会打印出一个“129382093854”。

    【讨论】:

    • 注意,对于这个特定的赋值,不需要清除错误状态:程序可能在|之后终止
    • @MSalters 同意,考虑到这一点会产生更好的解决方案。
    【解决方案4】:

    从控制台读取一行文本。如果该行包含|,则退出。否则,解析该行。

    int main()
    {
        for (;;)
        {
            std::cout << "Enter some nums YO (enter | to exit)" << std::endl;
            std::string s;
            std::getline(cin, s);
            if (s.find('|') != s.npos)
            {
                break;
            }
            std::istringstream ss(s);
            int i1, i2;
            ss >> i1 >> i2;
            std::cout << i1 << ' ' << i2 << std::endl;
        }
        return 0;
    }
    

    【讨论】:

      猜你喜欢
      • 2016-08-27
      • 2015-04-29
      • 1970-01-01
      • 2016-04-21
      • 2021-10-15
      • 2017-01-22
      • 1970-01-01
      • 2013-02-22
      • 1970-01-01
      相关资源
      最近更新 更多