DMissile
DMissile

Reputation: 13

Making a menu inside a menu

I am trying to make a menu within a menu. My problem is that the value of "choice" is not changed and thus the program stops when I am trying to change the value of choice, e.g. when I entered (2) as my first choice, then I enter (0), and when I enter (1), the program just terminates.

#include <stdio.h>

int choice, choiceJR;

void mainMenu() {
  printf("Select one of the following. \n");  
  printf("1. x \n");
  printf("2. menuJR \n");
  printf("3. xxx \n");

  printf("Choice: \n");
  scanf("%d", &choice);
}

void menuJR() {
  printf("Select one of the following. \n");
  printf("1. y \n");
  printf("2. yy \n");
  printf("0. go back \n");

  printf("Choice: \n");
  scanf("%d", &choiceJR);

}

int main() {

  mainMenu();

  while(choice != 1 && choice!= 2 && choice!= 3) {
    printf("Invalid choice! \n");
    mainMenu();
  }
  if(choice == 1) {
    printf("You have selected 1 \n");
  }
  else if(choice == 2) {
    printf("You have selected 2 \n");
    menuJR();
    while(choiceJR != 0) {
      menuJR();
    }
    if(choiceJR == 0) {
      printf("Going to menu! \n");
      mainMenu();
    }
  }
  else if(choice == 3) {
    printf("You have selected 3 \n");
  }


  return 0;
}

Upvotes: 0

Views: 369

Answers (1)

Anup Agrawal
Anup Agrawal

Reputation: 85

  • You should use do_while loop for choosing items.

  • Here is your solution, use this code.

    #include <stdio.h>
    
    int choiceJR=0;
    
    int mainMenu() {
        int choice=0;
        printf("Select one of the following. \n");  
        printf("1. x \n");
        printf("2. menuJR \n");
        printf("3. xxx \n");
    
        printf("Choice: \n");
        scanf("%d", &choice);
        return choice;
    }
    
    void menuJR() {
        printf("Select one of the following. \n");
        printf("1. y \n");
        printf("2. yy \n");
        printf("0. go back \n");
    
        scanf("%d", &choiceJR);
    }
    
    int main() {
        int ch=0;
    
        do {
            main:
            ch = mainMenu();
            while(ch != 1 && ch!= 2 && ch!= 3) {
                printf("Invalid choice! \n");
                mainMenu(ch);
            }
    
            switch(ch) {
                case 1:
                    printf("You have selected 1 \n");
                    break;
    
                case 2:
                    printf("You have selected 2 \n");
                    menuJR();
                    while(choiceJR != 0) {
                        menuJR();
                    }
                    if(choiceJR == 0) {
                        printf("Going to menu! \n");
                        goto main;
                    }
                    break;
    
                case 3:
                    printf("You have selected 3 \n");
                    break;
    
                default: 
                    printf("invalid choice \n");
            }
    
        }
        while(ch<=3);
    
        return 0;
    }
    

Upvotes: 1

Related Questions