Brakes in execution

This is a discussion on Brakes in execution within the C++ Programming forums, part of the General Programming Boards category; Well, I'm trying to find a way to make my program take brakes while executing. For example it outputs a ...

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    16

    Brakes in execution

    Well, I'm trying to find a way to make my program take brakes while executing. For example it outputs a number then waits 10 seconds and outputs a new number and keeps on doing this.
    Is there a way to achieve this. I'd appreciate your help.

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    One method from the C++ faq on this website:
    Code:
    #include <ctime> 
    ...
    ...
    long delay = clock() + 10000; //10 secs x 1000 milliseconds/sec
    while( clock() < delay ) 
         continue;
    Last edited by 7stud; 06-06-2005 at 03:38 AM.

  3. #3
    Super Moderater.
    Join Date
    Jan 2005
    Posts
    374
    If you have <windows.h>

    Then you should be able to do this

    Code:
    #include<windows.h>
    #include<iostream>
    
    using namespace std;
    
    int main()
    {
      cout<<5<<endl;
      Sleep(2000);// in milliseconds, wait 2 second
      cout<<8;
      
        int pause;
        cin>>pause;  
    
    }

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    16
    I don't know if I have windows.h. How can I check that? I'll try both ways.
    Last edited by cnoob; 06-06-2005 at 03:57 AM.

  5. #5
    ---
    Join Date
    May 2004
    Posts
    1,379
    What OS/compiler are you using

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    16
    Bloodshed

  7. #7
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    You're still neglecting most of sand_man's question. If you're using Dev-C++ under windows, you should have windows.h. Why don't you just try the code and check? If you don't have windows.h you're just going to get an error message.

  8. #8
    Shibby willc0de4food's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    378
    you would have windows.h unless you manually deleted it, but since you dont even know you have it then i'm doubting it was deleted.
    Registered Linux User #380033. Be counted: http://counter.li.org

  9. #9
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    cnoob,

    Here are the basic tradeoffs:

    7stud's solution is standard-portable C++. But, with a multitasking system it hogs CPU cycles while looping like crazy.

    treenef's solution frees-up the CPU to do other stuff during your program's sleep time. But, is not standard or portable (It's Windows only).

    It's OK to use non-standard code, but you should know that your code is non-standard, and you should have a good reason for using it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Execution Time - Rijandael encryption
    By gamer4life687 in forum C++ Programming
    Replies: 5
    Last Post: 09-20-2008, 10:25 PM
  2. Replies: 0
    Last Post: 10-06-2007, 02:25 PM
  3. Arbitrary Code Execution
    By CrazyNorman in forum C++ Programming
    Replies: 3
    Last Post: 04-10-2007, 03:33 PM
  4. What is the best way to record a process execution time?
    By hanash in forum Linux Programming
    Replies: 7
    Last Post: 03-15-2006, 07:17 AM
  5. Parallel execution
    By Panopticon in forum C++ Programming
    Replies: 2
    Last Post: 01-27-2003, 01:57 AM

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