function to simulate sleep

This is a discussion on function to simulate sleep within the C++ Programming forums, part of the General Programming Boards category; I am working on a program that creates a Robot using a class. I am trying to create a function ...

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    11

    function to simulate sleep

    I am working on a program that creates a Robot using a class. I am trying to create a function that will allow the robot to simulate sleeping for n seconds. I am unsure how to create this function though. I want the function to make the program pause for n seconds and that is all. Also, I'd like to make a function that will allow the robot to do some work that I have predefined as one of five tasks and I know how long each task will take to complete. How can I make a function for that which would complete the task and then output that the robot has completed whatever task was assigned to it? Any help with these two functions would be wonderful.

  2. #2
    _B-L-U-E_ Betazep's Avatar
    Join Date
    Aug 2001
    Posts
    1,412
    int n=10;

    sleep(n);

    from <dos> using standard namespace
    Last edited by Betazep; 03-16-2002 at 05:04 PM.
    Blue

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    11
    Will that sleep(n) line cause any problems or is it included in one of the standard library header files?

  4. #4
    Registered User
    Join Date
    Aug 2001
    Posts
    58
    first off sleep(n) is Sleep(n) (notice the capitol S its necessary) also with the headers, you will need windows.h (nope no linux programming her ) anyway see my thread about text output maybe you can use that. also, the n is measured in miliseconds.
    --== www.NuclearWasteSite.com==--

  5. #5
    _B-L-U-E_ Betazep's Avatar
    Join Date
    Aug 2001
    Posts
    1,412
    >>>first off sleep(n) is Sleep(n) (notice the capitol S its necessary) also with the headers

    Nope... (only in windows progs)

    Code:
    # include <dos>
    using namespace std;
    
    int main()
    {
       sleep(10);
    
       return 0;
    }
    Compile that ...



    "you will need windows.h" ... why use a huge library for a simple sleep command????

    >>>Will that sleep(n) line cause any problems or is it included in one of the standard library header files?

    it is considered an obsolete function (then again, so is dos)... but it works , so who cares.

    >>>also, the n is measured in miliseconds.

    The dos version is in seconds...
    Last edited by Betazep; 03-16-2002 at 06:55 PM.
    Blue

  6. #6
    Registered User
    Join Date
    Mar 2002
    Posts
    11
    I can't seem to get that function to work correctly. My files will compile but when I run the program it just says "press any key to continue" instead of showing me any of my outputs from any of my functions in the program. What am I doing wrong? Here is the code exerpt:

    #include <dos.h>

    void CRobot::work()
    {
    if (task == DRILLING)
    sleep(DRILLTIME);
    cout <<robotName<<" has completed the task DRILLING\n";
    if (task == LIFTING)
    sleep(LIFTTIME);
    cout <<robotName<<" has completed the task LIFTING\n";
    if (task == CUTTING)
    sleep(CUTTIME);
    cout <<robotName<<" has completed the task CUTTING\n";
    if (task == ASSEMBLING)
    sleep(ASSEMBLETIME);
    cout <<robotName<<" has completed the task ASSEMBLING\n";
    if (task == DISPOSING)
    sleep(DISPOSETIME);
    cout <<robotName<<" has completed the task DISPOSING\n";
    }

    //causes robot to sleep for n seconds
    void CRobot::sleep(int n)
    {
    sleep(n);
    }

    These functions are part of a header file which creates the robot, and then I have a cpp file that calls the work function which should call the sleep function, but the functions don't seem to be doing anything. Please help me figure out what is going wrong.

  7. #7
    _B-L-U-E_ Betazep's Avatar
    Join Date
    Aug 2001
    Posts
    1,412
    void CRobot::sleep(int n)
    {
    sleep(n);
    }


    name your public member function something different... like CRobot::sleeping(int n)

    what compiler?

    what OS?

    do a compile test of the code I provided above. Verify proper operations with that... if that doesn't work with your compiler or OS... use Sleep(int_milliseconds) from <windows> using the standard namespace as my contemporary suggested...
    Last edited by Betazep; 03-16-2002 at 05:39 PM.
    Blue

  8. #8
    Registered User
    Join Date
    Mar 2002
    Posts
    11
    When I changed the name of the member function it now says "error C2065: 'sleep' : undeclared identifier" I am using Microsoft Visual C++ 6.0 and running WinXP Home

  9. #9
    _B-L-U-E_ Betazep's Avatar
    Join Date
    Aug 2001
    Posts
    1,412
    you would have to make the changes for every CRobot::sleep(int n)...

    try to avoid using common function names as your function names unless you explicitly do not include the files that contain said function names, AND you are using your own namespace.
    Blue

  10. #10
    _B-L-U-E_ Betazep's Avatar
    Join Date
    Aug 2001
    Posts
    1,412
    make a new console application...

    Code:
    # include <dos>
    # include <iostream>
    using namespace std;
    
    int main()
    {
       cout << "test";
       sleep(10);
       cout << "\ncomplete";
       return 0;
    }
    compile that and see if you get a 10 second pause... if it works, the problem is with your coding.
    Blue

  11. #11
    Registered User
    Join Date
    Mar 2002
    Posts
    11
    I made sure that all instances of the function sleep now say "sleeping" just to make it different. I still am getting the error. Does it make any difference that the sleeping function is a private method of the class CRobot? Also, I am confused about what you are saying about a namespace, what is that and what do I need to do with it? If it isn't obvious, I'm a beginner programmer so I don't understand a lot of these things very well yet.

  12. #12
    _B-L-U-E_ Betazep's Avatar
    Join Date
    Aug 2001
    Posts
    1,412
    did you change the occurance of sleep to sleeping in the CRobot class? You have to change it everywhere.

    In your declaration, in your implementation, and anywhere utilized...
    Blue

  13. #13
    Registered User
    Join Date
    Mar 2002
    Posts
    11
    I created a new file to test that function, I still get the error that sleep is an undeclared identifier. I can't include the dos header file just as <dos> it has to be <dos.h> or it won't find it, could that be causing the problem?

  14. #14
    _B-L-U-E_ Betazep's Avatar
    Join Date
    Aug 2001
    Posts
    1,412
    did you have

    #include <dos>
    using namespace std;

    ???
    Last edited by Betazep; 03-16-2002 at 05:56 PM.
    Blue

  15. #15
    Registered User
    Join Date
    Mar 2002
    Posts
    11
    Here is my exact code for this test file:

    # include <dos.h>
    # include <iostream>
    using namespace std;

    int main()
    {
    cout << "test";
    sleep(10);
    cout << "\ncomplete";
    return 0;
    }

    When I try to compile this, it says "error C2065: 'sleep' : undeclared identifier"

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Brand new to C need favor
    By dontknowc in forum C Programming
    Replies: 5
    Last Post: 09-21-2007, 11:08 AM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  3. Calling a Thread with a Function Pointer.
    By ScrollMaster in forum Windows Programming
    Replies: 6
    Last Post: 06-10-2006, 09:56 AM
  4. C++ compilation issues
    By Rupan in forum C++ Programming
    Replies: 1
    Last Post: 08-22-2005, 06:45 AM
  5. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21