views:

1185

answers:

4

This doesn't work:

string temp;
cout << "Press Enter to Continue";
cin >> temp;
+3  A: 

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
+13  A: 
cout << "Press Enter to Continue";
cin.ignore();

or, better:

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

A succinct answer for an equally succinct question.

rlbond
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. ;-)
TomWij
cin is tied to cout, thus before any i/o of cin happens, the output of cout is flushed already
Johannes Schaub - litb
A: 

Try using std::cin.get() rather than std::cin::operator<<()

EDIT: Others were faster.

Billy3

Billy ONeal
+4  A: 

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