gpt4 book ai didi

c++ - 在 C++ 中应该使用什么代码而不是 getche

转载 作者:行者123 更新时间:2023-11-30 04:09:53 25 4
gpt4 key购买 nike

我是c++新手,

我应该写什么代码才能让屏幕静止不动。我用

getche();

C 语言。但不是 getche() 我应该在 C++ 中使用什么;我试过了

std::cin.get();

但控制台窗口会显示,然后迅速关闭。

最佳答案

the console windows displays and then goes off quickly.

看来你的缓冲区里有东西,而且cin.get正在把它当作一个角色来读。例如:

int i = 0;
std::cin >> i;
std::cin.get();

当您输入数字并按 Enter 键时,cin >> i会消耗号码,但会留下\n缓冲区中的字符(来自 Enter 击键)将由 std::cin.get() 读取无需等待用户输入新数据。

为了让这个例子像我们想要的那样运行,我们需要在调用 std::cin.get() 之前清空缓冲区。 :

int i = 0;
std::cin >> i;
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
std::cin.get();

std::numeric_limits<limits> 中定义头文件。

关于c++ - 在 C++ 中应该使用什么代码而不是 getche,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20921127/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com