Thread: Press any key

  1. #1
    Roland
    Guest

    Arrow Press any key

    I was wondering how to call the "press any key to continue" display in c++ using visual c++.

  2. #2
    Registered User
    Join Date
    Feb 2002
    Posts
    465
    in a console application, you can do

    #include <stdlib.h>

    system("pause");



    or, a little more drawn out, but useful if the above code doesnt work:

    #include <iostream.h>

    int x;
    cout << "Press any key to continue...";
    cin >> x;
    I came up with a cool phrase to put down here, but i forgot it...

  3. #3
    Ryan
    Guest
    Well here it is. If you use stdio.h and conio.h,
    printf("Press any key to continue");
    ch = getche(); \\ ch must be defined, its a variable

    if you use iostream,
    cout<<"Press any key to continue";
    cin>>ch; \\ ch must be defined, its a variable

  4. #4
    Registered User Sekti's Avatar
    Join Date
    Feb 2002
    Posts
    163

    heres one

    Code:
    #include <iostream.h>
    #include <conio.h> //I think...
    
    int main()
    {
    cout<<"Press any key to continue..."<<endl;
    
    if(!kbhit())
    {
         exit(0);
    }
    
    return 0;
    }
    I havent tried it but I'm pretty sure that works
    +++
    ++
    + Sekti
    ++
    +++

  5. #5
    Registered User purple's Avatar
    Join Date
    Mar 2002
    Posts
    28

    Quoting Homer Simpson: "Where's the any key?"

    Yeah I know...dumb...but I couldn't resist.

  6. #6
    Pygmy Monkey ErionD's Avatar
    Join Date
    Feb 2002
    Posts
    408
    just so you know, the cin >> variable method wont work cause that one waill wait until it gets an input before it's done so if you click enter before having written anything the only thing that happens is that it moves to the next line.
    some body wrote a thing like this i think:

    Code:
    #include <conio.h>
    
    void pause() {
    while(!kbhit());
    getch();
    }
    Dunno if it works....
    The bad thing is that conio.h is generally screwed up.

  7. #7
    Registered User
    Join Date
    Jan 2002
    Posts
    387
    The eziest way is:

    #include <stdlib.h>
    ...
    system("PAUSE");

    OR

    #include <iostream.h>
    #include <conio.h>
    ...
    cout << "Press any key to continue...";
    getch();

  8. #8
    Hamster without a wheel iain's Avatar
    Join Date
    Aug 2001
    Posts
    1,385
    dont use pause - its very pooor programming practice for various reasons - go with kbhit - its the best way
    you couls also use

    int wait;
    cout<<"Any key to continue!\n";
    wait=getch();

  9. #9
    Registered User C_Coder's Avatar
    Join Date
    Oct 2001
    Posts
    522
    wait=getch();
    You don't have to use return values, so you don't need wait.
    Code:
    cout<<"Any key to continue!\n"; 
    getch();
    All spelling mistakes, syntatical errors and stupid comments are intentional.

  10. #10
    Unregistered
    Guest

    You could do this....

    #include <iostream>
    using namespace std;

    int main()
    {
    printf( "Press any key to continue..." );
    fflush( stdin );
    getchar();
    return ( 0 );
    }

  11. #11
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Or you could not...

    >fflush( stdin );
    UN-DE-FINED. You cannot use fflush on input streams.

    Here's a run down of this thread, summarized into neato little Prelude comments for your enjoyment.
    >how to call the "press any key to continue" display in c++
    Ask the user for input

    >system("pause");
    Godawful slow, only use in extreme cases of need.

    >cin >> x;
    This is good, but you don't need a variable since all you're asking the user to do is enter 'something'.

    >ch = getche(); \\ ch must be defined, its a variable
    ch is not needed, getche is not standard and may not exist

    >if(!kbhit())
    Once again not standard and may not exist, so why bother if you don't have to have it?

    >Quoting Homer Simpson: "Where's the any key?"
    Ctrl+Alt+Delete

    >getch();
    I wonder how many programmer's heads will pop if we broadcast across the world that conio is not standard C or C++.

    >system("PAUSE");
    Easy, yes. Bad idea, oh yea.

    >its very pooor programming practice for various reasons
    Depends on what you are doing

    >getch();
    C_Coder...you're excused since everyone else was suggesting this. I'll assume you were going with the flow

    >fflush( stdin );
    Wrong, very wrong

    >getchar();
    Standard, effective, efficient...this is the best solution offered in this thread. I would have suggested it if Unregistered didn't. cin.get() would also work well.

    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 11-23-2007, 01:48 PM
  2. Virtual keys
    By Arkanos in forum Windows Programming
    Replies: 4
    Last Post: 12-12-2005, 10:00 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