Using Gotoxy in C++ for Positioning Characters on Screen

  • C/C++
  • Thread starter chrisalviola
  • Start date
  • Tags
    C++
In summary, there is no way to write characters or numbers anywhere on the screen in C++ like the ones used in pascal. You have to use an add-on library which is usually tailored for a particular operating system.
  • #1
chrisalviola
80
0
Is there a way I can write characters or numbers anywhere on the screen in C++ like the ones used in pascal like gotoxy where I simply write screen coordinates as x & y.
 
Technology news on Phys.org
  • #2
Standard C++ doesn't have graphics functions "built into" it. You have to use an add-on library which is usually tailored for a particular operating system. Your particular version of C++ may come with such a library, but we can't tell you how to use it unless we know which one it is! :frown:

So, tell us which C++ compiler you're using and whether you're using Windows, MacOS or whatever, and maybe someone can help.
 
  • #3
Under unix there is a library called curses (or ncurses in newer versions) to do printing effects on a character terminal.
Under a GUI it would depend on what platform but most have the ability to write text at a particular coordinate in a window, or you could use something like OpenGL.
 
  • #4
for msvc its:
#include <windows.h>
void gotoxy(int x, int y)
{
COORD coord;
coord.X = x; coord.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}

http://msdn2.microsoft.com/en-us/library/ms686025.aspx"
 
Last edited by a moderator:
  • #5
mgb_phys said:
Under unix there is a library called curses (or ncurses in newer versions) to do printing effects on a character terminal.
Under a GUI it would depend on what platform but most have the ability to write text at a particular coordinate in a window, or you could use something like OpenGL.

for GUI in c++ i recommend Qt, it's way of handling events is the best I've seen so far, and it works flawlessly on windows, Linux, and macs... that's the only platforms i programmed for, but it lists many more OS's.
for outputting text with Qt, you can use the QTextEdit widget, create QLabels anywhere on the window, or create you own display widget and render it using either OpenGL (using QGLWidget) or QPainter...

I really love the Qt documentation, it's full of really good examples and tutorials, and the classes all have great descriptions and examples per function...
 
  • #6
wow, tnks for all the reply will try that.
 
  • #7
In the Borland compiler you can include conio.h and then you can use gotoxy like pascal.
 
  • #8
you might need to use a flush on cout too.
 

1. What is Gotoxy in C++?

Gotoxy is a C++ function that allows you to set the cursor to a specific position on the screen. This is useful when creating text-based games or programs that require a specific layout.

2. How do I use Gotoxy in my C++ code?

To use Gotoxy, you need to include the header file #include <windows.h> in your code. Then, you can call the function gotoxy(x,y) to set the cursor to the desired position, where x and y are the coordinates of the position you want to move the cursor to.

3. Can Gotoxy be used in any C++ compiler?

Yes, Gotoxy is a standard C++ function and can be used in any C++ compiler. However, it may not work on all operating systems as it relies on the windows.h header file.

4. What happens if I use Gotoxy outside the boundaries of the screen?

If you use Gotoxy to set the cursor outside the boundaries of the screen, it will not display an error but it may cause unexpected behavior in your program. It is important to ensure that the coordinates you pass to Gotoxy are within the screen boundaries.

5. Are there any alternatives to using Gotoxy in C++?

Yes, there are other ways to achieve similar results as Gotoxy in C++. For example, you can use the SetConsoleCursorPosition function from the windows.h header file. Additionally, some graphics libraries like ncurses also provide similar functions for positioning the cursor on the screen.

Similar threads

  • Programming and Computer Science
Replies
10
Views
1K
  • Programming and Computer Science
Replies
24
Views
1K
  • Programming and Computer Science
Replies
11
Views
1K
  • Programming and Computer Science
Replies
18
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
3K
  • Programming and Computer Science
Replies
12
Views
2K
  • DIY Projects
Replies
10
Views
229
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
7
Views
1K
  • Programming and Computer Science
Replies
16
Views
1K
Back
Top