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?
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. curses is the most well-known and widely available library for terminal control.
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:
Keep in mind that this is not a standardised solution, and therefore your code will not be platform independent.
If you are under some Unix terminal (
xterm
,gnome-terminal
...), you can use console codes:Or using Box-drawing characters:
More info: