commit cb52cd91ed1e07a9eb328febe208b219a3b31c87
parent 03f1278ee3102e803e1f416613499f2a2feef478
Author: mjkloeckner <martinjkloeckner@gmail.com>
Date: Fri, 15 Mar 2024 13:28:31 -0300
Merge branch 'main' of github.com:mjkloeckner/CB100
Diffstat:
1 file changed, 31 insertions(+), 14 deletions(-)
diff --git a/solutions/ej11.cpp b/solutions/ej11.cpp
@@ -9,14 +9,20 @@
using namespace std;
-const unsigned int menu_items = 4;
-static struct termios default_attributes;
+enum {
+ ES,
+ EN,
+ DE,
+ PT,
+ MENU_ITEMS
+};
-string *cursor = new string[menu_items];
+static struct termios default_attributes;
+string *cursor = new string[MENU_ITEMS];
unsigned int cursor_pos;
bool run_program;
-string menu[menu_items] = {
+string menu[MENU_ITEMS] = {
"Español",
"English",
"Deutsch",
@@ -42,7 +48,7 @@ void set_input_mode (void) {
tcgetattr(STDIN_FILENO, &default_attributes);
/* hide cursor */
- printf("\033[?25l");
+ cout << "\033[?25l";
/* Set the terminal modes. */
tcgetattr(STDIN_FILENO, &tattr);
@@ -56,19 +62,19 @@ void reset_input_mode (void) {
tcsetattr(STDIN_FILENO, TCSANOW, &default_attributes);
/* show cursor */
- printf("\033[?25h");
+ cout << "\033[?25h";
}
void print_menu(void) {
- for(size_t i = 0; i < menu_items; ++i) {
+ for(size_t i = 0; i < MENU_ITEMS; ++i) {
cursor[cursor_pos].assign(((i == cursor_pos) ? " >\033[33m" : " "));
cout << cursor[cursor_pos] << menu[i]
- << (i == menu_items ? "\033[0m" : "\033[0m\n") << flush;
+ << (i == MENU_ITEMS ? "\033[0m" : "\033[0m\n") << flush;
}
}
void clear_menu(void) {
- cout << "\033[" << menu_items << "A";
+ cout << "\033[" << MENU_ITEMS << "A";
}
void refresh_menu(void) {
@@ -79,11 +85,11 @@ void refresh_menu(void) {
void move_cursor_up() {
if(cursor_pos >= 1)
cursor_pos--;
- else cursor_pos = (menu_items - 1);
+ else cursor_pos = (MENU_ITEMS - 1);
}
void move_cursor_down() {
- if(cursor_pos < (menu_items - 1))
+ if(cursor_pos < (MENU_ITEMS - 1))
cursor_pos++;
else cursor_pos = 0;
}
@@ -93,7 +99,7 @@ int main (void) {
unsigned int selected_item;
run_program = true;
cursor_pos = 0;
- selected_item = menu_items;
+ selected_item = MENU_ITEMS;
set_input_mode();
signal(SIGINT, sig_handler);
@@ -149,9 +155,20 @@ int main (void) {
}
}
+ string mg;
clear_menu();
- if(selected_item != menu_items)
- cout << "You selected: `" << menu[selected_item] << "`" << endl;
+ if(selected_item != MENU_ITEMS) {
+ if (menu[selected_item] == menu[ES]) {
+ mg = "Usted seleccionó: `";
+ } else if (menu[selected_item] == menu[DE]) {
+ mg = "Sie wählen: `";
+ } else if (menu[selected_item] == menu[PT]) {
+ mg = "Você seleciona: `";
+ } else {
+ mg = "You selected: `";
+ }
+ cout << mg << menu[selected_item] << "`" << endl;
+ }
exit(EXIT_SUCCESS);
}