Thread: Timer help

  1. #1
    Blizzarddog
    Guest

    Question Timer help

    Help! I jut started learning CPP and i know how to use the STD::STRING and all before, well almost, but i heard a command called delayed(1) to make the computer wait for one second before continuing. I know it is c programming, so it should work in c++ right? I keep getting 'delayed' undeclared.
    email me at [email protected]

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    For Windows machines, you can include windows.h and use Sleep(unsigned long duration). For console apps,you can make your own using the clock() function from time.h.
    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;
    }

  3. #3
    Unleashed
    Join Date
    Sep 2001
    Posts
    1,765
    For milisecond pauses, on a windows machine, use Sleep.
    Code:
    #include <stdio.h>
    #include <windows.h>
    
    int main ( void )
    {
    	printf("Pausing for 250 miliseconds..\n");
    	Sleep(250);
    	return 0;
    }
    For second pausing, use this..
    Code:
    #include <time.h>
    
    void Wait ( int Seconds );
    
    void Wait ( int Seconds ) 
    {
        clock_t endtime = clock() + Seconds * CLOCKS_PER_SEC;
        while ( ( clock() < endtime ) );
    }
    The world is waiting. I must leave you now.

  4. #4
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    There are several solutions. The simpliest solution for 32-bit console program running one process is sleep() as mentioned above.

    sleep() parameter is millisecond.

    Kuphryn

  5. #5
    Linux Forever
    Join Date
    Oct 2002
    Posts
    373

    Angry


    Okay, none of those work, the sleep has no errors, but it doesn't 'sleep'. The other i get 15 errors!
    This war, like the next war, is a war to end war.

  6. #6
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    What library files do you include in the program?

    Kuphryn

  7. #7
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by Blizzarddog

    Okay, none of those work, the sleep has no errors, but it doesn't 'sleep'. The other i get 15 errors!
    Shadow's windows code with the Sleep() function only sleeps for 250 milliseconds, thats a quarter of a second. To see it sleep for say 5 seconds, use Sleep(5000);
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  8. #8
    Unleashed
    Join Date
    Sep 2001
    Posts
    1,765
    To add to what Hammer said, as far as the raw second pausing goes, that was not a complete program.

    The first line, is the header you need to include.
    The second, is the function prototype.
    The third, is the actual function.

    To use it properly, do like so:
    Code:
    #include <time.h>
    
    void Wait ( int Seconds );
    
    int main ( void )
    {
        printf("Pausing for 3 seconds..\n");
        Wait(3);
        return 0;
    }
    
    void Wait ( int Seconds ) 
    {
        clock_t endtime = clock() + Seconds * CLOCKS_PER_SEC;
        while ( ( clock() < endtime ) );
    }
    The world is waiting. I must leave you now.

  9. #9
    Linux Forever
    Join Date
    Oct 2002
    Posts
    373

    the code

    This is my code:


    #include <iostream>
    #include <string>
    #include <cstdlib>
    #include <stdlib.h>
    #include <windows.h>
    #include <stdio.h>
    #include <windows.h>
    #include <time.h>
    using namespace std;

    void Wait ( int Seconds );

    int main ( void )

    {
    string a("Norton anti virus scan results:\n");
    string b("\n");
    string c("54 Hits\n");
    string d("Viruses found: 4\n");
    string e("Code red Worm\n");
    string f("Nimda Virus\n");
    string g("Love Bug Virus\n");
    string h("Kevlarsmart Worm\n");
    string j("Trojans: 50\n");
    string k("");
    cout << a;
    wait (5)
    cout << k;
    wait(2) cout << b;
    cout << k;
    cout << c;
    cout << k;
    cout << d;
    cout << k;
    cout << e;
    cout << f;
    cout << g;
    cout << h;
    cout << "No files fixed\n";
    cout << j;
    cout << "No files fixed\n";
    system("PAUSE");
    return 0;
    }


    The code is just a "play" program to use strings and timer.
    again, its written in c++
    hey, gimme a break, it sux because im a teen!
    This war, like the next war, is a war to end war.

  10. #10
    Unleashed
    Join Date
    Sep 2001
    Posts
    1,765
    This is what you want
    Code:
    #include <iostream>
    #include <string>
    #include <cstdlib>
    #include <stdlib.h>
    #include <windows.h>
    #include <stdio.h>
    #include <windows.h>
    #include <time.h>
    using namespace std;
    
    int Wait ( int Seconds );
    
    int main ( void )
    {
    	string a("Norton anti virus scan results:\n");
    	string b("\n");
    	string c("54 Hits\n");
    	string d("Viruses found: 4\n");
    	string e("Code red Worm\n");	
    	string f("Nimda Virus\n");
    	string g("Love Bug Virus\n");
    	string h("Kevlarsmart Worm\n");
    	string j("Trojans: 50\n");
    	string k("");
    	cout << a;
    	Wait (5);
    	cout << k;
    	Wait(2);
    	cout << b;
    	cout << k;
    	cout << c;
    	cout << k;
    	cout << d;
    	cout << k;
    	cout << e;
    	cout << f;
    	cout << g;
    	cout << h;
    	cout << "No files fixed\n";
    	cout << j;
    	cout << "No files fixed\n";
    	system("PAUSE");
    	return 0;
    }
    
    int Wait ( int Seconds ) 
    {
        clock_t endtime = clock() + Seconds * CLOCKS_PER_SEC;
        while ( ( clock() < endtime ) );
        return 0;
    }
    Oh, and please use code tags.
    The world is waiting. I must leave you now.

  11. #11
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >hey, gimme a break, it sux because im a teen!
    That's no excuse, we have teenage members who write very nice code. I myself am only barely out of my teens.

    Since you have windows.h you might as well make use of Sleep(), just remember that it takes milliseconds, not full seconds as an argument:
    Code:
    #include <iostream>
    #include <string>
    #include <windows.h>
    using namespace std;
    
    void wait ( int mseconds )
    {
      Sleep ( mseconds );
    }
    
    int main ( void )
    {
      string a ( "Norton anti virus scan results:\n" );
      string b ( "\n" );
      string c ( "54 Hits\n" );
      string d ( "Viruses found: 4\n" );
      string e ( "Code red Worm\n" );
      string f ( "Nimda Virus\n" );
      string g ( "Love Bug Virus\n" );
      string h ( "Kevlarsmart Worm\n" );
      string j ( "Trojans: 50\n" );
    
      cout << a;
      wait ( 5000 );
      cout << b;
      wait ( 2000 );
      cout<< c << d << e << f << g << h;
      cout << "No files fixed\n";
      cout << j;
      cout << "No files fixed\n";
    
      system ( "pause" );
    
      return 0;
    }
    -Prelude
    My best code is written with the delete key.

  12. #12
    Linux Forever
    Join Date
    Oct 2002
    Posts
    373

    Talking

    Well, i mean:
    Im only a teen that started CPP about 5 weeks ago!

  13. #13
    Unleashed
    Join Date
    Sep 2001
    Posts
    1,765
    > Im only a teen that started CPP about 5 weeks ago!
    90% of this community/message board cannot drive yet, or just barely is able to drive. They all right great code.

    Point being, anybody who is 20+ is the minority here..
    The world is waiting. I must leave you now.

  14. #14
    Linux Forever
    Join Date
    Oct 2002
    Posts
    373

    Talking

    ITS WORKING!
    This war, like the next war, is a war to end war.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. SIGALRM and timer
    By nkhambal in forum C Programming
    Replies: 1
    Last Post: 06-30-2008, 12:23 AM
  2. tic tac toe crashes :(
    By stien in forum Game Programming
    Replies: 4
    Last Post: 05-13-2007, 06:25 PM
  3. Need help with a count down timer
    By GUIPenguin in forum C# Programming
    Replies: 0
    Last Post: 07-07-2006, 04:18 PM
  4. Timer again.
    By geek@02 in forum Windows Programming
    Replies: 1
    Last Post: 05-04-2005, 10:19 PM