每秒清除终端但留下分钟

时间:2017-07-28 00:04:25

标签: c++ if-statement while-loop clock

我有一个秒钟和分钟计数器,非常类似于我的计时器。但是,我无法获得留在屏幕上的分钟数。

int main()
{
    int spam = 0;
    int minute = 0;

    while (spam != -1)
    {
        spam++;
        std::cout << spam << " seconds" << std::endl;
        Sleep(200);
        system("CLS");
        //I still want the system to clear the seconds
        if ((spam % 60) == 0)
        {
            minute++;
            std::cout << minute << " minutes" << std::endl;
        }
        //but not the minutes
    }
}

1 个答案:

答案 0 :(得分:1)

system("CLS")将清除屏幕,您在while循环的每次迭代中都会这样做,而您只需每隔分钟打印minute 。< / p>

您需要在每次迭代时打印分钟:

while (spam != -1)
{
    spam++;
    if (minute)
        std::cout << minute << " minutes" << std::endl;
    std::cout << spam << " seconds" << std::endl;
    Sleep(200);
    system("CLS");
    if ((spam % 60) == 0)
    {
        minute++;
    }
}

这里我假设你只想打印分钟,如果不是零,那么if (minute)

FWIW:您可能希望在更新spam时将0重置为minute,但这取决于您正在做什么。也许您只想显示总共经过的的数量。

相关问题