waiting for a keypress

This is a discussion on waiting for a keypress within the C Programming forums, part of the General Programming Boards category; Hey I'm working on a program for a class I'm in and I'm wondering how you would tell the program ...

  1. #1
    Registered User
    Join Date
    May 2002
    Posts
    4

    waiting for a keypress

    Hey I'm working on a program for a class I'm in and I'm wondering how you would tell the program to wait until any key is pressed. I'm displaying a bunch of information and I would like to have the computer wait for any key (or a specific one if it has to be) to be pressed before going back to the main menu. Is this possible?

    Thanks!

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Its in the faq
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Comment your source code! Lynux-Penguin's Avatar
    Join Date
    Apr 2002
    Posts
    533
    using the conio header file
    use getch() which waits for a key to be pressed before continuing
    Code:
    #include <conio.h>
    #include <stdio.h>
    int main()
    {
    	printf("TON OF INFO\n");
    	getch();
    	return 0;
    }
    //  NOW FOR C++ VERSION
    #include <iostream.h>
    #include <conio.h>
    int main()
    {
    	cout<<"TON OF INFO"<<endl;
    	getch();
    	return 0;
    }
    // getch() will stop after the endl; in C++ 
    // not sure bout the C though.
    usually it works like:
    cout<<"Press any key to continue"<<endl;
    getch();
    Asking the right question is sometimes more important than knowing the answer.
    Please read the FAQ
    C Reference Card (A MUST!)
    Pointers and Memory
    The Essentials
    CString lib

  4. #4
    Registered User stautze's Avatar
    Join Date
    Apr 2002
    Posts
    195
    This is what I use, you need a specific key stroke, but it is as portable as it gets.

    Code:
    printf("Please press <ENTER> to continue.");
    while(getchar() != '\n');
    'During my service in the United States Congress, I took the initiative in creating the Internet.' - Al Gore, March 9, 1999: On CNN's Late Edition

  5. #5
    Registered User
    Join Date
    May 2002
    Posts
    4

    Thanks

    Hey thanks guys!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 11-21-2009, 12:02 AM
  2. Detecting keypress
    By dandago in forum C Programming
    Replies: 4
    Last Post: 06-10-2007, 09:34 AM
  3. Getting input without waiting for keypress
    By Anon48 in forum C Programming
    Replies: 2
    Last Post: 04-08-2005, 02:18 AM
  4. Getting input without waiting for keypress
    By Ink in forum C Programming
    Replies: 4
    Last Post: 10-01-2004, 01:38 AM
  5. Resource ICONs
    By gbaker in forum Windows Programming
    Replies: 4
    Last Post: 12-15-2003, 06:18 AM

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