Press Enter to Continue

101,250

Solution 1

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

or, better:

#include <limits>
cout << "Press Enter to Continue";
cin.ignore(std::numeric_limits<streamsize>::max(),'\n');

Solution 2

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.

Solution 3

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.

Solution 4

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).

Solution 5

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();

Share:
101,250
Elliot
Author by

Elliot

Updated on February 01, 2020

Comments

  • Elliot
    Elliot over 4 years

    This doesn't work:

    string temp;
    cout << "Press Enter to Continue";
    cin >> temp;
    
  • Tamara Wijsman
    Tamara Wijsman almost 15 years
    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. ;-)
  • Johannes Schaub - litb
    Johannes Schaub - litb almost 15 years
    cin is tied to cout, thus before any i/o of cin happens, the output of cout is flushed already
  • AndreasHassing
    AndreasHassing almost 7 years
    @dani I think he argues that it is better because it the second version explicitly looks for a newline character, as requested.
  • Private_GER
    Private_GER over 6 years
    This answer isnt working anymore, the getch() function was deprecated.
  • Alexander
    Alexander almost 6 years
    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.
  • Dan Stahlke
    Dan Stahlke over 5 years
    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.