How can I print a string to the console at specific coordinates in C++?

后端 未结 7 1260
温柔的废话
温柔的废话 2020-12-01 15:24

I\'m trying to print characters in the console at specified coordinates. Up to now I have been using the very ugly printf(\"\\033[%d;%dH%s\\n\", 2, 2, \"str\");

7条回答
  •  生来不讨喜
    2020-12-01 15:59

    I have a little different method. Not sure whether this is better than ncurses package, so i leave that for the upvoters to decide.

    You can use Graphics package in C++ to output a text to a specific coordinate on your working screen. The syntax is outtextxy(x, y, text) ; Where x & y are coordinates.

    One example is:

    int main(void) {
    
        int gdriver = DETECT, gmode;
    
        int x = 200, y = 200;
    
      
    
        initgraph(&gdriver, &gmode, "c:\\tc\\bgi");
    
      
    
        outtextxy(x, y, "Hello World");
    
        closegraph();
    
     }
    

    This little program will print Hello World in the coordinate (200,200).

    For reference to what graphics package can do visit this link

提交回复
热议问题