Move text cursor to particular screen coordinate?

后端 未结 7 1850
余生分开走
余生分开走 2020-12-31 19:01

How can I set the cursor at the desired location on the console in C or C++?

I remember a function called gotoxy(x,y), but I think its deprecated. Is th

7条回答
  •  Happy的楠姐
    2020-12-31 19:51

    You can use this to set the cursor to specific coordinates on the screen, and then simply use cout<< or printf statement to print anything on the console:

    #include 
    #include 
    
    using namespace std;
    
    void set_cursor(int,int);
    
    int main()
    {
         int x=0 , y=0;
         set_cursor(x,y);
         cout<<"Mohammad Usman Sajid";
    
         return 0;
    }
    
    void set_cursor(int x = 0 , int y = 0)
    {
        HANDLE handle;
        COORD coordinates;
        handle = GetStdHandle(STD_OUTPUT_HANDLE);
        coordinates.X = x;
        coordinates.Y = y;
        SetConsoleCursorPosition ( handle , coordinates );
    }
    

提交回复
热议问题