35

I have a Win32 program that runs on a loop. I would like to be able to pause that program while awaiting a keypress. It doesn't matter whether I use 'any key' or a specific key, but I need to have the program freeze until I press something.

I am wondering which command I should use. I am working with Visual C++ and the compiler doesn't recognise any of the following commands:

cin.get()

std::cin.get()

getch()

I am relatively new to C++. I understand that in a console app this is a fairly simple action to take (cin.get), but that it can be more difficult in Win32. Any simple solution or workaround would be appreciated. The program is bespoke to be used in a single scientific experiment, so for now I'm not fussed if the solution is a little botchy(!)

Apologies if I've missed any important info from my question.

6
  • 5
    cin.get() and std::cin.get() are same! Commented Aug 10, 2011 at 12:31
  • 1
    What headers have you included? std::cin needs #include <iostream>. _getch requires #include <conio.h>
    – user786653
    Commented Aug 10, 2011 at 12:31
  • Is your program a console or a GUI app? Commented Aug 10, 2011 at 12:32
  • When I'm writing a console program in Windows, I just use system("pause");
    – Kaz Dragon
    Commented Aug 10, 2011 at 12:33
  • 1
    Why do you need to pause, exactly? The thing about a Windows program is that it sits there doing nothing in particular (but pumping messages) until it has something to do. Pausing would stop the message pump and not allow people to resize the window etc until a key was pressed. That's not normal Windows behaviour. Commented Aug 10, 2011 at 12:38

6 Answers 6

80

You should use neither.

You should use

#include <iostream>
...
int main()
{
   ... 
   std::cin.ignore(); //why read something if you need to ignore it? :)
}'

Here's the documentation

3
  • 5
    I like this because it's C++, not C, it doesn't require declaring some variable you're never going to use, and it's clear about your intent. Commented Aug 10, 2011 at 12:36
  • 6
    BTW, for me this is working as "Press enter to exit", not "Press any key to exit". I.e. I can press space, or any other keys, but it does not do anything until Enter is then pressed. (A C++ linux console application.) Commented Apr 6, 2014 at 23:18
  • 1
    @DarrenCook getting keyboard input without pressing enter is really really annoying and there is no portable way to do it. If you want to, you need input handling libraries, which seem overkill for such a simple task.
    – Sparr
    Commented Mar 20, 2015 at 2:15
8

Example:

#include <iostream>
#include <conio.h>

int main()
{
  std::cout << "Press any key to continue . . ." << std::endl;
  _getch(); // wait for keypress
}

_getch() is C++ equivalent to C getch()

1
  • +1 for portable way of actually getting a char and not a line. But unfortunately that produces a return value ignored warning but otherwise ok.
    – Emad
    Commented Sep 13, 2019 at 10:42
2

Try

#include <iostream>

using namespace std;

char temp;
cin >> temp;
3
  • 9
    Unfortunately, that's "press any key, then press enter".
    – Kaz Dragon
    Commented Aug 10, 2011 at 12:32
  • This can be used in a Win32 GUI app? Commented Aug 10, 2011 at 12:54
  • It seems I can't add <iostream> to my code. It throws up all sorts of errors before I even add anything post pre-compiler Commented Aug 10, 2011 at 12:56
2

Assuming that you are looking for an alternative for getch ( which does not echo to screen).

If you are using windows and visual studio to be precise try using _getch. Here is a link to it http://msdn.microsoft.com/en-us/library/078sfkak(v=VS.100).aspx

2
  • 1
    Thanks very much. This has compiled and I'll take it to the lab to try it out! Purely out of interest, does anyone know the reason why a comamnd like getch() would be depracated and then replaced with _getch()? Seems an unnecessary complication (to a noob) Commented Aug 10, 2011 at 12:40
  • Probably because it is not portable and the rename indicates that implicitly by the '_'.
    – Devolus
    Commented Nov 22, 2017 at 9:50
1

You should #include <iostream> and use std::cin.get();

I think the getch() is a C function, but since you are using C++, then the cin would be more appropriate.

-4
HWND hwnd = ::GetConsoleWindow();

while (!((::GetForegroundWindow() == hwnd) &&
        ((::GetKeyState(VK_SPACE) & 0x8000) != 0)))
    ::Sleep(0);

Suppose it is not the best way but it solved my problem. Replace VK_SPACE with any other value you like. And it is not portable.

2
  • 3
    Such infinile loop will eat all process time. Never use it in real projects. Commented Jul 22, 2013 at 11:38
  • @dmitry-sazonov, you are right. I've missed that if there is no other threads in the process Sleep will return immediately. I guess explicit time out would make this code little bit better. But still should not be used in real projects. Commented Jul 24, 2013 at 9:37

Not the answer you're looking for? Browse other questions tagged or ask your own question.