Thread: Switch Case menu problem????

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    7

    Switch Case menu problem????

    The following menu does not work. When I (for example) choose case 1 to change var1, the program does not return to the menu, it just ends.

    I'm not sure about the while loop aswell.

    Code:
    #include <stdio.h>
    #include <math.h>
    #include <stdlib.h>
    
    float total, var1;
    int menu, menu2;
    
    main()
    {     
          defaultdata();
          
          menu = 0;
          while (menu!=3)
          {
              
         
          printf("1. Change variables\n");
          printf("2. Run program\n");
          printf("3. Exit program\n");
          printf("Enter option. ");
          scanf("%d", &menu);
       
          
          switch (menu)
          {
                 case 1:
                               {  
                               
                                  system("cls");
                                  printf("1. variable1 %d\n", var1);
                                  printf("2. variable 2\n");
                                  printf("3. variable 3\n");
                                  printf("4. variable 4\n");
                                  printf("5. variable 5\n");
                                  printf("6. Reset all to default\n");
                                  printf("7. Return to Main Menu\n\n");
                                  printf("Enter option. ");
                                  scanf("%d", &menu2);
                                  
                               
                               switch (menu2)
                               {
                                      case 1: 
                                           {
                                              printf("Enter variable 1:");
                                              scanf("%d", &var1);
                                              total = 300 + (120 * var1);
                                              break;
                                           }
                                           
                                       case 2:
                                            {
                                                          
                                            }
                                            
                                       case 3:
                                             {
                                                          
                                             }  
                                            
                                       case 4:
                                            {
                                                          
                                            }
                                               
                                       case 5:
                                            {
                                                          
                                            }
                                            
                                       case 6:
                                            {
                                                  defaultdata();
                                                  break;
                                                          
                                            }
                                                                    
                                       case 7:
                                            {
                                                  menu =1;
                                                  break;                  
                                            }
                                            
                                       default:
                                               {
                                               printf("ERROR invalid");
                                              
                                            
                                               }                         
                                 }
                                 }                       
       }
       }
    }
    Last edited by Jason Singh; 10-23-2011 at 05:29 AM.

  2. #2
    Registered User
    Join Date
    Jul 2011
    Posts
    46
    how about using
    Code:
     for(;;)
    instead of the while loop

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    How about

    a) Indentation.
    Code:
    #include <stdio.h>
    #include <math.h>
    #include <stdlib.h>
    
    float total, var1;
    int menu, menu2;
    void defaultdata(void){}
    main()
    {
      defaultdata();
    
      menu = 0;
      while (menu != 3) {
        printf("1. Change variables\n");
        printf("2. Run program\n");
        printf("3. Exit program\n");
        printf("Enter option. ");
        scanf("%d", &menu);
    
        switch (menu) {
        case 1:
          {
            system("cls");
            printf("1. variable1 %f\n", var1);
            printf("2. variable 2\n");
            printf("3. variable 3\n");
            printf("4. variable 4\n");
            printf("5. variable 5\n");
            printf("6. Reset all to default\n");
            printf("7. Return to Main Menu\n\n");
            printf("Enter option. ");
            scanf("%d", &menu2);
    
            switch (menu2) {
            case 1:
              {
                printf("Enter variable 1:");
                scanf("%f", &var1);
                total = 300 + (120 * var1);
                break;
              }
            case 2:
              {
              }
            case 3:
              {
              }
            case 4:
              {
              }
            case 5:
              {
              }
            case 6:
              {
                defaultdata();
                break;
              }
            case 7:
              {
                menu = 1;
                break;
              }
            default:
              {
                printf("ERROR invalid");
              }
            }
          }
        }
      }
    }
    Good indentation goes a long way to helping you figure out problems yourself, as well as attracting the attention of helpers when you're stuck.

    b) Using a decent compiler.
    Code:
    $ gcc foo.c
    foo.c: In function ‘main’:
    foo.c:24: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘double’
    foo.c:38: warning: format ‘%d’ expects type ‘int *’, but argument 2 has type ‘float *’
    Your illegal format conversions are probably doing something you don't want.

    New result
    Code:
    $ ./a.out 
    1. Change variables
    2. Run program
    3. Exit program
    Enter option. 1
    sh: cls: not found
    1. variable1 0.000000
    2. variable 2
    3. variable 3
    4. variable 4
    5. variable 5
    6. Reset all to default
    7. Return to Main Menu
    
    Enter option. 1
    Enter variable 1:33
    1. Change variables
    2. Run program
    3. Exit program
    Enter option. 3
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. switch/case problem
    By maxilog1009 in forum C Programming
    Replies: 3
    Last Post: 09-07-2009, 08:35 PM
  2. switch/case problem?
    By Wiiplayer12 in forum C++ Programming
    Replies: 2
    Last Post: 05-15-2008, 11:13 PM
  3. case switch problem
    By digdug4life in forum C++ Programming
    Replies: 10
    Last Post: 10-08-2005, 09:25 AM
  4. Switch/case problem
    By Ivan! in forum C++ Programming
    Replies: 4
    Last Post: 01-31-2003, 11:48 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