Appeler la fonction de menu dans une autre fonction

J’essaie d’appeler la fonction de menu dans la fonction principale et de la laisser presser jusqu’à ce que l’utilisateur décide de quitter, mais il semble que cela ne me donne pas de réponse. Je suis nouveau dans le codage de ce site Web. le format du post que je peux améliorer!

fonction clearkeyboard:

void clearKeyboard(void) { int c; while ((c = getchar()) != '\n' && c != EOF); } 

La fonction pour appeler le menu:

 void ContactManagerSystem(void) { int contactchoice; int done = 1; char yesno, c; do { clearKeyboard(); contactchoice = menu(); switch (contactchoice) { case 1: printf("<<>>\n"); break; case 2: printf("<<>>\n"); break; case 3: printf("<<>>\n"); break; case 4: printf("<<>>\n"); break; case 5: printf("<<>>\n"); break; case 6: printf("<<>>\n"); break; case 0: printf("Exit the program? (Y)es/(N)o: "); scanf("%c%c", &yesno, &c); if (yesno == 'Y' || yesno == 'y' && c == '\n') { done = 0; break; } else if (yesno == 'N' || yesno == 'n') break; default: break; } } while (done == 1); } 

Fonction de menu:

 int menu(void) { int done = 1; int choice; char c; do { printf("Contact Management System\n"); printf("-------------------------\n"); printf("1. Display contacts\n"); printf("2. Add a contact\n"); printf("3. Update a contact\n"); printf("4. Delete a contact\n"); printf("5. Search contacts by cell phone number\n"); printf("6. Sort contacts by cell phone numbe\n"); printf("0. Exit\n\n"); printf("Select an option:> "); int rtn = scanf("%d%c", &choice, &c); if (rtn == EOF || rtn == 0 || c != '\n') clearKeyboard(); else if (choice >= 0 && choice <= 6 && c == '\n') done = 0; else { clearKeyboard(); printf("*** OUT OF RANGE *** : "); scanf("%d", &choice); } } while (done == 1); return choice; } 

L’image jointe ci-dessous indique les erreurs, la version appropriée doit traiter le cas 1 au lieu de conserver le menu contextuel.

Partie incorrecte

entrez la description de l'image ici

Merci d’avance!!!

il y a deux problèmes dans votre code.

  1. La fonction menu () renvoie int et vous faites correspondre int avec char dans le cas de commutation case '1': il devrait s’agir du case 1:

  2. remplacer la ligne ci-dessous

scanf("%c%c", &yesno,&c); avec scanf("%c%c", &c,&yesno);

espérons que cela vous aide.

Deux problèmes à améliorer.

1) Étant donné que menu() renvoie un entier, le casse du commutateur doit comporter des entiers et non des caractères.

2)

  printf("Exit the program? (Y)es/(N)o: "); scanf("%c%c", &yesno, &c); 

Vous n’utilisez pas c ;

scanf(" %c", &yesno); est assez.

 #include  void clearKeyboard(void) { int c; while ((c = getchar()) != '\n' && c != EOF); } void ContactManagerSystem(void) { int contactchoice; int done = 1; char yesno, c; do { contactchoice = menu(); switch (contactchoice) { case 1: printf("<<< Feature 1 is unavailable >>>\n"); break; case 2: printf("<<< Feature 2 is unavailable >>>\n"); break; case 3: printf("<<< Feature 3 is unavailable >>>\n"); break; case 4: printf("<<< Feature 4 is unavailable >>>\n"); break; case 5: printf("<<< Feature 5 is unavailable >>>\n"); break; case 6: printf("<<< Feature 6 is unavailable >>>\n"); case 0: printf("Exit the program? (Y)es/(N)o: "); scanf(" %c", &yesno); if (yesno == 'Y' || yesno == 'y') { done = 0; break; } else if (yesno == 'N' || yesno == 'n') break; default: break; } } while (done == 1); } int menu(void) { int done = 1; int choice; do { printf("Contact Management System\n"); printf("-------------------------\n"); printf("1. Display contacts\n"); printf("2. Add a contact\n"); printf("3. Update a contact\n"); printf("4. Delete a contact\n"); printf("5. Search contacts by cell phone number\n"); printf("6. Sort contacts by cell phone numbe\n"); printf("0. Exit\n\n"); printf("Select an option:> "); scanf("%d", &choice); if (choice >= 0 && choice <= 6) done = 0; else { clearKeyboard(); printf("*** OUT OF RANGE *** : "); scanf("%d", &choice); } } while (done == 1); return choice; } int main(void) { ContactManagerSystem(); return 0; } 

Sortie:

 Contact Management System ------------------------- 1. Display contacts 2. Add a contact 3. Update a contact 4. Delete a contact 5. Search contacts by cell phone number 6. Sort contacts by cell phone numbe 0. Exit Select an option:> 1 <<< Feature 1 is unavailable >>> Contact Management System ------------------------- 1. Display contacts 2. Add a contact 3. Update a contact 4. Delete a contact 5. Search contacts by cell phone number 6. Sort contacts by cell phone numbe 0. Exit Select an option:> 0 Exit the program? (Y)es/(N)o: Y 

Juste quelques nettoyages supplémentaires. Vous devez déterminer comment gérer la suppression par l’utilisateur en générant un EOF manuel avec Ctrl + d (ou Ctrl + z sur Windoze). Puisque vous renvoyez une valeur int , vous pouvez facilement renvoyer -1 pour votre cas EOF .

De plus, combien d’appels à printf devez-vous effectuer? Un suffit, par exemple

 int menu (void) { int rtn, choice; /* scanf return, menu choice */ for (;;) { /* loop until valid input or EOF displaying menu */ printf ("\nContact Management System\n" "-------------------------\n" " 1. Display contacts\n" " 2. Add a contact\n" " 3. Update a contact\n" " 4. Delete a contact\n" " 5. Search contacts by cell phone number\n" " 6. Sort contacts by cell phone number\n" " 0. Exit\n\n" "Select an option:> "); rtn = scanf ("%d", &choice); /* save scanf return */ if (rtn == 1 && 0 <= choice && choice <= 6) /* good value, break */ break; else if (rtn == EOF) { /* user canceled input, return -1 */ fprintf (stderr, "(user canceled input).\n"); return -1; } empty_stdin(); /* empty_stdin for rtn = 0 case */ fprintf (stderr, " error: invalid input.\n"); } empty_stdin(); /* empty_stdin after good value */ return choice; /* return choice to user */ } 

La fonction de switch C assure la casse jusqu'à ce qu'une instruction break soit rencontrée. Vous pouvez utiliser ceci à votre avantage dans votre fonction CMS jusqu'à ce que vous remplissiez tous les case: déclarations. Vous pouvez continuer à utiliser fallthrough pour gérer le 0 - exit et gérer le retour EOF (entrée annulée par l'utilisateur) depuis le menu() , par exemple

 void cms (void) { for (;;) { /* loop continually calling menu */ int choice = menu(); char c; switch (choice) { case 1: case 2: case 3: /* use case-fallthrough until setup */ case 4: case 5: case 6: printf("<<< Feature %d is unavailable >>>\n", choice); break; case 0: printf ("exit program [y/n]: "); /* confirm exit */ if (scanf (" %c", &c) == EOF) { /* handle EOF */ fprintf (stderr, "(user canceled input).\n"); return; } empty_stdin(); if (c == 'N' || c == 'n') { /* handle no exit */ continue; break; } case -1: /* fallthrough yes exit or EOF in menu */ return; break; } } } 

En résumé, vous pouvez créer un petit système de menu assez robuste, par exemple

 #include  void empty_stdin (void) { for (int c = getchar(); c != '\n' && c != EOF; c = getchar()) {} } int menu (void) { int rtn, choice; /* scanf return, menu choice */ for (;;) { /* loop until valid input or EOF displaying menu */ printf ("\nContact Management System\n" "-------------------------\n" " 1. Display contacts\n" " 2. Add a contact\n" " 3. Update a contact\n" " 4. Delete a contact\n" " 5. Search contacts by cell phone number\n" " 6. Sort contacts by cell phone number\n" " 0. Exit\n\n" "Select an option:> "); rtn = scanf ("%d", &choice); /* save scanf return */ if (rtn == 1 && 0 <= choice && choice <= 6) /* good value, break */ break; else if (rtn == EOF) { /* user canceled input, return -1 */ fprintf (stderr, "(user canceled input).\n"); return -1; } empty_stdin(); /* empty_stdin for rtn = 0 case */ fprintf (stderr, " error: invalid input.\n"); } empty_stdin(); /* empty_stdin after good value */ return choice; /* return choice to user */ } void cms (void) { for (;;) { /* loop continually calling menu */ int choice = menu(); char c; switch (choice) { case 1: case 2: case 3: /* use case-fallthrough until setup */ case 4: case 5: case 6: printf("<<< Feature %d is unavailable >>>\n", choice); break; case 0: printf ("exit program [y/n]: "); /* confirm exit */ if (scanf (" %c", &c) == EOF) { /* handle EOF */ fprintf (stderr, "(user canceled input).\n"); return; } empty_stdin(); if (c == 'N' || c == 'n') { /* handle no exit */ continue; break; } case -1: /* fallthrough yes exit or EOF in menu */ return; break; } } } int main (void) { cms(); return 0; } 

Exemple d'utilisation / de sortie

Vérification des cas où l'utilisateur annule l'entrée dans menu() ou à la exit program [y/n]: invite en générant un EOF manuel avec Ctrl + d (ou Ctrl + z sur Windoze):

 $ ./bin/scanf_menu_cms Contact Management System ------------------------- 1. Display contacts 2. Add a contact 3. Update a contact 4. Delete a contact 5. Search contacts by cell phone number 6. Sort contacts by cell phone number 0. Exit Select an option:> (user canceled input). $ ./bin/scanf_menu_cms Contact Management System ------------------------- 1. Display contacts 2. Add a contact 3. Update a contact 4. Delete a contact 5. Search contacts by cell phone number 6. Sort contacts by cell phone number 0. Exit Select an option:> 0 exit program [y/n]: (user canceled input). 

Tout va bien, vérifiez maintenant une réponse à un cas d'utilisation normal Non pour quitter, puis Oui:

 $ ./bin/scanf_menu_cms Contact Management System ------------------------- 1. Display contacts 2. Add a contact 3. Update a contact 4. Delete a contact 5. Search contacts by cell phone number 6. Sort contacts by cell phone number 0. Exit Select an option:> 4 <<< Feature 4 is unavailable >>> Contact Management System ------------------------- 1. Display contacts 2. Add a contact 3. Update a contact 4. Delete a contact 5. Search contacts by cell phone number 6. Sort contacts by cell phone number 0. Exit Select an option:> 0 exit program [y/n]: n Contact Management System ------------------------- 1. Display contacts 2. Add a contact 3. Update a contact 4. Delete a contact 5. Search contacts by cell phone number 6. Sort contacts by cell phone number 0. Exit Select an option:> 0 exit program [y/n]: y 

Enfin un cas d'entrée invalide:

 $ ./bin/scanf_menu_cms Contact Management System ------------------------- 1. Display contacts 2. Add a contact 3. Update a contact 4. Delete a contact 5. Search contacts by cell phone number 6. Sort contacts by cell phone number 0. Exit Select an option:> My dog has fleas! error: invalid input. Contact Management System ------------------------- 1. Display contacts 2. Add a contact 3. Update a contact 4. Delete a contact 5. Search contacts by cell phone number 6. Sort contacts by cell phone number 0. Exit Select an option:> -1 error: invalid input. Contact Management System ------------------------- 1. Display contacts 2. Add a contact 3. Update a contact 4. Delete a contact 5. Search contacts by cell phone number 6. Sort contacts by cell phone number 0. Exit Select an option:> 7 error: invalid input. Contact Management System ------------------------- 1. Display contacts 2. Add a contact 3. Update a contact 4. Delete a contact 5. Search contacts by cell phone number 6. Sort contacts by cell phone number 0. Exit Select an option:> 4 <<< Feature 4 is unavailable >>> 

Bien qu'il ne camelCase pas d'une erreur, le style de codage standard pour C évite l'utilisation de noms de variables camelCase ou MixedCase en faveur de tous les minuscules, tout en réservant des noms en majuscules à utiliser avec des macros et des constantes. C’est une question de style. C’est donc votre choix, mais si vous ne le suivez pas, vous risquez d’avoir une mauvaise première impression dans certains milieux.