Beginner: Cannot resolve Sleep()

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • EhYay
    New Member
    • Oct 2022
    • 6

    Beginner: Cannot resolve Sleep()

    I've been looking around the web about how to make the program wait for a set amount of time and discovered this type of code. However, my IDE says it cannot resolve Sleep(). I have included the appropriate library and even tried its variations. I'm confused about what's wrong and would much appreciate help.
    Code:
    #include <windows.h>
    #include <iostream>
    
    using namespace std;
    
    int main() {
    	sleep();
    }
    I use the Eclipse IDE patch 4.24
    My compiler is MinGW, downloaded from sourceforge.net and said to be last updated 2021-09-05.
    I am on Windows 10 Pro.
    OS build 19044.1348

    I cycled between using the libraries: <windows.h> and <unistd.h>. I found no results.

    Edit 1: While continuing experimenting with C++, I've discovered that other symbols such as cout and string have encountered the same problem as with Sleep(); the symbols cannot be resolved.
    This was a non-issue before. cout and string were both functional when last used (to elaborate, I'd been using cout since I started coding; I used string and Sleep() together while I was experimenting but Sleep() could not be resolved by the IDE during said experiment and up to this day).
    Last edited by EhYay; Oct 15 '22, 08:05 AM. Reason: To supplement additional information about the OS, IDE and compiler and to post new findings while testing C++.
  • dev7060
    Recognized Expert Contributor
    • Mar 2017
    • 655

    #2
    I've been looking around the web about how to make the program wait for a set amount of time and discovered this type of code. However, my IDE says it cannot resolve Sleep(). I have included the appropriate library and even tried its variations. I'm confused about what's wrong and would much appreciate help.

    ------
    #include <windows.h>
    #include <iostream>

    using namespace std;

    int main() {
    sleep();
    }
    [b]------[\B]

    I use the Eclipse IDE.
    My compiler is MinGW

    I cycled between using the libraries: <windows.h> and <unistd.h>. I found no results.
    Did you try Sleep(time)?

    Comment

    • EhYay
      New Member
      • Oct 2022
      • 6

      #3
      Yes, I have. e.g.
      Code:
      Sleep(1000);
      .
      Last edited by EhYay; Oct 15 '22, 08:06 AM. Reason: I forgot to put in the semi-colon.

      Comment

      • zmbd
        Recognized Expert Moderator Expert
        • Mar 2012
        • 5501

        #4
        Sounds as if your headers are missing or damaged

        Open a new project and then try compiling and running the following code as written:
        Code:
        // Bytes977919_CannotResolveSleep.cpp 
        //
        #include <iostream>
        #include <Windows.h>
        
        int main()
        {
            std::cout << "Hello World!\n";
            printf("starting to sleep...\n");
            Sleep(5000);   // sleep five seconds 
            printf("sleep ended\n");
        }
        This compiles and runs in VisualStudio201 9

        + Take a look in the "include" directory and verify that both windows.h AND iostream are available...
        You may need to use "search:" against the install directory for your IDE - if found, then make sure there's a copy in the correct location for your IDE to find them.

        I'm not familiar with your IDE; however, even at that, these would be the first things I would check for any IDE/compiler.

        Something else to keep in mind:
        Why using namespace std; is considered bad practice

        (Might include your entire code... if it's not too long... maybe there's something like an extra reserved symbol - been known to avoid the author's eyes and be a red-flag-to-the-bull for another...
        Please be sure to enclose the code by selecting it and then clicking on the [CODE/] formatting in the reply ribbon)
        Last edited by zmbd; Oct 16 '22, 02:17 AM.

        Comment

        • dev7060
          Recognized Expert Contributor
          • Mar 2017
          • 655

          #5
          Yes, I have. e.g.
          Code:
          Sleep(1000);
          Seems correct for the windows environment. I see no reason why one should introduce any change.

          Eclipse is not my primary IDE. I installed it and tested the code, everything works fine. The compiler is available in the path, hence eclipse probably auto-detected it plus the include directories during the initial project setup.



          You might need to manually add them: C/C++ Build > Settings > Tool Settings > GCC C++ Compiler > Includes

          While continuing experimenting with C++, I've discovered that other symbols such as cout and string have encountered the same problem as with Sleep(); the symbols cannot be resolved.
          Probably not a compiler error. Are you reporting the hints/suggestions popups present with the underlined texts in the editor? What do you see in the console? Share the exact dump from the console. Did you try manually compiling using the g++ through a terminal? What does it say?

          Use Ctrl + B to generate the build and see if there's an exe build in the directory to run.

          You may want to share the exact steps being taken to set up the project for the other party to reproduce the same.
          Attached Files

          Comment

          • bhuttehai
            New Member
            • Oct 2022
            • 2

            #6
            I cycled between using the libraries: <windows.h> and <unistd.h>.
            teatv download
            Get LIVE Stock Price & Share Price information of all the companies listed on BSE and NSE.

            Comment

            • dev7060
              Recognized Expert Contributor
              • Mar 2017
              • 655

              #7
              I cycled between using the libraries: <windows.h> and <unistd.h>.
              As OP mentioned Windows and unistd.h is a standard header for Unix-like systems, let me ask why and how?

              Comment

              • EhYay
                New Member
                • Oct 2022
                • 6

                #8
                To: dev7060

                The include directories are already present in the project folder.
                Other than that, something strange happened. cout and string, cout is no longer an issue and issue for some reason while string remains one. However, now Sleep() is functioning while cout is not.
                This horribly confused me until I decided to close then re-open my IDE which resulted in both those aformentioned codes
                Code:
                // Bytes977919_CannotResolveSleep.cpp
                //
                #include <iostream>
                #include <Windows.h>
                
                int main()
                {
                    std::cout << "Hello World!\n";
                    printf("starting to sleep...\n");
                    Sleep(5000);   // sleep five seconds
                    printf("sleep ended\n");
                }
                I have feeling there's a bug with my IDE.

                P.S. As for the console logs, I don't know how to do it and the methods I'm employing, discovered via browsing, are ineffective.

                Comment

                • EhYay
                  New Member
                  • Oct 2022
                  • 6

                  #9
                  And when I meant cycled, I meant swapping the libraries between each run to see if there is any change.

                  Comment

                  • dev7060
                    Recognized Expert Contributor
                    • Mar 2017
                    • 655

                    #10
                    To: dev7060

                    The include directories are already present in the project folder.
                    Other than that, something strange happened. cout and string, cout is no longer an issue and issue for some reason while string remains one. However, now Sleep() is functioning while cout is not.
                    This horribly confused me until I decided to close then re-open my IDE which resulted in both those aformentioned codes
                    Code:
                    // Bytes977919_CannotResolveSleep.cpp
                    //
                    #include <iostream>
                    #include <Windows.h>
                     
                    int main()
                    {
                        std::cout << "Hello World!\n";
                        printf("starting to sleep...\n");
                        Sleep(5000);   // sleep five seconds
                        printf("sleep ended\n");
                    }
                    I have feeling there's a bug with my IDE.

                    P.S. As for the console logs, I don't know how to do it and the methods I'm employing, discovered via browsing, are ineffective.
                    And when I meant cycled, I meant swapping the libraries between each run to see if there is any change.
                    I think you ignored the questions and rephrased your previous posts about what is and isn't working. It kind of sounds like a perspective point of view since you have provided no technical context/language from the IDE/compiler or anything.

                    Comment

                    • EhYay
                      New Member
                      • Oct 2022
                      • 6

                      #11
                      Sorry. I'm new to all of this, both as a site member and as a coder. I'll try to focus on answering questions and providing better data.

                      Comment

                      Working...