pp fernandes pp fernandes - 1 month ago 9
C Question

GotoXY is not working as i expected

GotoXY is not working if i separate the functions it will work but i need them all in the same file.

#include "colors.h"
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#define clear() printf("\033[H\033[J")



void gotoXY(int x, int y) {
COORD coord;
coord.X = x;
coord.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}



void welcome() {
printf("%s", KRED);
printf(" _ __________ __________ __ _________\n");
printf(" | | / / ____/ / / ____/ __ \\/ |/ / ____/\n");
printf(" | | /| / / __/ / / / / / / / / /|_/ / __/ \n");
printf(" | |/ |/ / /___/ /___/ /___/ /_/ / / / / /___ \n");
printf(" |__/|__/_____/_____/\\____/\\____/_/ /_/_____/ \n");
printf(" \n");
printf("\n\n%s", KGRN);
sleep(3);
clear();
}

void menu_main_interface() {
clear();
printf("%s",KBLU);
printf(" ___ ___ ___ _____ _ _ ___ ___ _____ _ _ _ _ \n");
printf(" | \\/ | / _ \\|_ _| \\ | | | \\/ || ___| \\ | | | | |\n");
printf(" | . . |/ /_\\ \\ | | | \\| | | . . || |__ | \\| | | | |\n");
printf(" | |\\/| || _ | | | | . ` | | |\\/| || __|| . ` | | | |\n");
printf(" | | | || | | |_| |_| |\\ | | | | || |___| |\\ | |_| |\n");
printf(" \\_| |_/\\_| |_/\\___/\\_| \\_/ \\_| |_/\\____/\\_| \\_/\\___/ \n");
printf("%s",KGRN);
printf("\n\n ________________________________________________________\n |");
printf(" |\n");
printf(" | 1 ---> NEW GAME |\n");
printf(" | 2 ---> LOAD GAME |\n");
printf(" | 3 ---> TOP 10 TIMES |\n");
printf(" | |\n");
printf(" | 0 ---> EXIT |\n");
printf(" | ENTER OPTION: |\n");
printf(" |______________________________________________________|\n");
gotoXY(16, 25);
}


int main(int argc, char** argv) {

printf("%s", KGRN);
welcome();
start();

return (EXIT_SUCCESS);
}


And start() function only calls select_menu_interface() leading to:

void select_menu_interface() {
int selection;

do {
menu_main_interface();
readInt(&selection, 0, 3, "");
switch (selection) {
case 1: select_newgame(); break;
case 2: break;
case 3: readHS(); break;
case 0: break;
default: printf("ERROR: Bad Input!"); break;
}
} while (selection < 0 && selection > 3);

}


I want the cursor on "Enter Option:", but its doesn't work : the cursor is on the beginning of another line instead:

enter image description here

Answer

Solution (credits to @Christophe):

The readInt() function does a displayMessage() that changed the cursor position. We only needed to change it to a scanf() function to read the value:

void select_menu_interface() {
    int selection;

    do {
        menu_main_interface();
        scanf("%d", &selection);
        switch (selection) {
            case 1: select_newgame(); break;
            case 2: break;
            case 3: readHS(); break;
            case 0: break;
            default: printf("ERROR: Bad Input!"); break;
        }
    } while (selection < 0 && selection > 3);

}