12

Here I have a very simple program:

 printf("Enter your number in the box below\n");
 scanf("%d",&number);

Now, I would like the output to look like this:

 Enter your number in the box below
 +-----------------+
 | |*|             |
 +-----------------+

Where, |*| is the blinking cursor where the user types their value.

Since C is a linear code, it won't print the box art, then ask for the output, it will print the top row and the left column, then after the input print the bottom row and right column.

So, my question is, could I possibly print the box first, then have a function take the cursor back into the box?

Jørgen R
  • 10,568
  • 7
  • 42
  • 59
It'sRainingMen
  • 173
  • 1
  • 1
  • 10
  • @SouravGhosh Alright, Does this have a relative displacement rather than an absolute? – It'sRainingMen Oct 17 '14 at 11:12
  • 1
    This is not possible in standard C99. On some operating systems, you could use some libraries like [ncurses](http://www.gnu.org/software/ncurses/) or [readline](http://www.gnu.org/software/readline) – Basile Starynkevitch Oct 17 '14 at 11:14

4 Answers4

36

If you are under some Unix terminal (xterm, gnome-terminal ...), you can use console codes:

#include <stdio.h>

#define clear() printf("\033[H\033[J")
#define gotoxy(x,y) printf("\033[%d;%dH", (y), (x))

int main(void)
{
    int number;

    clear();
    printf(
        "Enter your number in the box below\n"
        "+-----------------+\n"
        "|                 |\n"
        "+-----------------+\n"
    );
    gotoxy(2, 3);
    scanf("%d", &number);
    return 0;
}

Or using Box-drawing characters:

printf(
    "Enter your number in the box below\n"
    "╔═════════════════╗\n"
    "║                 ║\n"
    "╚═════════════════╝\n"
);

More info:

man console_codes
David Ranieri
  • 39,972
  • 7
  • 52
  • 94
  • 4
    Note that ```#define gotoxy(x,y) printf("\033[%d;%dH", (x), (y))``` should actually be ```#define gotoxy(x,y) printf("\033[%d;%dH", (y), (x))```. You switched the x and the y! – travisjayday Jun 11 '19 at 23:37
18

In the linux terminal you may use terminal commands to move your cursor, such as

printf("\033[8;5Hhello"); // Move to (8, 5) and output hello

other similar commands:

printf("\033[XA"); // Move up X lines;
printf("\033[XB"); // Move down X lines;
printf("\033[XC"); // Move right X column;
printf("\033[XD"); // Move left X column;
printf("\033[2J"); // Clear screen

Keep in mind that this is not a standardised solution, and therefore your code will not be platform independent.

Cantfindname
  • 2,008
  • 1
  • 17
  • 30
  • 2
    +1 This was a great answer, it went above and beyond by also explaining other commands that could change the cursor. I think this should be the answer because of how fast and short yet useful it was. – Nfagie Yansaneh Aug 08 '17 at 05:31
  • Note that in your first statement (8, 5) -> row 8, column 5! Took me a minute to figure out why my program wasn't working as expected... Thanks anyway! +1 – travisjayday Jun 11 '19 at 23:36
1

The C language itself doesn't have any notion of a screen with a cursor. You'll have to use some kind of library that provides this support. is the most well-known and widely available library for terminal control.

luser droog
  • 18,988
  • 3
  • 53
  • 105
  • The language itself, indeed, but most consoles have various features to that end. And `curses` uses escape sequences just like others have exposed. – Alexis Wilke Jun 25 '19 at 06:04
1
#include <conio.h>

void main() {
    char d;
    cprintf("*---------*\n\r|         |\n\r*---------*");
    gotoxy(4,2);
    scanf("%c",&d);
}

this is for all os

ali naderi
  • 19
  • 2