【问题标题】:getch() function does nothing when entering keygetch() 函数在输入密钥时什么也不做
【发布时间】:2019-03-26 12:43:17
【问题描述】:

getch() 函数什么都不做,我的程序卡在了 getch() 行!

我想在 c++ 中使用 getch() 获取 char 输入,但是当我运行此代码时没有任何反应,即使我输入了某些内容,它也卡在 char c= getch() 中。

#include <iostream>
#include <graphics.h>
#include <conio.h>

using namespace std;
int main()
{
    int n = 4;
    int table[10][10] = {{0}};
    srand(time(NULL));
    table[0][rand() % n] = 2;
    table[n - 1][rand() % n] = 2;
    cout << 1;
    char c = getch();
    cout << c;
}

【问题讨论】:

  • getch 在某些平台上已弃用。您可以改用std::cin,特别是如果您在代码中使用std::cout
  • 其实我想在游戏中使用它作为按键输入。
  • 试试std::cin.get()
  • 它需要进入控制台,但我不想要它。
  • 您想在哪里输入密钥? getch 还要求您在控制台中输入密钥。

标签: c++ getch


【解决方案1】:

我的项目是一个图形项目,但我的代码中没有 initwindow(lentgh,width)。所以我在循环之前将此命令添加到我的代码中,然后再次尝试(在图形窗口打开时按下我的键)。然后,它奏效了!我不知道原因,但这就是我的问题的答案。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-24
    • 1970-01-01
    • 1970-01-01
    • 2018-09-15
    • 2019-04-28
    相关资源
    最近更新 更多