【发布时间】: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 还要求您在控制台中输入密钥。