-
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;
}
-
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.
-
Gee
Gee, that code sure was in a chaos ;)
-
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));