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

后端 未结 7 1250
温柔的废话
温柔的废话 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:38

    What you are doing is using some very terminal specific magic characters in an otherwise pure C++ application. While this works, you will probably have a far easier time using a library which abstracts you from having to deal with terminal specific implementation details and provides functions that do what you need.

    Investigate whether curses or ncurses libraries are available for your system.

    0 讨论(0)
  • 2020-12-01 15:43
    void screenpos(int x,int y,char textyowanawrite[20])
    {
    //printf for right shift
    // \n for downward shift
    //loops through the rows and shifts down 
    for(int row=0;row<=y;row++)
    {
    printf("\n");
    for (int i = 0; i < x; i++)
    {
    printf("%s "," " );
    }
    }
    printf("%s ",textyowanawrite );
    } 
    

    //this should work to certain extinct only problem is u cant go from somewhere like 4,4 to 2,2 thats the problem

    0 讨论(0)
  • 2020-12-01 15:47

    I remember using gotoxy(x,y) in Turbo C++ (conio.h) - don't know if it'll work for you though. It moves the cursor to the coordinates specified by x and y.

    EDIT: If you're using Windows, here's a gotoxy clone:

    #include <windows.h>
    
    void gotoxy(int x, int y)
    {
      COORD coord;
      coord.X = x;
      coord.Y = y;
      SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
    }
    
    0 讨论(0)
  • 2020-12-01 15:50

    Curses is what you are looking for.

    0 讨论(0)
  • 2020-12-01 15:59

    First:

    void printToCoordinates(int x, int y, string text)
    {
        printf("\033[%d;%dH%s\n", x, x, text);
    }
    

    You don't want to copy the string argument, you want to pass it by (const) reference. Also, the (only) right way to get a char* from a std::string is to call its c_str() member function:

    void printToCoordinates(int x, int y, const std::string& text)
    {
        printf("\033[%d;%dH%s\n", x, x, text.c_str());
    }
    

    As to your question: C++ has no way to do what you want, but it allows you to use platform-specific ways to do it. You would have to tell us your platform in order to get meaningful answers.

    0 讨论(0)
  • 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

    0 讨论(0)
提交回复
热议问题