为什么说“sleep(1)”未定义?

问题描述 投票:0回答:1

here's the error i got

#ifdef _WIN32
#include <windows.h>
#endif
#include <iostream>
#include <cstdlib>
using namespace std;

int main()
{
    srand((unsigned)time(NULL));
    int random = 1 + rand() % 99;
    int user_input;
    int attempts = 1;

    cout << "random = " << random << endl << endl;
    /* delet this line if you dont want spoilers ^^^ */
    do
    {
        cout << "----------| ";
        cout << attempts << " |----------" << "\n";
        attempts++;
        cout << "guess a random number 1 - 100" << endl;
        cin >> user_input;
        cout << endl;

        if (user_input > random || user_input < random)
        {
            cout << "worng try again" << endl;
        }
    } while (user_input < random || user_input > random);

    cout << "you did it!" << endl;
    cout.flush();
    sleep(1);
    cout << "your prize is";
    cout.flush();
    sleep(1);
    cout << ".";
    cout.flush();
    sleep(1);
    cout << ".";
    cout.flush();
    sleep(1);
    cout << "." << endl;
    cout.flush();
    sleep(1);
    cout << "Nothing!";

    return 0;
}

我在学校开发一个随机数猜测器,并且使用在线 C++ 编译器,它运行得很好。但是,当我将其移至 Visual Studio 时,它说“sleep(1)”未定义,我不知道如何修复它。

我尝试将 _win34 更改为 _win32,但这不起作用,而且我找不到 unistd.h 的任何替代品。当我运行代码时,我得到的都是相同的错误

c++ sleep unistd.h
1个回答
0
投票

MS UCRT 具有

_sleep()
的 POSIX 兼容性,但它自 2015 年以来已被弃用,转而支持 Windows API
Sleep()
功能。

参见 睡眠函数 (synchapi.h)。

© www.soinside.com 2019 - 2024. All rights reserved.