【问题标题】:Cannot exit While loop无法退出 While 循环
【发布时间】:2019-10-24 06:03:43
【问题描述】:

当变量“持续时间”达到 5 秒时,我想退出 While 循环。

变量“持续时间”总是=0

#include <iostream>
#include <Windows.h>
#include <chrono>

using namespace std;
DOUBLE duration;


int main()
{

    using clock = std::chrono::system_clock;
    using sec = std::chrono::duration <double>;
    const auto before = clock::now();
    while (duration < 5.0)     
    {
        const auto after = clock::now();
        const sec duration = after - before;
        std::cout << "It took " << duration.count() << "s" << std::endl;

    }
    std::cout << "After While Loop ";  //Never reaches this line!!!!!
    return 0;
}

实际输出:.
.
花了 9.50618 秒
它工具 9.50642s
.
我预计 while 循环会在 5.0 或更高版本退出。

显示变量始终显示 0。

【问题讨论】:

  • duration 您正在打印的是该循环的本地。虽然您正在签入的 duration while 循环是不同的
  • 您的“实际输出”看起来不像是所示代码的输出。
  • 被忽视Variable shadowing的经典案例
  • 谢谢大家。它有效。

标签: c++ while-loop duration


【解决方案1】:

您正在使用两个名为 duration 的单独变量。

using sec = std::chrono::duration <double>; // number 1
const auto before = clock::now();
while (duration < 5.0) // checking number 1, which is not changed during looping    
{
    const auto after = clock::now();
    const sec duration = after - before; // number 2, created afresh in each iteration 
    std::cout << "It took " << duration.count() << "s" << std::endl;

}

因此,您在循环条件中检查的内容并不是在循环主体中发生更改的内容。

【讨论】:

    【解决方案2】:

    你有两个变量叫做持续时间:

    DOUBLE duration;
    

    在while循环中:

    const sec duration = after - before;
    

    在评估while 语句期间,您正在检查第一个从未设置或更改的语句。 在while 循环体内,您正在创建一个隐藏全局变量的新变量。见https://en.wikipedia.org/wiki/Variable_shadowing

    在 Insead 中,您不应声明新变量,而应使用在进入 while 循环之前声明的变量。

    【讨论】:

    • 漂亮的背景链接。 (对不起,我之前没有注意到它。你可能知道我的意思......)
    【解决方案3】:

    这里有两个问题:

    1. 声明一个与全局变量同名的局部变量会使它们成为两个独立的实体,这可能会让人很困惑。这种现象被称为Variable shadowing

    2. 您正在比较 duration 变量,它实际上是 sec 类的 对象不是整数或浮点数。因此,要比较 duration 中的值,只需执行 duration.count() 以使其与整数或浮点值相当。

    在这里,在您的代码中,全局 duration 变量永远不会更改,因为您在 while 循环中声明了一个新变量。在while 条件中检查的duration 变量是全局duration 变量,但您希望它是循环内的持续时间变量。

    有两种可能的解决方案:

    1. 要么使两个duration 变量相同。

    2. 或者当duration变量保持值&gt;= 5时打破while(1)循环,

    解决方案 1 代码如下所示:

    #include <iostream>
    #include <Windows.h>
    #include <chrono>
    
    using namespace std;
    
    int main()
    {
    
        using clock = std::chrono::system_clock;
        using sec = std::chrono::duration <double>;
        sec duration;
        const auto before = clock::now();
        while (duration.count() < 5.0)
        {
            const auto after = clock::now();
            duration = after - before;
            std::cout << "It took " << duration.count() << "s" << std::endl;
        }
        std::cout << "After While Loop ";  //Never reaches this line!!!!!
        return 0;
    }
    

    解决方案 2 代码如下所示:

    #include <iostream>
    #include <Windows.h>
    #include <chrono>
    
    using namespace std;
    
    int main()
    {
    
        using clock = std::chrono::system_clock;
        using sec = std::chrono::duration <double>;
        const auto before = clock::now();
        while (1) // Soln 1: while(1)
        {
            const auto after = clock::now();
            const sec duration = after - before;
            std::cout << "It took " << duration.count() << "s" << std::endl;
            if(duration.count() >= 5) 
                break;
        }
        std::cout << "After While Loop ";  //Never reaches this line!!!!!
        return 0;
    }
    

    【讨论】:

    • 没有给你反对票,但想提一下,在你的代码中sec duration; 行将创建一个编译错误,因为sec 稍后定义。其次是duration 是一个对象,您正在尝试与浮点数/双精度数进行比较。
    • 你错过了我提到的第二个问题。 duration 在 while 循环中(如果在第二个解决方案中)应该是 duration.count()
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-20
    • 1970-01-01
    相关资源
    最近更新 更多