Saturday, May 22, 2010

gotoxy in Code::Blocks

Solution to gotoxy in codeblocks.

1. #include <windows.h>
2. Define a global variable  COORD coord = {0, 0}; // sets coordinates to 0,0
3. Make a function gotoxy()
void gotoxy (int x, int y)

        coord.X = x; coord.Y = y; // X and Y coordinates
        SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}

You can call this gotoxy()  function to put cursor at any postion.

to change cursor type i.e. blinking,

_setcursortype(_NORMALCURSOR);  //for normal blinking cursor                               _setcursortype(_NOCURSOR);  //for NO cursor                                                                                      put these functions accordingly where u need it

9 comments:

  1. thanks for help.i do this but it wasn't useful..
    #include
    #include
    using namespace std;
    int main()
    {
    gotoxy(12,5);
    cout<<"hello";
    }
    error!:
    |6|error: 'gotoxy' was not declared in this scope|
    ||=== Build finished: 1 errors, 0 warnings ===|

    ReplyDelete
  2. You're a genious, thank you.


    Muchas gracias bato loco, me ha servido un montón.

    Paz

    ReplyDelete
  3. @leila
    the function definition for the gotoxy must be placed before main function otherwise it must be declared and defined at any place you want. hope u can get away with that error.

    ReplyDelete
  4. Hi this was very helpful! Thanks a lot !

    ReplyDelete
  5. hello,

    this was very very helpful... thnks a lot

    ReplyDelete
  6. hi just tried this one and it had an error. help please?


    here's my code.


    #include
    #include
    #define coord = {0, 0};


    void gotoxy(int x, int y){
    coord.X= x;
    coord.Y= y;
    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
    }

    int main()
    {
    gotoxy(12,10);
    printf("Hello world!\n");
    return 0;
    }

    error:expected expression before '=' token

    ReplyDelete
    Replies
    1. Instead of #define coord = {0,0}; put the following global variable
      COORD coord = {0, 0};

      Since the COORD is a structure which contains the structure for holding x and y coordinates.
      This may solve the problem.

      Delete