Issue
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?
Solution
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
Answered By - David Ranieri Answer Checked By - Katrina (PHPFixing Volunteer)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.