Search code examples
c++command-lineposition

C++ - Check if a character is present at a given position on the console


If I have the coordinates of a point on the screen x and y and I wanna check if a specified character is at those coordinates on the screen, how do I do it?

I am using Borland C++(for a school project) and the screen is command-line.


Solution

  • Though I avoided the use of this in my program, and came up with a better solution to the odd problem, this seemed to work :

    #include <windows.h>
    
    //....
    
     char get_char_at_xy( int x, int y )
      {
       CHAR_INFO ci;
       COORD xy = { 0, 0 };
       SMALL_RECT rect = { x, y, x, y };
       return ReadConsoleOutput(GetStdHandle(STD_OUTPUT_HANDLE),&ci,1,xy,rect)
           ? ci.Char.AsciiChar
           : '\0';
      }