Waiting for a key to be pressed

This is a discussion on Waiting for a key to be pressed within the C Programming forums, part of the General Programming Boards category; I'm using C and on a *nix platform. Wondering how to make it wait for the user to hit any ...

  1. #1
    Ne0
    Ne0 is offline
    Registered User
    Join Date
    Jul 2004
    Posts
    17

    Waiting for a key to be pressed

    I'm using C and on a *nix platform. Wondering how to make it wait for the user to hit any key to continue, or at the least the enter key. Been through 20 pages on google and many forums, keep going in loops lol.

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,159
    Waiting for the user to press the enter key is much easier than waiting for them to press any key:
    Code:
    #include <stdio.h>
    
    int main(void)
    {
      printf("Press ENTER to exit program");
      getchar();
      return 0;
    }

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,337
    Well the standard getchar() will wait for enter to be pressed, so long as you haven't already messed up the input stream by using scanf()

    You probably need to flush the input stream before calling getchar()
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Ne0
    I'm using C and on a *nix platform. Wondering how to make it wait for the user to hit any key to continue, or at the least the enter key. Been through 20 pages on google and many forums, keep going in loops lol.
    You could have always just checked the FAQ...

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Ne0
    Ne0 is offline
    Registered User
    Join Date
    Jul 2004
    Posts
    17
    getchar() works if it's alone, I have a pretty long program with a lot of inputting so i assume i have to flush it before I want to make it wait for a key to be pressed, i could just make a function for this no problem, i read the faq and i'm pretty clueless :P Still new to C x.x After looking at that faq basically i wants me to enter all this crap each time, any easy way to make this its own function so i could essentialy do whatever(); and it flushes? Thanks

  6. #6
    Ne0
    Ne0 is offline
    Registered User
    Join Date
    Jul 2004
    Posts
    17
    Got it Thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Virtual keys
    By Arkanos in forum Windows Programming
    Replies: 4
    Last Post: 12-12-2005, 09:00 AM
  2. Resource ICONs
    By gbaker in forum Windows Programming
    Replies: 4
    Last Post: 12-15-2003, 06:18 AM
  3. Directional Keys - Useing in Console
    By RoD in forum C++ Programming
    Replies: 38
    Last Post: 10-06-2002, 04:42 PM
  4. FAQ: Directional Keys - Useing in Console
    By RoD in forum FAQ Board
    Replies: 38
    Last Post: 10-06-2002, 04:42 PM

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