如何设置时间以差 1 秒输出数字

How to set time to output numbers by difference 1 second

本文关键字:输出 数字 时间 何设置 设置      更新时间:2023-10-16

我有一个这样的程序:

using namespace std;
int main()
{
    srand(time());
    int izlases_sk, a, b, c, d, e;
    cout << "Enter 5 numbers" << endl;
    cin >> a;
    cin >> b;
    cin >> c;
    cin >> d;
    cin >> e;
    cout << endl;
    for (int x = 0; x < 20; x++)
    {
        izlases_sk = rand() % 77;
        cout << izlases_sk << "t";
        if( izlases_sk == a || izlases_sk == b || izlases_sk == c || izlases_sk == d || izlases_sk == e)
            cout << " You predicted this number!t";
        else
            cout << "Not";
        {
            if (a > 77 || b > 77 || c > 77 || d > 77 || e > 77)
                goto end;
        }
        if ((x + 1) % 5 == 0)
            cout<<endl;
    }
    end:
    getch ();
}

我希望我的随机生成的数字一一输出,中间有 1 秒的延迟。我该怎么做?

示例:c++生成76,然后在一秒钟后生成下一个数字。

使用 Sleep 函数(Windows 和 Linux 版本)

如果您

使用的是符合 C++11 的编译器,那么只需在您希望程序休眠一秒钟的任何位置插入以下行(您需要标头 <thread><chrono>):

std::this_thread::sleep_for(std::chrono::seconds(1));

此外,此代码是可移植的。