Thursday, March 17, 2011

gotoxy function in Codeblocks

gotoxy(int x, int y) a function available in Turbo C/C++. It is not standard C. This function is used to move the cursor on the screen to the desire location. The top left corner of the monitor is 0,0 and the bottom right corner might be any number based on the size of the screen. But today's standard C++ compiler such as Visual Studio, GCC and clang do not provide gotoxy function. However, if you are developing console applications without a third party graphic library then gotoxy function is useful to manage text alignment on the screen.

If you need similar implementation on a windows machine with GCC compiler then here is an example source code.
#include <windows.h>    //  header file for gotoxy
#include <iostream> //header file for standard input output
COORD coord= {0,0}; // this is global variable
void gotoxy(int x,int y)
{
    coord.X=x;
    coord.Y=y;
    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),coord);
}

int main()
{
    //calling these function
    int x;
    gotoxy(15,5);
    std::cout<<"1. This is item number one.";
    gotoxy(15,7);
    std::cout<<"2. This is item number two.";
    gotoxy(15,9);
    std::cout<<"3. This is item number three.";
    gotoxy(15,11);
    std::cout<<"4. This is item number four.";
    gotoxy(15,13);
    std::cout<<"5. This is item number five.";
    std::cin>>x;
    return 0;
}
Please watch the video below for detailed explanation.

17 comments:

  1. Very nice code... thank a lot....

    ReplyDelete
  2. Thank you!!!
    It worked!!

    ReplyDelete
  3. Exactly what i needed, thank you!!

    ReplyDelete
  4. Muchas gracias :D :D :D!!!

    ReplyDelete
  5. Just curious, for Linux and other systems that support escape sequences, do so.

    void gotoxy(int x,int y)
    {
    static const char *CSI = "\33["; // Esc (Escape) -> 27(10) = 33(8) = 1B(15)
    printf("%s%i;%iH", CSI, x, y);
    }

    ReplyDelete
  6. gracias hermano, queda entendido.

    ReplyDelete
  7. Thank u so much its workink.........

    ReplyDelete
  8. How to increase the font size in that?

    ReplyDelete
  9. It doesn't work for me the windows.h and stdlib.h to use gotoxy

    ReplyDelete
  10. Excellent post,this is really helpful to me.Continue sharing.
    Regards,
    C++ Training in Chennai | C++ programming course

    ReplyDelete
  11. Excellent post, Thanks for sharing this.

    Learn more about our courses to get the best knowledge. Agile Coach Certification

    ReplyDelete