Friday, January 3, 2014

cpp,c++ or c gotoxy

The x and y axis of c++ or gotoxy()

Sometimes or several times the use of \t for tab, \n for new line and spacebar is very time consuming in preparing a nice and well formatted output in c++, as you read this blog you will learn how to format your output without using spacebar to align and \n to brake a line, and the most important you will learn how to use gotoxy in c++.

Before we start our discussion you must have an idea on Cartesian coordinates plane, the quadrants of (-x,+y), the quadrant of (+x,+y), the quadrant of (-x,-y) and lastly the quadrant of (+x,-y). Since this blog is focus on c++ and not in math subject in gotoxy we are only using the quadrant (+x,+y). I provide an image below so it will wash away the confusion inside you mind:

Remember the maximum value of x-axis is 80, then the y-axis is 50, negative value of coordinates is still accepted but there’s nothing change if you try to use it. As you can see the character M is place at bottom right of the window and to attain that position your gotoxy must have the values of (80,50) or gotoxy(80,50) in which x is 80 and y is 50.

gotoxy() required conio header(include) , the syntax of gotoxy is gotoxy(int x,int y) and remember the cursor will move to the position declared inside gotoxy function.

Sample codes of gotoxy in c++

#include<stdio.h>
#include<conio.h>
main()
{
clrscr();
 gotoxy(80,50);
 printf(“right bottom”);
getch();
}

These codes will produce an output “right bottom” and it is place at the lower right of your c++ window. What if the code become like this :

clrscr();
 gotoxy(80,50);
 printf(“right bottom”);
printf(“another printf”);
getch();

In code above another printf is inserted below, thus the first printf is place at the lower right of the screen, the second printf will join the first printf but not place in the lower right but in lower left. The rule is like typing a paragraph in msword when you reach the right margin the next characters you type will be on the lower left of the previous characters you type. Lastly, what if you like to split the position of two printf? It is just simple put gotoxy function before the printf that you would want to declare the position. Example:

clrscr();
 gotoxy(80,50);
 printf("right bottom");
 gotoxy(0,25);
 printf("another printf");
getch();

C++ programming is easy and nice foundation, remember live and love, thank you for reading my simple tutorial about x axis and y axis or gotoxy in c++, I hope you’ll learn something. Thank you and have a nice day!