_sleep() 在 C++ 中不起作用

_sleep() is not working in C++

我试图编写简单的代码,但每当我调用函数 _sleep() 时它都不起作用。我现在已经在两个不同的项目中尝试过它,每次我使用它时,它都会给我一个错误 says:1

'_sleep': This function or variable has been superseded by newer library or operating system functionality. Consider using Sleep instead. See online help for details.

我尝试了很多其他东西,例如 Sleep()sleep() 以及其他一些随机垃圾,但最终都没有用。如果有另一个命令会暂停控制台一段时间,我可以改变我想要的方式,并且不会弹出一些丑陋的东西,比如 "Press any key to continue...",或者修复我的命令,请让我知道了!

代码

#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

int rouletteGen();

int main()
{
    //--------
    //| Idea |
    //--------
    //cout << box 1 << box 2 << endl;

    int wheel, a, b, c, time, cA, cB, cC;

roulette:
    while (a >= 1)
    {
        cout << "          _|_" << endl;
        cout << "          \|/" << endl;
        cout << "_______ _______ _______" << endl;
        cout << "|     | |     | |     |" << endl;
        cout << "|  "<< cA <<"  | |  "<< cB<<"  | |  "<< cC<<"   |" << endl;
        cout << "|     | |     | |     |" << endl;
        cout << "_______ _______ _______" << endl;

        _sleep(250);
        while (b >= 1)
        {
            cout << "_|_" << endl;
            cout << "\|/" << endl;
            _sleep(250);
            while (c >= 15)
            {

            }
        }
    }
}

int rouletteGen()
 {
    int dice;
    srand(time(NULL));
    dice = rand() % 3;

    dice = dice + 1;

    return dice;
}

要使用 Sleep,您需要 #include <windows.h>。在我看来,更好的解决方案是取消该警告。 _sleep 工作正常。

只要您可以访问 C++11 或更高版本,我就会使用 std::this_thread::sleep_for 并让标准库担心调用正确的 "sleep" 函数。

Sleep() 函数在各种 OS 中是不同的,因为它是由 OS 库实现的。 如果你使用 windows 最好的方法是 #include windows.h 头文件, 然后在你想使用睡眠功能的地方使用它作为 Sleep(time_in_ms).

#include <iostream>
#include <windows.h>
int main(){
std::cout << "A" << std::endl;
Sleep(3000);
std::cout << "B" << std::endl;
return 0;
}