Like Tree1Likes

indefinite switch board

This is a discussion on indefinite switch board within the C Programming forums, part of the General Programming Boards category; I have an assigment where im making a calendar, they have alot of options and theres 2 submenus. when they ...

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    10

    indefinite switch board

    I have an assigment where im making a calendar, they have alot of options and theres 2 submenus.

    when they are in a menu and they choose an option, it performs a task, after that the program closes, how do i get it to go back to the menu they was on.?

    please help me?

  2. #2
    Registered User
    Join Date
    Jan 2011
    Posts
    36
    do you have any code already or idea in the general direction which you want to go? if you do post what you have and i might be able to to help

  3. #3
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,832
    The menu part is inside a loop. Or it could be in a function which is called in a loop.

  4. #4
    Registered User
    Join Date
    Mar 2011
    Posts
    10
    Code:
    int main(void)  
    {
     
    int value=0;
     
     printf("\nChoose from the list of options:\n");
     printf("\n1.Calender Options");
     printf("\n2.Appointment Options");
     printf("\n3.Quit\n\n");
     printf("Option: ");
      scanf("%d", &value);
      
      switch(value)
      {
        case 1:
            subm_cal();
            break;
        case 2:
             subm_app();                                             
             break;                                                   
             
        case 3:                                                         // This Quits from the menu
             break;                                                     //
        default:                                                        // For when they type something not in range of 1-3
          printf("\nInvalid Choice\n\n");                               //
          
          
       }
      printf("\n\n");
      system("pause");
      return 0;
      
    } 
    //////////////////////////////////////////////////////////////////////////
    // Sub menus                                                              /
    //////////////////////////////////////////////////////////////////////////
    void subm_cal()
    {
    int value=0;
     
     printf("\nCalender Menu:\n");
     printf("\n1.Monthly Calender");
     printf("\n2.Yearly Calender");
     printf("\n3.Time Slots");
     printf("\n4.Return To Main Menu\n\n");
     printf("Option: ");
      scanf("%d", &value);
      
      switch(value)
      {
        case 1:
              int n, s;                                
       	     printf("\n\nEnter number of days in month:");              //
    	     scanf("%d", &n);                                           //
    	     printf("\n\nEnter the day for the first of month:");       // This Area is calling the function to make the calender
    	     scanf("%d", &s);                                           //
    	     makeCalendar(n,s);                                         //
             break;                                                    //
     
        case 2:
             cal_year();                          
             break;                                                     
             
        case 3:
             printf("Time Slots Here");
             break;                                           
             
         case 4:                                                         
             break;                                                  
             
        default:                                                        // For when they type something not in range of 1-3
          printf("\nInvalid Choice\n\n");                               //
       }
    
    }
    
    
    void subm_app()
    {
    int value=0;
     
     printf("\nAppointments Menu:\n");
     printf("\n1.Add Appointment");
     printf("\n2.Edit Appointment");
     printf("\n3.View / Load Appointments");
     printf("\n4.Return To Main Menu\n\n");
     printf("Option: ");
      scanf("%d", &value);
      
      switch(value)
      {
        case 1:
             appointment();                                       
             break;                                                    
     
        case 2:
             printf("Edit Appointments Here");                            
             break;                                                     
             
        case 3:
             ld_apps();
             break;                                           
             
         case 4:                                                       
             break;                                                  
             
        default:                                                        // For when they type something not in range of 1-3
          printf("\nInvalid Choice\n\n");                               //
          
          
       }
       
    }
    basically, when the switch menu option is choose, like 1, it performs a function, i just want the menu to come back up after they have done the function so they can carry on performing tasks instead of it closing and haviong to reopen it

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    2,776
    It looks like you could place your switch statements into a couple of loops.


    Jim

  6. #6
    Registered User
    Join Date
    Mar 2011
    Posts
    10
    what a loop for each switchboard? or like 1 for main, and one for my submenus?

  7. #7
    Registered User
    Join Date
    May 2010
    Posts
    2,776
    Sounds good, why don't you give it a try and see for yourself?

    Jim

  8. #8
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,832
    Code:
    while (1) {  /* loop forever */
     printf("\nChoose from the list of options:\n");
     printf("\n1.Calender Options");
     printf("\n2.Appointment Options");
     printf("\n3.Quit\n\n");
     printf("Option: ");
      scanf("%d", &value);
      
      switch(value)
      {
        case 1:
            subm_cal();
            break;
        case 2:
             subm_app();                                             
             break;                                                   
             
        case 3:                                                         // This Quits from the menu
             break;                                                     //
        default:                                                        // For when they type something not in range of 1-3
          printf("\nInvalid Choice\n\n");                               //
          
          
       }
      printf("\n\n");
      system("pause");
      }

  9. #9
    Registered User
    Join Date
    Mar 2011
    Posts
    10
    thanks nonoob

    it works perfectly, i hate C to be 100% honest. but its soo pleasing when it all works =)

  10. #10
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,832
    When you start succeeding more than failing, say, over 50% of the time, things start getting fun! Eventually you'll reach a point where you KNOW everything is solvable by yourself, given a bit of time. That's when you start wishing for really complicated challenges.

    I'd say it takes at least a few months if this is your first programming language.
    CommonTater likes this.

  11. #11
    Registered User
    Join Date
    Mar 2011
    Posts
    10

    thanks

    yeah thanks for your help.

    the while loop you gave me worked, but i couldnt exit it, was very funny.

    i latered it now, so that it all quits and stuff

    made my while loop, not equal to a value which when entered quits.

    =)

  12. #12
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,832
    Oh sorry. Yes the nice way is to put a decision in the while() to exit. I saw the return 0 but I didn't pay close attention. If it's for the exit case 4 then the forever loop would have been OK.

  13. #13
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Conor1992 View Post
    yeah thanks for your help.

    the while loop you gave me worked, but i couldnt exit it, was very funny.

    i latered it now, so that it all quits and stuff

    made my while loop, not equal to a value which when entered quits.

    =)
    Code:
     while (1) {  /* loop forever */
     printf("\nChoose from the list of options:\n");
     printf("\n1.Calender Options");
     printf("\n2.Appointment Options");
     printf("\n3.Quit\n\n");
     printf("Option : ");
      scanf("%d", &value);
      
      switch(value)
      {
        case 1:
            subm_cal();
            break;
        case 2:
             subm_app();                                             
             break;                                                   
             
        case 3:                                                         // This Quits from the menu
          Exit(1);
        default:                                                        // For when they type something not in range of 1-3
          printf("\nInvalid Choice\n\n");                               //
          }
      }

  14. #14
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,512
    Code:
      case 3:                                                         // This Quits from the menu
          Exit(1);
    Not sure what Exit() is with a capital 'E' (is it some special function I don't know about?), but exit() will quit more than the menu. It will quit the entire program in great haste, skipping any "leave the terminal open" commands and any clean up code.

    @OP: I find the best method for running a loop based on user input is to use a do while loop. Do while loops always execute once, which is good because you always want to display the menu and get input at least once. You also don't have to worry about initializing the user's input value to something non-quit. Example:
    Code:
    do {
        print menu
        get input
        act on input
    } while (input != quit);

  15. #15
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by anduril462 View Post
    Code:
      case 3:                                                         // This Quits from the menu
          Exit(1);
    Not sure what Exit() is with a capital 'E' (is it some special function I don't know about?), but exit() will quit more than the menu.
    Hermmm... guilty with explaination...

    That would be yer basic typo anduril. In my own defense... I am working with a messed up right hand (sprained fingers)...

    Code:
      case 3:                                                         // This Quits from the menu
          // insert cleanup code here
          exit(1);

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. About an indefinite matrix
    By nick048 in forum C++ Programming
    Replies: 4
    Last Post: 11-19-2006, 05:19 AM
  2. indefinite number of datapoints question
    By Wick in forum C++ Programming
    Replies: 1
    Last Post: 09-04-2003, 09:33 AM
  3. where is my indefinite loop here?
    By pancho in forum C Programming
    Replies: 9
    Last Post: 03-15-2002, 12:42 PM
  4. Should this board be a Java board?
    By Xterria in forum C# Programming
    Replies: 49
    Last Post: 02-18-2002, 06:46 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21