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; Then MSVC doesn't have that function... try rkjd2's suggestion of using windows I am suprised that you cannot compile that. ...

  1. #16
    _B-L-U-E_ Betazep's Avatar
    Join Date
    Aug 2001
    Posts
    1,412
    Then MSVC doesn't have that function...

    try rkjd2's suggestion of using windows

    I am suprised that you cannot compile that. I have it in Borland. You would think that a Microsoft product would have it...

    Code:
    # include <windows>
    # include <iostream>
    
    using namespace std;
    
    int main()
    {
    
       cout << "test";
       Sleep (10000);  // ten seconds
       cout << "complete";
       return 0;
    }
    that will work... sorry to confuse you. I am having a slow brain day...
    Blue

  2. #17
    Evil Member
    Join Date
    Jan 2002
    Posts
    638
    I don't think sleep() type functions are standard.

    The name might be something else, you should open up your dos.h, or do some guess and check.

    my (djgpp) function is:
    delay(x) //Where x is milliseconds

    Others might be
    sleep()
    _sleep()
    alarm()
    wait()

    Also, you might be dealing with milliseconds or seconds, the switching of which could be quite annoying. Either use the semistandard Sleep from windows.h or use your compiler's, which you will need to flush out.

  3. #18
    Registered User
    Join Date
    Mar 2002
    Posts
    11
    OK, finally, that seems to be working. Now, do you think you could help me figure out why my work function if statement is not working correctly? Here is the code:

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

    //causes robot to sleep for n seconds
    void CRobot::sleeping(int n)
    {
    int j;
    j=1000*n;
    Sleep(j);
    }

    The work function is showing all 5 options instead of just the one that is supposed to be shown. When I call the function, the task should be "CUTTING" but when I compile and run the program, it displays all 5 statements that the robot has completed each task, instead of the one I need it to.

  4. #19
    Evil Member
    Join Date
    Jan 2002
    Posts
    638
    An if only binds to whatever immediately follows it, not all the way to the next if. Yoiu will need to put { } around the statement blocks to bind them all to the ifs.

    ie

    Code:
    if (task == DRILLING) {
    sleeping(DRILLTIME); 
    cout <<robotName<<" has completed the task DRILLING\n"; 
    }

  5. #20
    Registered User
    Join Date
    Aug 2001
    Posts
    58
    who would want Sleep to work in seconds? milliseconds are better to work with you can do very short delays easyer. Also, windows.h is pretty common and although big, dosn't take too much longer to load (of corse unless your running off a old 486) btw did u guys hear that mac's are claming that their 800mhz is better than the Processers found in IBM's? am i crazy or are the new 2.2ghz processers about 4 times the speed of the G4's? buying mac is like buying a big car with a small engine and no capacity for upgrade.
    --== www.NuclearWasteSite.com==--

Page 2 of 2 FirstFirst 12
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