如何在c ++中从提示符(通过按ENTER键终止)获取用户输入?

时间:2010-08-21 15:14:37

标签: c++ console

有人知道这个提示吗?

2 个答案:

答案 0 :(得分:5)

尝试getline

string s;
getline(cin, s);

答案 1 :(得分:2)

返回键很简单,就像Mehrdad回答的那样,只是从std::cin读了一些东西。

如果您想要按其他按键终止,例如,退出任意按键,您可以在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");
相关问题