getch() 函数在输入 key 时不执行任何操作

getch() function does nothing when entering key

本文关键字:执行 任何 操作 key 输入 函数 getch      更新时间:2023-10-16

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

我想使用 getch(( 在 c++ 中获取 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;
}

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