Search code examples
c++getch

getch() function does nothing when entering key


getch() function does nothing and my program is stuck in getch() line!

I want to get char input in c++ with getch() but nothing happens when I run this code and It is stuck in char c= getch() even when I enter something.

#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;
}

Solution

  • My project was a graphic project but i hadn't had initwindow(lentgh,width) in my code. So I added this command to my code before my loop and then tried again (Pressing my key while the graphic window was open). Then, It worked! I don't know the reason but that's the answer to my question.