_sleep() is not working in C++

眉间皱痕 提交于 2019-12-11 18:08:36

问题


I was trying to write simple code, but whenever I call the function _sleep() it doesn't work. I have tried it in two different projects now, and every time I use it, it gives me an error that 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.

I tried so much other stuff like Sleep(), sleep() and just a bunch of other random junk that didn't end up working. 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!

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

回答1:


The Sleep() function is different in various OS as it is being implemented by the OS libraries. 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).

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



回答2:


As long as you have access to C++11 or higher I would use std::this_thread::sleep_for and let the standard library worry about calling the correct "sleep" function.




回答3:


To use Sleep you need to #include <windows.h>. The better solution in my view is to suppress that warning. _sleep works just fine.



来源:https://stackoverflow.com/questions/34948371/sleep-is-not-working-in-c

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!