Thread: While loop in menu help!!

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

    While loop in menu help!!

    Code:
    I AM HAVING PROBLEMS WITH THE MENU. AFTER TYPING IN A CASE OPTION THE MAIN MENU SHOULD RE-APPEAR BUT DOES NOT.
    ANY SUGGESTIONS
    
    #include <stdio.h>
    #include <math.h>
    #include <grx20.h>
    
    
    #define length 2
    
     int n, i, menu1;
     float m, y, y0, x, y, x1[length], y1[length], xsum, ysum, xysum, xsq[length]; 
     float xsqsum, b[length], xres, yres, xplot, xploti, yplot, yploti, y2[length];
    
    
    main()
    
    {          
          
          /* Main menu */
         
          menu1 = 1;
          while (menu1==1)
              
          {
              n = length;     
              xsum = 0;
              ysum = 0;
              xysum = 0;
              xsqsum = 0;
               
              printf("What do you want to do?   Select an option\n\n");
              printf("1. Input data\n");
              printf("2. View table of data\n");
              printf("3. Plot line of best fit\n");
              printf("4. Exit program\n");
              scanf("%d", &menu1);
          
              switch (menu1)
              {
                     case 1:
                          
                          /* Generate set of data */                   
         
                          printf("\nEnter the number of x-y values: ");     
                          scanf("%d", &n);                         
                            
                          for (i=0; i<n; i++ )                                          
                          {
                              printf("Enter x-y values separated by a space: ");
                              scanf("%f %f", &x, &y);
                        
                              x1[i] = x;
                              xsq[i] = x1[i]* x1[i];
                              y1[i] = y;
                              xsum = xsum + x1[i];
                              xsqsum = xsqsum + xsq[i];
                              ysum = ysum + y1[i];
                              xysum = xysum + (x1[i]*y1[i]);
                              
                          }
                               
                          /* Calculate gradient */
                          m = ((n * xysum) - (xsum * ysum)) / (n * (xsqsum)) - 
                          (xsum * xsum);
                        
                          /* Calculate y-intercept */
                          y0 = (ysum - (m * xsum)) / n;
                     
                          for (i=0; i<n; i++ )
                          {
                             
                              y2[i] = y0 + m*x1[i];
                              b[i] = y2[i] - y1[i];
                             
                          }      
                        
                          printf("Gradient = %f, y-intercept = %f\n", m, y0);
                          scanf("%d", &i);
                          break;
                          
                        
                          case 2:
                               printf("X \t Y \t y-Y");
                               for (i=0; i<n; i++ )
                               {
                                                                                                                                               
                                   printf("%f %f %f", x1[i], y1[i], b[i]);
                               }
                               scanf("%d", &i);
                               break;
                   
                          case 3:
                               for (i=1; i<n; i++ )
                               {
                                                                                          
                                   GrColor bgColour = 0;        /* background colour*/
                                   GrColor pColour = 2;         /*plot colour*/
                   
                                   GrSetMode(GR_default_graphics);
                                   xres = GrScreenX();
                                   yres = GrScreenY();
                             
                                   /*GrClearScreen(bgColour);*/
                             
                                   xplot = x1[i-1];
                                   xploti = x1[i];
                                   yplot =  y1[i-1];
                                   yploti = y1[i];
                                   GrLine(xplot, yplot, xploti, yploti, pColour);
                          
                          
                               }
                               system ("PAUSE");
                               GrSetMode(GR_default_text);
                               break;
                   
                               case 4: 
                                    menu1 = 0;
                                                
                               default:
                                       printf("Invalid option. Try again\n");
                                       system ("PAUSE");
                   }
          }       
    }

  2. #2
    Registered User
    Join Date
    Mar 2011
    Posts
    278
    Your code doesn't compile without errors and warnings. Please turn on ALL warnings fix them, and re-post.

    For ex, 'y' is declared twice...
    Code:
    float m, y, y0, x, y,
    Also: you have no break after case4:
    y0 appears to be declared in math.h. Why is it a global for you?
    Last edited by mike65535; 05-02-2011 at 06:13 AM.

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    278
    More to your point, menu1 only stays equal to 1 the first time through, or if the user's menu choice is also 1. Once you choose something else (like 2 or 3, etc.) in the menu, menu1 changes and the while kicks out.

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Try ... while (menu1 != 4)

  5. #5
    Registered User
    Join Date
    Mar 2011
    Posts
    278
    His final case, 4, sets menu1 = 0 so he should test for menu1 != 0.

    I say this because if, in the future, he adds another menu item (and its necessary case statement) he doesn't have to edit the while()

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by mike65535 View Post
    His final case, 4, sets menu1 = 0 so he should test for menu1 != 0.

    I say this because if, in the future, he adds another menu item (and its necessary case statement) he doesn't have to edit the while()
    Good catch... I looked at the menu and saw option 4, but didn't check the cases... my bad.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. loop in menu
    By aama100 in forum C++ Programming
    Replies: 1
    Last Post: 01-23-2008, 11:56 PM
  2. Menu Loop
    By alleycat in forum C++ Programming
    Replies: 1
    Last Post: 11-07-2006, 05:06 AM
  3. program will not stay in menu loop
    By StevenGarcia in forum C++ Programming
    Replies: 3
    Last Post: 11-06-2006, 05:17 PM
  4. create menu with do while loop advice pls
    By ali_1 in forum C++ Programming
    Replies: 1
    Last Post: 04-23-2006, 09:34 PM
  5. Help me in while loop with case menu
    By playboy1620 in forum C Programming
    Replies: 1
    Last Post: 02-20-2002, 11:07 PM

Tags for this Thread