gotoxy() function in c++

What is gotoxy() function?

gotoxy() function in C++  is used to place the position of the cursor at the required position on the screen. You can pass the x and y coordinates to the function, based on which function decides the desired position of the cursor on the screen to print the required output message. It’s very useful to improve the readability of the console messages.

In this article, we are going to study different aspects of this function in C++, like its syntax, input parameters, function return type, compatibility, and required header files. We will also see the sample code and its output to understand it in a better way. Stay tuned and go through this article thoroughly.

Default gotoxy() function:

Function Syntax:

Syntax of this function is very simple and it only takes two input parameters that are x co-ordinate and y co-ordinate as shown below.

gotoxy(int x, int y);

gotoxy(): Function name

int x: First input parameter in integer

int y: Second input parameter in integer

Function Input Parameters:

This function takes two input parameters i.e. X co-ordinate and Y co-ordinate.

X co-ordinate (First parameter): This parameter represents the horizontal position of the cursor from left to right on the screen. You can also refer to it as a column number.

Y co-ordinate (Second Parameter): This parameter represents the vertical position of the cursor from top to bottom on the screen. You can also refer to it as row number.

Example:

gotoxy(15,25);

This function will move the cursor to 15 positions from the left to right and 25 positions from top to bottom.

Function return type:

This function is a void function. It does not return anything.

Required Header File:

It is a predefined declared in the conio.h header file.

Example:

#include <stdio.h>
#include <conio.h>

void main()
{
	gotoxy(10,15);
	printf("Welcome to Technolads !!! \n");
	getch();
}

Limitations of predefined default gotoxy() function:

This function works only if you are using Turbo c++ compiler. Modern C/C++ compilers does not support gotoxy() function. gotoxy() is not a standard C/C++ library function.

Custom gotoxy() function:

You can create your own gotoxy() function to achieve the required functionality. Use SetConsoleCursorPosition() function to carry out the same operation. Let’s understand in detail about the SetConsoleCursorPosition() function.

Required header file:

SetConsoleCursorPosition() function is predefined in the windows.h header file. You have to include windows.h header file at beginning of the program as shown below.

Input Parameters:

SetConsoleCursorPosition() function takes two arguments i.e.  HANDLE and COORD.

HANDLE: A HANDLE is a handle to the output console screen. You can get the handle using predefined function “GetStdHandle(STD_OUTPUT_HANDLE)”.

COORD: denoted the new cursor position in characters on the output screen. It contains X and Y coordinate values. These co=ordinates must be within the output console screen boundaries.

Function Return Type:

SetConsoleCursorPosition() function return 0 if there is any error while setting the cursor position otherwise it returns non zero values.

Example:

Following is a simple example of the gotoxy function.

#include <stdio.h>
#include <windows.h> //You must include this header file

void gotoxy(short a, short b); //function declaration
// Main Function
void main()
{
   
    gotoxy(15,25);   //Call to custom gotoxy() function
  
    printf("Welcome to Technolads"); //Output statement on the screen

    getch();//function to hold the screen
}
void gotoxy(short a, short b) //Custom gotoxy() function
{
    COORD coordinates; //Data type of co-ordinates
    coordinates.X = a; //Assign value to X- Co-ordinate
    coordinates.Y = b; //Assign value to Y Co-ordinate

SetConsoleCursorPosition(
        GetStdHandle(STD_OUTPUT_HANDLE), coordinates);

}

Output:

gotoxy

Final thoughts:

If you are using old age Turbo C++ compiler (I am not sure why you are still using this ancient compiler)

Then you can directly use gotoxy(). But if you are using any other compiler then you can easily create your own gotoxy() function using the SetConsoleCursorPosition() function as shown above.

We hope this article was informative and you got a clear idea about the use of the gotoxy() function.

Keep Learning !!!.

Leave a Comment