Thread: function to replace system("pause");

  1. #16
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Quote Originally Posted by ZuK
    Found that on another board. It's supposed to be portable. It works by setting the IO-buffersize of stdin to 0. ( that is effectively clearing the buffer ) and setting it to the original size afterwards.

    Code:
    #include <stdio.h>
    
    void wait ()
    {
        puts("press enter to continue.");
        setvbuf(stdin,NULL,_IONBF,0);
        setvbuf(stdin,NULL,_IOFBF,BUFSIZ);
        getchar();
    }
    Kurt
    Great if you want to hit enter, but willc0de4food wanted:
    >but how would i have it pause until any key is pressed?

  2. #17
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by swoopy
    Great if you want to hit enter, but willc0de4food wanted:
    >but how would i have it pause until any key is pressed?
    Then you are out of luck looking for a portable solution. My solution waiting for enter isn't really portable either. But it should be good enough if it is only used for the problem "I cannot see what my program is doing because the window is closing", and I cannot see any other reason why anybody would use system("pause") in a real application ( see topic. )
    Kurt

  3. #18
    Registered User
    Join Date
    Aug 2005
    Posts
    266
    im still pretty new to C, any good site where i can get conio.h, mine doesnt come with it i think...

  4. #19
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by rodrigorules
    im still pretty new to C, any good site where i can get conio.h, mine doesnt come with it i think...
    http://www.eskimo.com/~scs/C-faq/q10.11.html
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #20
    Registered User
    Join Date
    Aug 2005
    Posts
    266
    Quote Originally Posted by Dave_Sinkula
    thanks, that was right - i checked my "bin" folder and i do have conio.h ...but when i try to compile a program with it, it says it doesnt exist in the libary - "clrscr"
    for ex.
    Code:
    #include <stdio.h>
    #include <conio.h> 
    
    int main()
    {
      printf("say this so something may be cleared");
      sleep ( 1200 );
      clrscr();   /* It doesnt clear, i get error weh compileing,, (undefined reference to 'clrscr' ) */
      return(0);
    }
    its supposed to clear it, but its not found
    + im pretty sure clrscr is in the libary

  6. #21
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  7. #22
    Registered User
    Join Date
    Aug 2005
    Posts
    266
    yea, it is - "dev c++ 4"

  8. #23
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    Did you click the link in Dave_Sinkula's post?

  9. #24
    Super Moderater.
    Join Date
    Jan 2005
    Posts
    374
    Code:
    #include <stdio.h>
    #include <conio.h>
    #include <windows.h> 
    
    int main()
    {
      printf("say this so something may be cleared");
      Sleep ( 1200 );// Sleep NEEDS to be capitalized
                     //make sure you include windows.h!
      system("cls"); //not great  but it works on dev++
      printf("hello there again\n");
      getch(); //pause the program
    }
    Of course, not everyone is using windows and in general 'system' commands are frowned upon. Check out the FAQ for more info.

  10. #25
    Registered User
    Join Date
    Aug 2005
    Posts
    266
    i got mine to work btw - i had to include conio.c instead of conio.h (???)

  11. #26
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    The problem may have been that you needed to link the program to the conio library.

  12. #27
    Registered User
    Join Date
    Aug 2005
    Posts
    10

    Thumbs up

    Just to be different:
    Code:
    // an asm wait to mimic getch()
    void wait()
    {
      asm   mov dx,3DAh
    l1:
      asm   in al,dx
      asm   and al,08h
      asm   jnz l1
    l2:
      asm   in al,dx
      asm   and al,08h
      asm   jz  l2
    }
    Ask smart questions - if I would be that smart, I wouldn't need to ask questions.

  13. #28
    Shibby willc0de4food's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    378
    mm...assembly is sweet

    but i dont think i'll ever understand how it works.
    or how to program in it.
    Registered Linux User #380033. Be counted: http://counter.li.org

  14. #29
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > an asm wait to mimic getch()
    And you thought conio lacked portability - gotta love 16 bit assembler in a 32 bit world.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  15. #30
    Shibby willc0de4food's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    378
    *32/64 bit world
    Registered Linux User #380033. Be counted: http://counter.li.org

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner Needs help in Dev-C++
    By Korrupt Lawz in forum C++ Programming
    Replies: 20
    Last Post: 09-28-2010, 01:17 AM
  2. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  3. const at the end of a sub routine?
    By Kleid-0 in forum C++ Programming
    Replies: 14
    Last Post: 10-23-2005, 06:44 PM
  4. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM
  5. I need help with passing pointers in function calls
    By vien_mti in forum C Programming
    Replies: 3
    Last Post: 04-24-2002, 10:00 AM