开发者

How to get user input from a prompt(terminated by press ENTER) in c++?

开发者 https://www.devze.com 2023-01-12 07:50 出处:网络
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.

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

精彩评论

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