#I'm making an interactive menu but i dont know how to fix this

9 messages · Page 1 of 1 (latest)

full stone
#
#include <stdio.h>
#pragma warning(disable : 4996)
#include <stdbool.h>
int option_choose = 5;
bool control_state = true;

int on = 1;

int main() {
    while (true) {

        if (option_choose == 5) {
            system("cls");
            printf("Rezervari hotel meniu\n");

            printf("[1] Optiunea 1\n");
            printf("[2] Optiunea 2\n");
            printf("[3] Optiunea 3\n");
            printf("[4] Optiunea 4\n");

            scanf("%d", &option_choose);
        }
       

        else if (option_choose == 1)) {
            system("cls");
            printf("Optiunea 1\nApasati [5] ca sa va intoarceti inapoi la meniul principal.\n");
            on = 0;

            scanf("%d", &option_choose);
        }
        else if (option_choose == 2) {
            system("cls");
            printf("Optiunea 2\nApasati [5] ca sa va intoarceti inapoi la meniul principal.\n");

            scanf("%d", &option_choose);
            

        }
        else if (option_choose == 3) {
            system("cls");
            printf("Optiunea 3\nApasati [5] ca sa va intoarceti inapoi la meniul principal.\n");

            scanf("%d", &option_choose);


        }
        else if (option_choose == 4) {
            system("cls");
            printf("Optiunea 4\nApasati [5] ca sa va intoarceti inapoi la meniul principal.\n");

            scanf("%d", &option_choose);


        }
        else if (option_choose == 5) {
            system("cls");
            printf("Optiunea 5\nApasati [5] ca sa va intoarceti inapoi la meniul principal.\n");

            scanf("%d", &option_choose);


        }


    }
    

    return 0;
}

My interactive menu is half finished because I don't know how to stop the user from inputting another number in a submenu (optiunea 1, optiunea 2, etc). I want to only press 5 but i don't know how to implement this. I wanted to do this alone but i feel this might be a chance to get some code review. Thanks

swift saddleBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

pulsar tulip
#

if i understand you correctly you want to validate user input? (i.e. you want to accept a specific range of numbers?). if thats the case simply loop user the input requesting it to be sent again as long as its wrong. consider:

int input = some_invalid_input;
while(input != ...) { // or while(input < ... || input > ...)
  if(scanf("%d", &input) != 1) { ... }
  // request another input
}```
you can rewrite that loop as you see fit
full stone
#

if(scanf("%d", &input) != 1) this is nice

#

Hold on im gonna change my code

#

oh it works

#
else if (option_choose == 1) {
    system("cls");
    printf("Optiunea 1\nApasati [5] ca sa va intoarceti inapoi la meniul principal.\n");
    on = 0;

    while (option_choose != 5){
        scanf("%d", &option_choose);
    }
}
#

nice

#

!solved