Thread: help improve my code

  1. #1
    Registered User
    Join Date
    Aug 2001
    Posts
    380

    help improve my code

    How would I go about changing this section of code so that the code that's used to check to see if the number of entries is equal to the maximum amount that could be entered to only be written once? Also, I would like it be reprinted menu and give control back to the user.

    Code:
    int numList; // Number of current filled entries
    
    void crMenu(void)
    { // Displays Create DATABANK menu
     int choice;
    clrscr();
    
     (numList > 1) || (numList == 0) ? printf("There are %d entries\n", numList) : printf("There is %d entry\n", numList);
     printf("The list of possible question types avaliable");
     printf("\n\t1. Direct");
     printf("\n\t2. True or False");
     printf("\n\t3. Fill in the blank");
     printf("\n\t4. Multiple Choice");
     printf("\n\t5. Back to main menu");
     printf("\nYour choice "); 
     do
       {
        choice = getChoice();
        switch(choice)
        {
          case (1) : if (numList >= NUM) // Number of maximum possible entries		
               		       { printf("\nThe databank is full; you cannot add more\n");
                                     pressKey();				
    			         crMenu();
                                   }
                                   directQuestion();
                                   break;
         case (2) : if (numList >= NUM) // Number of maximum possible entries
                                  { printf("\nThe databank is full; you cannot add more\n");
          				pressKey();				
    				crMenu();	
    			      }
    			      break;
         case (5) : menu();
    		break;
         default : printf("Enter a choice from the list\n");
    	       pressKey();
    		break;
        }
    	   crMenu();
       } while((choice > 1) || (choice < 5));
    
     return;
    }
    Last edited by lambs4; 11-21-2001 at 11:47 AM.

  2. #2
    Sayeh
    Guest
    An easier way is to simply check against your max number whenever one is entered or deleted. Then either enable, or disable the menu choice.

    That lets you do the check in one place, much more cleanly. Just don't give a user an option they can't use.

  3. #3
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145

    Gee

    Gee, that code sure was in a chaos
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  4. #4
    Registered User
    Join Date
    Aug 2001
    Posts
    380
    Is this how you suggested the improvement? When the maximum is reached the function loops until the user enters #5 as their choice. Therefore in the end the options are disables anyways.
    Code:
    	do
    	{
    	choice = getChoice();
    	if ((numList >= NUM) && ((choice >= 1) && (choice <= 4)))
    	{ printf("\nThe databank is full; you cannot add more\n");
    	pressKey();
    	crMenu();
    	// Return from the function early
    	}
    	switch(choice)
    	 {
    	 case (1) : directQuestion();
    		    break;
    	 case (5) : menu();
                	    break;
    	  default : printf("Enter a choice from the list\n");
    		    pressKey();
    		    break;
    	 }
    		  crMenu();
    	} while((choice > 1) || (choice < 5));

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Explain this C code in english
    By soadlink in forum C Programming
    Replies: 16
    Last Post: 08-31-2006, 12:48 AM
  2. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  3. How to improve code
    By rugby in forum C Programming
    Replies: 3
    Last Post: 04-15-2003, 09:24 AM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Replies: 0
    Last Post: 02-21-2002, 06:05 PM