简体   繁体   English

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

[英]_sleep() is not working in C++

I was trying to write simple code, but whenever I call the function _sleep() it doesn't work. 我试图编写简单的代码,但是每当我调用函数_sleep()它都无效。 I have tried it in two different projects now, and every time I use it, it gives me an error that says:1 我现在已经在两个不同的项目中尝试过它,每次使用它时,都会出现一个错误:1

'_sleep': This function or variable has been superseded by newer library or operating system functionality. '_sleep':此函数或变量已被较新的库或操作系统功能所取代。 Consider using Sleep instead. 考虑改用睡眠。 See online help for details. 详细信息请参见在线帮助。

I tried so much other stuff like Sleep() , sleep() and just a bunch of other random junk that didn't end up working. 我尝试了很多其他东西,例如Sleep()sleep()以及其他一堆不能正常工作的随机垃圾。 If there another command that will pause the console for a certain time, that I can change how I want, and that doesn't pop up with some ugly thing like "Press any key to continue..." , or a fix to my command, please let me know! 如果有另一个命令会使控制台暂停一定时间,那么我可以更改所需的方式,并且不会出现诸如"Press any key to continue..."类的丑陋内容,或者对我的修复命令,请让我知道!

Code

#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;
}

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

The Sleep() function is different in various OS as it is being implemented by the OS libraries. 由于操作系统库正在实现Sleep()函数,因此在各种操作系统中它都不同。 If you are using windows the best way is #include the windows.h header file, then where you want to use sleep function use it as Sleep(time_in_ms) . 如果您使用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;
}

To use Sleep you need to #include <windows.h> . 要使用Sleep您需要#include <windows.h> The better solution in my view is to suppress that warning. 我认为更好的解决方案是禁止该警告。 _sleep works just fine. _sleep工作正常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM