Thread: Pauseing a program for 3 seconds

  1. #1
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342

    Pauseing a program for 3 seconds

    Hello, I am new to C++, this is only my third post on this forum.

    Also, does anyone know how to make a program pause for 3 seconds, and then continue on like normal?
    The only way I know of doing it is by using the
    Code:
    system("cls");
    command. And I want to avoid the system command all togeather.

    Thanks in Advance, August

    P.S. My compiler doesn't have <windows.h>

  2. #2
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  3. #3
    Registered User
    Join Date
    Sep 2004
    Posts
    36
    Try this

    Code:
    for(long i= 0; i<100000000000000; i++);
    Put this where u want to pause the thing.

    This makes 100,000,000,000,000 iterations where "i" goes from 0 to that really huge number. Since there is no body for the loop, the loop does nothing for that many times. Try experimenting with the really big number. Make it more, or make it less to suit ur needs.

    if u want to pause for exactly 3 sec, I recommend using the time.h library mentioned by the previous poster instead of the above.

    But if u just wanna pause for some time, then do my way.
    Last edited by Sridar; 04-08-2005 at 05:56 PM.

  4. #4
    Slave MadCow257's Avatar
    Join Date
    Jan 2005
    Posts
    735
    I hate to break it to you sridar, but that loop really is not a suitable option for pausing.

  5. #5
    Registered User
    Join Date
    Sep 2004
    Posts
    36
    I tried it MadCow257 and it worked for me.
    I used BloodShed Dev compiler to make a program in which the text runs from the left to right of the screen in 1 second intervals

    Ex:

    "Hello World"
    "Hello World"
    "Hello World"
    "Hello World"
    "Hello World"
    "Hello World"
    "Hello World"
    "Hello World"
    .......



    The code i mentioned b4 printed to the screen, the i used gotoxy() and clrscr() to make it so that the text is "moving" Every second it refreshed for me.

    Here is the code:

    Code:
    /*
      Name: Scroller
      Copyright: 2005
      Author: Sridar Ravi
      Date: 05/04/05 18:31
      Description: A scrolling text program
    */
    
    
    #include <iostream>
    #include<conio.h>
    
    using namespace std;
    //*****************************************************************************
    void Scroll(string &msg)
    {
           string scrolled,temp,temp2;
           
           temp=msg.substr(1,(msg.length()));
           temp2=msg.substr(0,1);
           
           msg=temp+temp2;
           if(msg.length()+1==' ')
           {
                                cout<<temp+' '+temp2;
                                }
           else
           {
           
           cout<<msg;
           }
           }
    //*****************************************************************************       
    int main()
    {
        string msg;
        cout<<"Enter the message that you wish to display as scroller: ";
        getline(cin,msg);
        msg=msg+".      ";
        int x=0;
        
        while(x<100000)
        {
                   Scroll(msg);
                   for(long y=0; y<30000000; y++);
                   system("cls");
                   
                   x++;    
                   }
        cin.get();
        return 0;
    }

  6. #6
    Slave MadCow257's Avatar
    Join Date
    Jan 2005
    Posts
    735
    I tried it MadCow257 and it worked for me.
    I know it works, but I was just saying that there are better ways. You might want to read the FAQ for some examples.

  7. #7
    Registered User
    Join Date
    Sep 2004
    Posts
    36
    I was just mentioning an easier way to do it. I honestly believe that the faqs are the way to go, but they often require u to write more code.

    The op can choose whichever he wishes.

  8. #8
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >I was just mentioning an easier way to do it.
    Easier, perhaps. But far from optimal. Busy wait loops are generally considered anti-social, and using a loop with an arbitrary value is silly because the loop will have different times for just about every different processor it's run with. A better solution is with the <ctime> functions and types, but that still doesn't help the fact that it's still a busy wait loop. Sadly, there's no good portable solution, so it's better to go with a system-dependent solution that actually does the job right.

    >for(long i= 0; i<100000000000000; i++);
    This is undefined behavior unless long on your system can support values that large. On my system, long is 32 bits, and signed integral overflow is undefined if I'm to believe the C++ standard.
    My best code is written with the delete key.

  9. #9
    Flash Animator, OF DOOOOM bluehead's Avatar
    Join Date
    Nov 2001
    Posts
    269
    ok. you guy's make everything complicated.

    the easiest way is this. (if you want it PAUSED for 3 seconds)

    Code:
    //headers
    #include <windows.h>
    
    //code
    Sleep(3000); // in milliseconds
    make sure you use capital S on sleep
    Code:
    #if _emo
      #define stereo_type_i_dislike
    #endif

  10. #10
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >#include <windows.h>
    Nice try slick, but the OP explicitly said "P.S. My compiler doesn't have <windows.h>".
    My best code is written with the delete key.

  11. #11
    Flash Animator, OF DOOOOM bluehead's Avatar
    Join Date
    Nov 2001
    Posts
    269
    oh crap

    darn, and i was feeling pretty smart for a sec

    theres a wait(); function im pretty sure, i think in either devcpp or borland's <conio>. but i haven't used that in a long time
    Code:
    #if _emo
      #define stereo_type_i_dislike
    #endif

  12. #12
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    Quote Originally Posted by Cool-August
    P.S. My compiler doesn't have <windows.h>
    If he doesn't have <windows.h>, he probably is not going to have conio either.
    Last edited by prog-bman; 04-08-2005 at 07:58 PM.
    Woop?

  13. #13
    Flash Animator, OF DOOOOM bluehead's Avatar
    Join Date
    Nov 2001
    Posts
    269
    What, is wait(); a windows.h function as well?

    well you know what would be an easier solution... getting a new compiler?

    hey cool-august, what compiler are you using?
    Code:
    #if _emo
      #define stereo_type_i_dislike
    #endif

  14. #14
    Let's do some coding! Welshy's Avatar
    Join Date
    Mar 2005
    Location
    Staffordshire University, UK
    Posts
    168
    instead of getting a new compiler wouldnt downloading windows.h and conio.h be the easier solution?

  15. #15
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >wouldnt downloading windows.h and conio.h be the easier solution?
    Yes, if you ignore the fact that headers only contain declarations, and even if you also downloaded the corresponding libraries, the chances of them being compatible with your compiler are practically zero, it would be easier.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with my program...
    By Noah in forum C Programming
    Replies: 2
    Last Post: 03-11-2006, 07:49 PM
  2. Date program starts DOS's date
    By jrahhali in forum C++ Programming
    Replies: 1
    Last Post: 11-24-2003, 05:23 PM
  3. Replies: 3
    Last Post: 01-14-2003, 10:34 PM
  4. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM