Thread: Switch statement menu

  1. #1
    Registered User
    Join Date
    Sep 2013
    Posts
    13

    Switch statement menu

    Hello, I was hoping that I could get some help in finding out how to re-prompt a user with a switch statement menu that I prompted them earlier with. There are 4 options and after the user chooses an option the menu is supposed to pop up again unless the user chooses option 4, how do I do this? For reference I'll put up my source code.

    Code:
    
    
    
    int main(){
    int init_bal,choice,balance,investment, donation;
    
    
    
    
    printf("Welcome!\n");
    printf("What is the initial balance of the fund?");
    scanf("%d",&init_bal);
    printf("What would you like to do?\n");
    
    
    printf("1 - Make a donation\n");
    printf("2 - Make an investment\n");
    printf("3 - Print balance of fund\n");
    printf("4 - Quit\n");
    scanf("%d",&choice);
    
    
    
    
    switch (choice) {
        case 1: printf("How much you like to donate?");
            scanf("%d",&donation);
            balance = donation+init_bal;
    
    
        case 2: printf("How much would you like to invest?");
            scanf("%d", &investment);
            if (balance - investment < init_bal)
            printf("You cannot make an investment of that amount");
            else balance = balance - investment;
    
    
    
    
    
    
    }
    
    
    }

  2. #2
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    555
    Have you learned loops yet? while and for, and do while() especially for this situation?

  3. #3
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    555
    Since you provided some code, let me give you a little structure suggestion here.

    Here is how I would do it, with pseudo code:

    Code:
    enum tasks displayMenu()
    {
      enum tasks choice;
      /* printfs to display menu and read input and return the choice made by the user */
    
      return choice;
    }
    
    void handleChoice(enum tasks choice)
    {
      switch(choice) {
        case CHOICE_1:
           /* do stuff for task 1 .. */
           break;
       /* and so on */
      }
    }
    
    int main(void) {
      enum tasks choice;
    
      while (  (choice = displayMenu()) != QUIT)
        handleChoice(choice);
    
      return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Switch Case menu problem????
    By Jason Singh in forum C Programming
    Replies: 2
    Last Post: 10-23-2011, 09:01 AM
  2. Menu Switch Problem
    By software tester in forum Game Programming
    Replies: 2
    Last Post: 02-02-2011, 09:20 AM
  3. Return to menu after switch executes
    By wco5002 in forum C++ Programming
    Replies: 5
    Last Post: 07-06-2008, 10:46 PM
  4. menu to switch between OS's
    By DavidP in forum Tech Board
    Replies: 3
    Last Post: 12-22-2002, 02:24 AM
  5. Menu System using switch and case.
    By Spectrum48k in forum C Programming
    Replies: 3
    Last Post: 07-23-2002, 08:23 AM

Tags for this Thread