Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Press Enter to Continue

This doesn't work:

string temp;
cout << "Press Enter to Continue";
cin >> temp;
like image 255
Elliot Avatar asked May 24 '09 06:05

Elliot


People also ask

How to make press Enter to Continue in C++?

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.

How do you press Enter in C #?

The ASCII Code of ENTER KEY is 10 in Decimal or 0x0A in Hexadecimal.

What does Cin get () do C++?

get() is used for accessing character array. It includes white space characters. Generally, cin with an extraction operator (>>) terminates when whitespace is found.

How do you exit a code in C++?

In C++, you can exit a program in these ways: Call the exit function. Call the abort function. Execute a return statement from main .


4 Answers

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

or, better:

#include <limits>
cout << "Press Enter to Continue";
cin.ignore(std::numeric_limits<streamsize>::max(),'\n');
like image 153
rlbond Avatar answered Sep 24 '22 08:09

rlbond


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.

like image 32
paxdiablo Avatar answered Sep 27 '22 08:09

paxdiablo


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.

like image 8
Nick Presta Avatar answered Sep 24 '22 08:09

Nick Presta


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

like image 2
Ziezi Avatar answered Sep 23 '22 08:09

Ziezi