Thread: How to implement a pause

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    62

    How to implement a pause

    Just for the sake of wanting my own programs to not scroll off endlessly (Gets annoying after a time), I am wondering if C has any way to pause the program after it scrolls through a determined (or built-in) number of lines. Either works fine, but i'd just like my stuff to stop scrolling 4000 lines before I get a chance to actually see it.
    Thanks, cheers.

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Not built into the basic printf type funcitons. You can just pipe your output through a pager like more or less, e.g. "my_program | less". Otherwise, you would need your own print functions that have a buffer with the number of lines you want, and use those instead of printf et al. When that fills up, print all the lines to screen and clear the buffer. Note you will have to manually count newlines in the formatted text, so you know how many actual lines you have, instead of how many calls to my_printf.

  3. #3
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    As an alternative, "lazy" alternative you could throw in an empty getchar(). Which will just wait for you to hit enter before continue. But if you have more/less available, it's probably the easiest way of doing this as you don't even need to modify your code.

  4. #4
    Registered User
    Join Date
    Jan 2011
    Posts
    55
    #include<unistd.h>

    usleep(amount of time to wait);

    Place usleep(); after any part you want to pause.

    Example:
    Code:
    #include<stdio.h>
    #include<unistd.h>
    
    
    
    
    #define NUM_STARS 5
    
    void diamond( int len )
    {
    int i, sp;
    
    
    //print stars diagonaly left to right
    
    for (i = 0; i < len; ++i)
      {
       printf("   ");
        for(sp = 0; sp < i; ++sp)
        {
         printf(" ");
        }
       printf("*\n");
       usleep(60000);
      }
    
    //print stars diagonaly right to left
    
    for (i = len - 2; i >= 0; --i)
      {
       printf("   ");
        for(sp = 0; sp < i; ++sp)
        {
         printf(" ");
        }
       printf("*\n");
       usleep(60000);
      }
    }
    
    
    int main(void)
    {
    
    int t;
    int x;
    
    
    srand( time(0) );
    for (t = 1; t >= 0; ++t)
      {
       x = rand()%21 + 7;
       diamond (x);
      }
    
    
    
    
    return 0;
    
    }
    Let me know if this helps...
    Last edited by DuckCowMooQuack; 04-18-2011 at 09:22 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 11-12-2007, 03:29 PM
  2. pause
    By firefly in forum C++ Programming
    Replies: 16
    Last Post: 06-23-2005, 11:23 AM
  3. How do i get it to pause ?
    By joeyzt in forum C++ Programming
    Replies: 6
    Last Post: 06-19-2003, 02:52 PM
  4. Pause for just a bit...
    By WhiteTiger0 in forum C++ Programming
    Replies: 3
    Last Post: 07-16-2002, 04:23 PM
  5. pause
    By Moffesto in forum C++ Programming
    Replies: 4
    Last Post: 06-21-2002, 10:15 AM