41

This doesn't work:

string temp;
cout << "Press Enter to Continue";
cin >> temp;
Jaroslav Kadlec
  • 2,505
  • 4
  • 32
  • 43
Elliot
  • 6,086
  • 11
  • 45
  • 57
  • The right way to achieve this is: `inline void WaitEnter() { std::cout << "Press Enter to continue..."; while (std::cin.get()!='\n'); }` Most of the answers here is just messing about. You can even put this in a lambda if you want. – slashmais Apr 29 '18 at 15:45

7 Answers7

84
cout << "Press Enter to Continue";
cin.ignore();

or, better:

#include <limits>
cout << "Press Enter to Continue";
cin.ignore(std::numeric_limits<streamsize>::max(),'\n');
JHBonarius
  • 10,824
  • 3
  • 22
  • 41
rlbond
  • 65,341
  • 56
  • 178
  • 228
  • That is indeed a cross platform way, one can put << flush behind the cout and cin.sync() between those lines to make sure it works in every case. ;-) – Tamara Wijsman May 24 '09 at 07:05
  • 5
    cin is tied to cout, thus before any i/o of cin happens, the output of cout is flushed already – Johannes Schaub - litb May 24 '09 at 15:06
  • @dani I think he argues that it is better because it the second version explicitly looks for a newline character, as requested. – AndreasHassing Jul 18 '17 at 00:39
  • 1
    What is the difference, isn't `cin.ignore()` also looking for a newline character? After a quick test here, I could not see any difference in behavior. – Alexander Jun 02 '18 at 17:30
9

Try:

char temp;
cin.get(temp);

or, better yet:

char temp = 'x';
while (temp != '\n')
    cin.get(temp);

I think the string input will wait until you enter real characters, not just a newline.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
8

Replace your cin >> temp with:

temp = cin.get();

http://www.cplusplus.com/reference/iostream/istream/get/

cin >> will wait for the EndOfFile. By default, cin will have the skipws flag set, which means it 'skips over' any whitespace before it is extracted and put into your string.

Nick Presta
  • 28,134
  • 6
  • 57
  • 76
2

Try:

cout << "Press Enter to Continue";
getchar(); 

On success, the character read is returned (promoted to an int value, int getchar ( void );), which can be used in a test block (while, etc).

Ziezi
  • 6,375
  • 3
  • 39
  • 49
2

You need to include conio.h so try this, it's easy.

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

int main() {

  //some code like
  cout << "Press Enter to Continue";
  getch();

  return 0;
}

With that you don't need a string or an int for this just getch();

Floern
  • 33,559
  • 24
  • 104
  • 119
HappyMajor
  • 21
  • 3
2

The function std::getline (already introduced with C++98) provides a portable way to implement this:

#include <iostream>
#include <string>

void press_any_key()
{
    std::cout << "Press Enter to Continue";
    std::string temp;
    std::getline(std::cin, temp);
}

I found this thanks to this question and answer after I observed that std::cin >> temp; does not return with empty input. So I was wondering how to deal with optional user input (which makes sense for a string variable can of course be empty).

Wolf
  • 9,679
  • 7
  • 62
  • 108
  • This is the only one that eats spurious input in the case where the line is not blank. If used in a loop, the other answers will run multiple iterations in that case. – Dan Stahlke Aug 23 '18 at 23:20
0

Yet another solution, but for C. Requires Linux.

#include <stdio.h>
#include <stdlib.h>

int main(void) {
    printf("Press any key to continue...");
    system("/bin/stty raw"); //No Enter
    getchar();
    system("/bin/stty cooked"); //Yes Enter
    return 0;
}
Ricky Gonce
  • 105
  • 1
  • 10