printf("Enter your number in the box below\n");
scanf("%d",&number);
Ahora, me gustaría que la salida se viera así:
Enter your number in the box below
+-----------------+
| |*| |
+-----------------+
donde, |*| es el cursor parpadeante donde el usuario escribe su valor.
Dado que C es un código lineal, no imprimirá el arte de la caja, luego solicitará la salida, imprimirá la fila superior y la columna izquierda, luego, después de la entrada, imprimirá la fila inferior y la columna derecha.
Entonces, mi pregunta es, ¿podría imprimir el cuadro primero y luego hacer que una función devuelva el cursor al cuadro?
@SouravGhosh Muy bien, ¿tiene esto un desplazamiento relativo en lugar de absoluto?
– Está lloviendo hombres
17/10/2014 a las 11:12
Esto no es posible en el estándar C99. En algunos sistemas operativos, puede usar algunas bibliotecas como maldiciones o línea de lectura
– Basile Starynkevitch
17/10/2014 a las 11:14
david ranieri
Si está bajo algún terminal Unix (xterm, gnome-terminal …), puede usar códigos de consola:
#incluye #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;
}
printf(
"Enter your number in the box below\n"
"╔═════════════════╗\n"
"║ ║\n"
"╚═════════════════╝\n"
);
More info:
man console_codes
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, 2019 at 23:37
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.
+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 8, 2017 at 5: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, 2019 at 23:36
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.
The language itself, indeed, but most consoles have various features to that end. And curses uses escape sequences just like others have exposed.
Esta web utiliza cookies propias y de terceros para su correcto funcionamiento y para fines analíticos y para mostrarte publicidad relacionada con sus preferencias en base a un perfil elaborado a partir de tus hábitos de navegación. Al hacer clic en el botón Aceptar, acepta el uso de estas tecnologías y el procesamiento de tus datos para estos propósitos.
Configurar y más información
@SouravGhosh Muy bien, ¿tiene esto un desplazamiento relativo en lugar de absoluto?
– Está lloviendo hombres
17/10/2014 a las 11:12
Esto no es posible en el estándar C99. En algunos sistemas operativos, puede usar algunas bibliotecas como maldiciones o línea de lectura
– Basile Starynkevitch
17/10/2014 a las 11:14