Thread: Need help

  1. #1
    Registered User
    Join Date
    Jan 2004
    Posts
    25

    Need help

    In a class, I'm trying to get a member method to do all the work in it to produce a sleeping/waking time by making it about 20 seconds. Apparently, I'm getting an infinate loop. Any help is appreciated.

    Code:
    #include <iostream>
    #include <ctime>
    
    // Other Methods
    Sleep( int seconds ) const 
    {
         std::cout << "You begin to sleep..." << std::endl;
         clock_t EndSleeping;
    					
         EndSleeping = clock () + seconds * CLK_TCK ;
         while (clock() < EndSleeping) {}
         int n;
         for (n=18; n>0; n--)
         {
                 Sleep(1);
         }
    
         std::cout << "You wake up feeling refreshed." << std::endl;
    }
    
    int main()
    {
        using namespace std;
        Player.Sleep(1);
    My work:
    http://phil.webula.net

  2. #2
    Veni Vidi Vice
    Join Date
    Aug 2001
    Posts
    343
    If iīm not misstaken there is Sleep() function in windows.h that would work here.
    Code:
    #include <windows.h>
    ...
    Sleep(2000); //pause for 2 seconds
    ...
    01000111011011110110111101100100 011101000110100001101001011011100110011101110011 01100100011011110110111001110100 01100011011011110110110101100101 01100101011000010111100101110011 0110100101101110 01101100011010010110011001100101
    Good things donīt come easy in life!!!

  3. #3
    Banned nickname_changed's Avatar
    Join Date
    Feb 2003
    Location
    Australia
    Posts
    986

    Re: Need help

    Your problem lies in that you are calling Sleep(1) from Sleep(1) (in the for loop... thereby creating an endless loop because Sleep(1) gets called each time the function is called. You should take a harder look at your programming logic here my friend.

    Or, heres on I did because I', a lonely person waiting for my girlfriend to ring:
    Code:
    #include <windows.h>
    ....
    MySleep (int Seconds) const // Notice its renamed, so it doesn't confuse 
                                         // itself with the Windows Sleep() function 
    {
        cout << "You begin to sleep..." << endl;
        Sleep(Seconds * 1000);
        cout << "You wake up again! Get your hand off that cock... ";
    }
    The windows Sleep() function takes the argument in microseconds, so you must multiply the number of seconds you want by 1000.
    Last edited by nickname_changed; 01-21-2004 at 06:17 AM.

  4. #4
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    The windows Sleep() function takes the argument in microseconds...
    You mean milliseconds.
    ...so you must multiply the number of seconds you want by 1000.
    Right!

    milli = 1/1,000

    micro = 1/1,000,000

    nano = 1/1,000,000,000

  5. #5
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Code:
    Sleep( int seconds ) const 
    {
         std::cout << "You begin to sleep..." << std::endl;
         clock_t EndSleeping;
    					
         EndSleeping = clock () + seconds * CLK_TCK ;
         while (clock() < EndSleeping) {}
         int n;
         for (n=18; n>0; n--)
         {
                 Sleep(1);
         }
    
         std::cout << "You wake up feeling refreshed." << std::endl;
    }



    - Your function is missing a return type.
    - Time here is in clock_t, it should the type parameter of your sleep function, too.
    - Pick one: a clock() loop or a single call to std::Sleep(), they're mutually exclusive because they perform the same task (of slowing the interface).
    - Your 'n' loop doesn't fit either. If you're going to add a 'tweak' to it, make it a parameter to the function (these kinds of hidden 'features' often are the cause of myterious bugs, strange output, etc.).
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  6. #6
    Registered User
    Join Date
    Jan 2004
    Posts
    25
    Thanks, Ripper, I tried your thoughts and it worked greatly. Plus it makes more sense than whatever I had at first.

    Final code:

    Code:
    // Other Methods
    void Sleeping() const 
         {
                std::cout << "You begin to sleep..." << std::endl;
                Sleep(20000);    // 20 seconds
                std::cout << "You wake up." << std::endl;
         }
    My work:
    http://phil.webula.net

Popular pages Recent additions subscribe to a feed