Drake Nguyen
Drake Nguyen

Reputation: 124

gotoXY display on the screen

I know my question is silly but I still need your help.why function of gotoxy do not work expect?

void gotoxy(int x,int y)
{
    COORD coord={x,y};
    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),coord);
}
using namespace std;
int main()
{
    cout<<"___________________________________________________________________________________________________________________________\n";
   cout<<"|                |      XUAT SAC      |        GIOI        |        KHA         |     TRUNG BINH     |        YEU         |\n";
   cout<<"|     MA LOP     |--------------------------------------------------------------------------------------------------------|\n";
   cout<<"|                |    SL    |    %    |    SL    |    %    |    SL    |    %    |    SL    |    %    |    SL    |    %    |\n";
   cout<<"|-------------------------------------------------------------------------------------------------------------------------|\n";
   gotoxy(0,5);cout<<"gotoxy(0,5)";
}

and it is display this:

___________________________________________________________________________________________________________________________
   |                |      XUAT SAC      |        GIOI        |        KHA         |     TRUNG BINH     |        YEU         |
   |     MA LOP     |--------------------------------------------------------------------------------------------------gotoxy(0,5)

I want to gotoxy(0,5)on the screen but it is display in the line of 3

Upvotes: 1

Views: 374

Answers (1)

Or Groman
Or Groman

Reputation: 123

I want to gotoxy(0,5)on the screen but it is display in the line of 3

No, it's actually displayed on line 5.

Default console width on my platform is 120 pixels. When the line printed is too long to fit into the 120 pixels restriction windows console will automatically create a new line, hence Y=Y+1.

This code will most likely work for you since the width is shorter...

std::cout << "_____________________1\n";
std::cout << "_____________________2\n";
std::cout << "_____________________3\n";
std::cout << "_____________________4\n";
std::cout << "_____________________5\n";


gotoxy(0, 5); 
cout << "gotoxy(0,5)";

You can adjust the console width to a wider length and it should work as expected.

https://www.howtogeek.com/howto/19982/how-to-make-the-windows-command-prompt-wider/

Upvotes: 1

Related Questions