This doesn't work:
string temp;
cout << "Press Enter to Continue";
cin >> temp;
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.
The ASCII Code of ENTER KEY is 10 in Decimal or 0x0A in Hexadecimal.
get() is used for accessing character array. It includes white space characters. Generally, cin with an extraction operator (>>) terminates when whitespace is found.
In C++, you can exit a program in these ways: Call the exit function. Call the abort function. Execute a return statement from main .
cout << "Press Enter to Continue";
cin.ignore();
or, better:
#include <limits>
cout << "Press Enter to Continue";
cin.ignore(std::numeric_limits<streamsize>::max(),'\n');
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.
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.
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With