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).
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:
or, better yet:
I think the string input will wait until you enter real characters, not just a newline.
You need to include conio.h so try this, it's easy.
With that you don't need a string or an int for this just
getch();
Try:
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).The function std::getline (already introduced with C++98) provides a portable way to implement this:
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).Replace your
cin >> temp
with: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.or, better: