How can I print a string to the console at specific coordinates in C++?
Asked Answered
W

7

12

I'm trying to print characters in the console at specified coordinates. Up to now I have been using the very ugly printf("\033[%d;%dH%s\n", 2, 2, "str"); But I just had to ask whether C++ had any other way of doing this. The problem is not even that it's ugly, the problem comes up when I try to make myself a prettier function like so:

void printToCoordinates(int x, int y, string text)
{
    printf("\033[%d;%dH%s\n", x, x, text);
}

It doesn't work, even if I typecast to (char*). Another problem is that I have to print out the \n for the page to be refreshed... I just don't enjoy using printf in general.

Similarily to using cout instead of printf, I believe there should be a more recent way of doing this (ideally a way that allows me to easily write strings where I want on the screen, and ideally a way that doesn't required these weird symbols: \033[%d;%dH)

So, do any of you have what I'm looking for?

Wales answered 3/11, 2009 at 23:46 Comment(2)
What platform are you working on?Hypodermis
Ubuntu 9.04, I'm using g++ to compileWales
B
7

What you are doing is using some very terminal specific magic characters in an otherwise pure C++ application. While this works, you will probably have a far easier time using a library which abstracts you from having to deal with terminal specific implementation details and provides functions that do what you need.

Investigate whether curses or ncurses libraries are available for your system.

Bainmarie answered 3/11, 2009 at 23:50 Comment(0)
N
15

Curses is what you are looking for.

Nasty answered 3/11, 2009 at 23:48 Comment(1)
I can't find what to include to have access to curses. Nor can I find any documentation for the API..Wales
H
14

I remember using gotoxy(x,y) in Turbo C++ (conio.h) - don't know if it'll work for you though. It moves the cursor to the coordinates specified by x and y.

EDIT: If you're using Windows, here's a gotoxy clone:

#include <windows.h>

void gotoxy(int x, int y)
{
  COORD coord;
  coord.X = x;
  coord.Y = y;
  SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}
Hypodermis answered 3/11, 2009 at 23:51 Comment(0)
B
7

What you are doing is using some very terminal specific magic characters in an otherwise pure C++ application. While this works, you will probably have a far easier time using a library which abstracts you from having to deal with terminal specific implementation details and provides functions that do what you need.

Investigate whether curses or ncurses libraries are available for your system.

Bainmarie answered 3/11, 2009 at 23:50 Comment(0)
D
6

A few improvements to your function:

void printToCoordinates(int x, int y, const char *format, ...)
{
    va_list args;
    va_start(args, format);
    printf("\033[%d;%dH", x, y);
    vprintf(format, args);
    va_end(args);
    fflush(stdout);
}

This version:

  • allows you to use any arbitrary format string and variable argument lists
  • automatically flushes stdout without printing a newline
  • uses x and y in the format string (your use of x and x may have been a typo)

However, because varargs is essentially a C feature and doesn't really understand C++ objects, you'd have to call it like this:

printToCoordinates(10, 10, "%s", text.c_str());

A better option really is to use curses (for Unix-like platforms) or Win32 console functions (for Windows) as mentioned in other answers.

Dactylology answered 3/11, 2009 at 23:51 Comment(2)
what does the fflush(stdout); part do?Wales
Normally stdout is "buffered" which means the C runtime library queues up what you print and only sends it to the console when (a) you output a newline, (b) the queue fills up, or (c) you manually flush the file. Using fflush() in this case is more straightforward and obvious than printing a newline (and it will prevent scrolling problems if you're trying to print stuff on the bottom line of the screen).Dactylology
I
4

First:

void printToCoordinates(int x, int y, string text)
{
    printf("\033[%d;%dH%s\n", x, x, text);
}

You don't want to copy the string argument, you want to pass it by (const) reference. Also, the (only) right way to get a char* from a std::string is to call its c_str() member function:

void printToCoordinates(int x, int y, const std::string& text)
{
    printf("\033[%d;%dH%s\n", x, x, text.c_str());
}

As to your question: C++ has no way to do what you want, but it allows you to use platform-specific ways to do it. You would have to tell us your platform in order to get meaningful answers.

Inescutcheon answered 3/11, 2009 at 23:53 Comment(1)
Should be printf(...., x, y, text); not printf(...,x,x,text) .. cannot edit above.Outspeak
M
0
void screenpos(int x,int y,char textyowanawrite[20])
{
//printf for right shift
// \n for downward shift
//loops through the rows and shifts down 
for(int row=0;row<=y;row++)
{
printf("\n");
for (int i = 0; i < x; i++)
{
printf("%s "," " );
}
}
printf("%s ",textyowanawrite );
} 

//this should work to certain extinct only problem is u cant go from somewhere like 4,4 to 2,2 thats the problem

Monti answered 17/1, 2018 at 4:18 Comment(0)
K
-1

I have a little different method. Not sure whether this is better than ncurses package, so i leave that for the upvoters to decide.

You can use Graphics package in C++ to output a text to a specific coordinate on your working screen. The syntax is outtextxy(x, y, text) ; Where x & y are coordinates.

One example is:

int main(void) {

    int gdriver = DETECT, gmode;

    int x = 200, y = 200;

  

    initgraph(&gdriver, &gmode, "c:\\tc\\bgi");

  

    outtextxy(x, y, "Hello World");

    closegraph();

 }

This little program will print Hello World in the coordinate (200,200).

For reference to what graphics package can do visit this link

Kasper answered 17/1, 2018 at 4:42 Comment(1)
The graphics package is non-standard, and probably unavailable on Ubuntu, the OP's plateformlAppellant

© 2022 - 2024 — McMap. All rights reserved.