📂 Archivo técnico
Este contenido lo publiqué en un blog anterior durante mis primeros proyectos. Hoy lo conservo aquí como referencia y ejemplo práctico para quienes quieran reforzar la lógica de programación.
Contenido
ToggleMenú de opciones con teclas direccionales
Para esta entrada sobre Menú de opciones con teclas direccionales en C++, pondré tres versiones diferentes. La explicación completa lo encontrarás en los videos de youtube que pongo más adelante.
- Utilizando la función getch2 definida mediante la API de Windows
- Utilizando el IDE Dev c++ 4.9.9.2 con la librería conio2
- Utilizando Borland C++
CAPTURAS DE PANTALLA DEL PROGRAMA
VIDEOTUTORIALES CON EXPLICACIÓN COMPLETA
Te recomiendo que sigas los tutoriales para entiendas mejor el código y puedas compilarlo de forma correcta.
Utilizando la función getch2 definida mediante la API de Windows
// Se usa la función getch implementada mediante la API de Windows
// Pude encontrar una función getch que sí funciona, ya lo puse :D
// Solo las teclas i k para arrib y abajo
#include
#include
/*
#define ARRIBA 72
#define ABAJO 80
#define ENTER 13
*/
#define ARRIBA 'i'
#define ABAJO 'k'
#define ENTER 13
/*
#define ARRIBA 1
#define ABAJO 0
#define ENTER 13
#define INVALIDO -1
*/
using namespace std;
// API DE WINDOWS
// Lo bajé de aquí: https://w...content-available-to-author-only...a.org/foro/viewtopic.php?f=6&t=6086
char getch2 ()
{
char c=0;
DWORD modo, contador;
HANDLE ih = GetStdHandle(STD_INPUT_HANDLE);
// Desactivamos escritura en terminal
SetConsoleMode (ih, modo & ~(ENABLE_ECHO_INPUT | ENABLE_LINE_INPUT));
ReadConsoleA (ih, &c, 1, &contador, NULL);
if (c == 0) {
ReadConsoleA (ih, &c, 1, &contador, NULL);
}
SetConsoleMode (ih, modo); // Devolvemos control normal
return c;
}
/* CON ESTA NO FUNCIONÓ
int getch(void)
{
int car;
DWORD leidos, modo;
GetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), &modo);
SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), modo & !ENABLE_ECHO_INPUT & !ENABLE_PROCESSED_INPUT);
ReadConsole(GetStdHandle(STD_INPUT_HANDLE), &car, 1, &leidos, NULL);
SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), modo);
return car;
}*/
int gotoxy(USHORT x,USHORT y) {
COORD cp = {x,y};
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), cp);
}
int menu(const char titulo[], const char *opciones[], int n);
void menu_principal();
void menu_suma();
void menu_resta();
void menu_producto();
void menu_division();
int main()
{
menu_principal();
return 0;
}
void menu_principal()
{
bool repite = true;
int opcion;
// Titulo del menú y nombres de las opciones
const char *titulo = "MENU DE OPCIONES";
const char *opciones[] = {"Sumar", "Restar", "Multiplicar", "Dividir", "Salir"};
int n = 5; // Numero de opciones
do {
opcion = menu(titulo, opciones, n);
switch (opcion) {
case 1:
menu_suma();
break;
case 2:
menu_resta();
break;
case 3:
menu_producto();
break;
case 4:
menu_division();
break;
case 5:
cout << "\nEl programa se cerrara!! ADIOS" << endl;
repite = false;
system("pause>nul");
break;
}
} while(repite);
}
void menu_suma()
{
bool repite = true;
int opcion;
// Titulo del menú y nombres de las opciones
const char *titulo = "MENU PARA SUMAR";
const char *opciones[] = {"Sumar enteros", "Sumar flotantes", "Regresar"};
int n = 3; // Numero de opciones
int a, b;
float c, d;
do {
opcion = menu(titulo, opciones, n);
system("cls");
switch (opcion) {
case 1:
cout << "numero1 = ";
cin >> a;
cout << "numero2 = ";
cin >> b;
cout << a << " + " << b << " = " << a + b << endl;
system("pause>nul");
break;
case 2:
cout << "numero1 = ";
cin >> c;
cout << "numero2 = ";
cin >> d;
cout << c << " + " << d << " = " << c + d << endl;
system("pause>nul");
break;
case 3:
repite = false;
break;
}
} while(repite);
}
void menu_resta()
{
bool repite = true;
int opcion;
// Titulo del menú y nombres de las opciones
const char *titulo = "MENU PARA RESTAR";
const char *opciones[] = {"Restar enteros", "Restar flotantes", "Regresar"};
int n = 3; // Numero de opciones
int a, b;
float c, d;
do {
opcion = menu(titulo, opciones, n);
system("cls");
switch (opcion) {
case 1:
cout << "numero1 = ";
cin >> a;
cout << "numero2 = ";
cin >> b;
cout << a << " - " << b << " = " << a - b << endl;
system("pause>nul");
break;
case 2:
cout << "numero1 = ";
cin >> c;
cout << "numero2 = ";
cin >> d;
cout << c << " - " << d << " = " << c - d << endl;
system("pause>nul");
break;
case 3:
repite = false;
break;
}
} while(repite);
}
void menu_producto()
{
bool repite = true;
int opcion;
// Titulo del menú y nombres de las opciones
const char *titulo = "MENU PARA MULTIPLICAR";
const char *opciones[] = {"Multiplicar enteros", "Multiplicar flotantes", "Regresar"};
int n = 3; // Numero de opciones
int a, b;
float c, d;
do {
opcion = menu(titulo, opciones, n);
system("cls");
switch (opcion) {
case 1:
cout << "numero1 = ";
cin >> a;
cout << "numero2 = ";
cin >> b;
cout << a << " x " << b << " = " << a * b << endl;
system("pause>nul");
break;
case 2:
cout << "numero1 = ";
cin >> c;
cout << "numero2 = ";
cin >> d;
cout << c << " x " << d << " = " << c * d << endl;
system("pause>nul");
break;
case 3:
repite = false;
break;
}
} while(repite);
}
void menu_division()
{
bool repite = true;
int opcion;
// Titulo del menú y nombres de las opciones
const char *titulo = "MENU PARA DIVIDIR";
const char *opciones[] = {"Dividir enteros", "Dividir flotantes", "Regresar"};
int n = 3; // Numero de opciones
int a, b;
float c, d;
do {
opcion = menu(titulo, opciones, n);
system("cls");
switch (opcion) {
case 1:
cout << "numero1 = ";
cin >> a;
cout << "numero2 = ";
cin >> b;
if (b == 0)
cout << "No se puede dividir por cero" << endl;
else
cout << a << " div " << b << " = " << a / b << endl;
system("pause>nul");
break;
case 2:
cout << "numero1 = ";
cin >> c;
cout << "numero2 = ";
cin >> d;
if (d == 0)
cout << "No se puede dividir por cero" << endl;
else
cout << c << " / " << d << " = " << c / d << endl;
system("pause>nul");
break;
case 3:
repite = false;
break;
}
} while(repite);
}
int menu(const char titulo[], const char *opciones[], int n)
{
int opcionSeleccionada = 1; // Indica la opcionSeleccionada
int tecla;
bool repite = true; // controla el bucle para regresar a la rutina que lo llamo, al presionar ENTER
do {
system("cls");
system("color 1e");
gotoxy(5, 3 + opcionSeleccionada); cout << "==>" << endl;
// Imprime el título del menú
gotoxy(15, 2); cout << titulo;
// Imprime las opciones del menú
for (int i = 0; i < n; ++i) {
gotoxy(10, 4 + i); cout << i + 1 << ") " << opciones[i];
}
// Solo permite que se ingrese ARRIBA, ABAJO o ENTER
do {
tecla = getch2();
// gotoxy(15, 15); cout << "tecla presionada: " << (char)tecla << " = " << tecla;
} while (tecla != ARRIBA && tecla != ABAJO && tecla != ENTER);
switch (tecla) {
case ARRIBA: // En caso que se presione ARRIBA
opcionSeleccionada--;
if (opcionSeleccionada < 1) {
opcionSeleccionada = n;
}
break;
case ABAJO:
opcionSeleccionada++;
if (opcionSeleccionada > n) {
opcionSeleccionada = 1;
}
break;
case ENTER:
repite = false;
break;
}
} while (repite);
return opcionSeleccionada;
}
Utilizando el IDE Dev c++ 4.9.9.2 con la librería conio2
#include
#include
#include
#define TECLA_ARRIBA 72
#define TECLA_ABAJO 80
#define ENTER 13
using namespace std;
void menu_principal();
void menu_suma();
void menu_resta();
void menu_multiplicacion();
void menu_division();
int menu(const char *titulo, const char *opciones[], int n);
int main()
{
menu_principal();
return 0;
}
void menu_principal()
{
bool repite = true;
int opcion;
// Título y las opciones del menú
const char *titulo = "MENU PRINCIPAL";
const char *opciones[] = {"Sumar", "Restar", "Multiplicar", "Dividir", "Salir"};
int n = 5; // Número de opciones
do {
opcion = menu(titulo, opciones, n);
// Alternativas
switch (opcion) {
case 1:
menu_suma();
break;
case 2:
menu_resta();
break;
case 3:
menu_multiplicacion();
break;
case 4:
menu_division();
break;
case 5:
repite = false;
break;
}
} while (repite);
}
void menu_suma()
{
bool repite = true;
int opcion;
int a, b;
float c, d;
const char *titulo = "MENU SUMA";
const char *opciones[] = {"Sumar enteros", "Sumar flotantes", "Regresar"};
int n = 3;
do {
opcion = menu(titulo, opciones, n);
// Alternativas
system("cls");
switch (opcion) {
case 1:
cout << "numero1: ";
cin >> a;
cout << "numero2: ";
cin >> b;
cout << "\n" << a << " + " << b << " = " << a + b << endl;
system("pause>nul");
break;
case 2:
cout << "numero1: ";
cin >> c;
cout << "numero2: ";
cin >> d;
cout << "\n" << c << " + " << d << " = " << c + d << endl;
system("pause>nul");
break;
case 3:
repite = false;
break;
}
} while (repite);
}
void menu_resta()
{
bool repite = true;
int opcion;
int a, b;
float c, d;
const char *titulo = "MENU RESTA";
const char *opciones[] = {"Restar enteros", "Restar flotantes", "Regresar"};
int n = 3;
do {
opcion = menu(titulo, opciones, n);
// Alternativas
system("cls");
switch (opcion) {
case 1:
cout << "numero1: ";
cin >> a;
cout << "numero2: ";
cin >> b;
cout << "\n" << a << " - " << b << " = " << a - b << endl;
system("pause>nul");
break;
case 2:
cout << "numero1: ";
cin >> c;
cout << "numero2: ";
cin >> d;
cout << "\n" << c << " - " << d << " = " << c - d << endl;
system("pause>nul");
break;
case 3:
repite = false;
break;
}
} while (repite);
}
void menu_multiplicacion()
{
bool repite = true;
int opcion;
int a, b;
float c, d;
const char *titulo = "MENU MULTIPLICACION";
const char *opciones[] = {"Multiplicar enteros", "Multiplicar flotantes", "Regresar"};
int n = 3;
do {
opcion = menu(titulo, opciones, n);
// Alternativas
system("cls");
switch (opcion) {
case 1:
cout << "numero1: ";
cin >> a;
cout << "numero2: ";
cin >> b;
cout << "\n" << a << " x " << b << " = " << a * b << endl;
system("pause>nul");
break;
case 2:
cout << "numero1: ";
cin >> c;
cout << "numero2: ";
cin >> d;
cout << "\n" << c << " x " << d << " = " << c * d << endl;
system("pause>nul");
break;
case 3:
repite = false;
break;
}
} while (repite);
}
void menu_division()
{
bool repite = true;
int opcion;
int a, b;
float c, d;
const char *titulo = "MENU DIVISION";
const char *opciones[] = {"Dividir enteros", "Dividir flotantes", "Regresar"};
int n = 3;
do {
opcion = menu(titulo, opciones, n);
// Alternativas
system("cls");
switch (opcion) {
case 1:
cout << "numero1: ";
cin >> a;
cout << "numero2: ";
cin >> b;
if (b == 0) {
cout << "\nNo se puede dividir por cero" << endl;
} else {
cout << "\n" << a << " / " << b << " = " << a / b << endl;
}
system("pause>nul");
break;
case 2:
cout << "numero1: ";
cin >> c;
cout << "numero2: ";
cin >> d;
if (d == 0) {
cout << "\nNo se puede dividir por cero" << endl;
} else {
cout << "\n" << c << " / " << d << " = " << c / d << endl;
}
system("pause>nul");
break;
case 3:
repite = false;
break;
}
} while (repite);
}
int menu(const char *titulo, const char *opciones[], int n)
{
int opcionSeleccionada = 1;
int tecla;
bool repite = true;
do {
system("cls");
gotoxy(5, 3 + opcionSeleccionada); cout << "==>";
// Imprime el título
gotoxy(15, 2); cout << titulo;
// Imprime las opciones
for (int i = 0; i < n; i++) {
gotoxy(10, 4 + i); cout << i + 1 << ") " << opciones[i];
}
do {
tecla = getch();
} while (tecla != TECLA_ARRIBA && tecla != TECLA_ABAJO && tecla != ENTER);
switch (tecla) {
case TECLA_ARRIBA:
opcionSeleccionada--;
if (opcionSeleccionada < 1) {
opcionSeleccionada = n;
}
break;
case TECLA_ABAJO:
opcionSeleccionada++;
if (opcionSeleccionada > n) {
opcionSeleccionada = 1;
}
break;
case ENTER:
repite = false;
break;
}
} while (repite);
return opcionSeleccionada;
}
Utilizando Borland C++
#include
#include
#define ARRIBA 72
#define ABAJO 80
#define ENTER 13
/*
#define ARRIBA 'i'
#define ABAJO 'k'
#define ENTER ' '
*/
int menu(const char titulo[], const char *opciones[], int n);
int main()
{
bool repite = true;
int opcion;
// Titulo del menú y nombres de las opciones
const char *titulo = "MENU DE OPCIONES";
const char *opciones[] = {"Sumar", "Restar", "Multiplicar", "Dividir", "Salir"};
int n = 5; // Numero de opciones
do {
clrscr();
opcion = menu(titulo, opciones, n);
switch (opcion) {
case 1:
cout << "\nElegiste la opcion para SUMAR" << endl;
break;
case 2:
cout << "\nElegiste la opcion para RESTAR" << endl;
break;
case 3:
cout << "\nElegiste la opcion para MULTIPLICAR" << endl;
break;
case 4:
cout << "\nElegiste la opcion para DIVIDIR" << endl;
break;
case 5:
cout << "\nEl programa se cerrara!! ADIOS" << endl;
repite = false;
break;
}
cin.get();
} while(repite);
cin.get();
return 0;
}
int menu(const char titulo[], const char *opciones[], int n)
{
int opcionSeleccionada = 1; // Indica la opcionSeleccionada
int tecla;
bool repite = true; // controla el bucle para regresar a la rutina que lo llamo, al presionar ENTER
do {
clrscr();
gotoxy(5, 2 + opcionSeleccionada); cout << "==>" << endl;
gotoxy(15, 2); cout << titulo;
for (int i = 0; i < n; ++i) {
gotoxy(10, 3 + i); cout << (i + 1) << ") " << opciones[i];
}
// Solo permite que se ingrese ARRIBA, ABAJO o ENTER
do {
tecla = getch();
// clreol(); gotoxy(15, 15); cout << "tecla presionada: " << (char)tecla << " = " << tecla;
} while (tecla != ARRIBA && tecla != ABAJO && tecla != ENTER);
switch (tecla) {
case ARRIBA: // En caso que se presione ARRIBA
opcionSeleccionada--;
if (opcionSeleccionada < 1) {
opcionSeleccionada = n;
}
break;
case ABAJO:
opcionSeleccionada++;
if (opcionSeleccionada > n) {
opcionSeleccionada = 1;
}
break;
case ENTER:
repite = false;
break;
}
} while (repite);
return opcionSeleccionada;
}
view raw

