How to get user input from a prompt(terminated by press ENTER) in c++?
A开发者_运维知识库nyone knows this tip?
Try getline
:
string s;
getline(cin, s);
Return key is the easy case, as Mehrdad answered, just read something from std::cin
.
If you want to terminate on a different key press, for example, exit on any key, you can use a couple non-standard calls in conio.h
.
#include <conio.h>
// wait for any key press
while (!kbhit()) { }
// wait for q key press
while (!kbhit() || getch() != q) { }
// wait for any key press on windows
system("pause");
精彩评论