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

  1. #1
    Shibby willc0de4food's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    378

    function to replace system("pause");

    hey, i'm looking to write a function that will have a similar effect as system("pause"); and i can get it to where the program will pause until the enter key is pressed, but how would i have it pause until any key is pressed? is it doable? thanks
    Registered Linux User #380033. Be counted: http://counter.li.org

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    Well if you're using an implementation that has conio.h (not standard, but fairly common) getch() does the trick quite nicely.

  3. #3
    Nonconformist Narf's Avatar
    Join Date
    Aug 2005
    Posts
    174
    is it doable?
    Sure, but it's not as easy without knowing your compiler and OS, and does it really matter what the user types to stop the program? I mean, what do you really gain by letting them hit any key as opposed to enter?
    Just because I don't care doesn't mean I don't understand.

  4. #4
    Shibby willc0de4food's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    378
    Windows XP Pro SP2 | Dev-C++ 4.9.9.2
    mm..i'm lazy. its easier to hit the space bar than the enter key. lol

    and portability would be nice, but not absolutely necessary. lol i dual boot fedora core 4 x86_64
    Last edited by willc0de4food; 08-31-2005 at 08:08 PM.
    Registered Linux User #380033. Be counted: http://counter.li.org

  5. #5
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    getch also exists in the ncurses library under unix, but if you want to be truly portable, you can't "wait for a key" because the C standard doesn't require or understand the notion of a keyboard.

  6. #6
    Shibby willc0de4food's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    378
    thats more portable than i was thinking :P
    anything i'd want to use it for would have a keyboard.. lol
    Registered Linux User #380033. Be counted: http://counter.li.org

  7. #7
    Banned
    Join Date
    Jun 2005
    Posts
    594
    you could always switch to c++ and use


    cin.get();

  8. #8
    Shibby willc0de4food's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    378
    mm...i have it working in C. i like C better than C++, i find it easier to get around.. esp. with file i/o. but here's the code i have:
    Code:
    #include <stdio.h>
    
    #ifdef WIN32
        #include <conio.h>
    #elif LINUX
        #include <ncurses.h>
    #else
    #error Need another OS
    #endif
    
    void pause (void)
    {
         int ch;
         
         /* flush the input buffer, just in case */
         while ((ch = getchar()) != '\n' && ch != EOF);
         
         printf("Press any key to continue.. ");
         getch();
    }
    Registered Linux User #380033. Be counted: http://counter.li.org

  9. #9
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    If only it were that easy. With ncurses you have to call initscr to initialize the curses window and cbreak to disable line oriented mode for getch to work properly, then when you're done with the curses part of your program, you need to call endwin as the inverse to initscr. Or you could just write your own getch using Linux terminal controllers:
    Code:
    #ifdef WIN32
    #include <conio.h>
    #else
    #include <termios.h>
    #include <unistd.h>
    #include <stdio.h>
    
    int getch ( void )
    {
      struct termios term, save;
      int c;
    
      /* Get the old terminal and save it */
      tcgetattr ( STDIN_FILENO, &term );
      save = term;
    
      /* Enter non-canonical mode */
      term.c_lflag &= ~ICANON;
      term.c_lflag &= ~ECHO;
      term.c_cc[VMIN] = 1;
      term.c_cc[VTIME] = 0;
    
      /* Apply the new settings */
      tcsetattr ( STDIN_FILENO, TCSANOW, &term );
    
      /* getch! */
      c = getchar();
    
      /* Restore the original terminal */
      tcsetattr ( STDIN_FILENO, TCSANOW, &save );
    
      return c;
    }
    #endif
    
    void pause ( void )
    {
      int c;
    
      while ( ( c = getchar() ) != EOF && c != '\n' )
        ;
    
      printf ( "Press any key to continue..." );
      fflush ( stdout );
      getch();
    }
    >you could always switch to c++ and use
    >cin.get();
    And that solves the problem how? get has the same problem as getchar in that input in C and C++ is line oriented. get is not the same as getch.
    My best code is written with the delete key.

  10. #10
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    Be careful of using the function name "pause" under unix and its derivatives, as it's implemented already to wait for a signal.

  11. #11
    Banned
    Join Date
    Jun 2005
    Posts
    594
    oh i didnt read the problem as clear as i should, i though
    he just wanted a way to pause the screen without running
    an independent program, that was portable.

  12. #12
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Windows XP Pro SP2 | Dev-C++ 4.9.9.2
    getche() does exactly what you are looking for. But it isn't standard, either.

    [edit]
    Or see the FAQ.
    [/edit]
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  13. #13
    Shibby willc0de4food's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    378
    i know it does what i'm looking for. lol
    i dual boot fedora core 4 x86_64
    (post 4)
    i'd like it to work in linux too.

    thanks guys
    Registered Linux User #380033. Be counted: http://counter.li.org

  14. #14
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    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
    Last edited by ZuK; 09-02-2005 at 04:55 AM.

  15. #15
    Nonconformist Narf's Avatar
    Join Date
    Aug 2005
    Posts
    174
    It works by setting the IO-buffersize of stdin to 0.
    That's a clever and creative solution. But it's not portable. setvbuf has to be called after the stream is open--okay so far, stdin is opened by the runtime startup code--and before any other input functions are called. So it can't be used to discard leftover characters in the stream.
    Just because I don't care doesn't mean I don't understand.

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