开发者

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");
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜