在另一个功能中调用菜单功能

时间:2018-03-21 03:52:33

标签: c

我试图在主要功能中调用菜单功能并让它提示直到用户决定退出,但似乎它没有给我回复.. 我是这个网站编码的新手,让我知道是否有任何例如。发布格式我可以改进!

clearkeyboard功能:

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

调用菜单的功能:

void ContactManagerSystem(void)
{
    int contactchoice;
    int done = 1;
    char yesno, c;
    do {
        clearKeyboard();
        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");
            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);
}

菜单功能:

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 *** <Enter a number between 0 and 6>: ");
            scanf("%d", &choice);
        }
    } while (done == 1);

    return choice;
}

下面附图是出错的地方,正确的版本应该做案例1,而不是再次保留提示菜单。

不正确的部分

enter image description here

提前致谢!!!

3 个答案:

答案 0 :(得分:1)

您的代码中存在两个问题。

  1. menu()函数返回int,并且您在int切换案例char中匹配case '1':它应该是case 1:

  2. 替换以下行

  3. 带有scanf("%c%c", &yesno,&c);

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

    希望这对你有所帮助。

答案 1 :(得分:1)

有两个问题需要改进。

1)由于menu()返回整数,因此switch case应该有整数而不是字符。

2)

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

您不使用c;

scanf(" %c", &yesno); 就够了。

#include <stdio.h>

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 *** <Enter a number between 0 and 6>: ");
            scanf("%d", &choice);
        }
    } while (done == 1);

    return choice;
}

int main(void)
{
    ContactManagerSystem();
    return 0;
}

输出:

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

答案 2 :(得分:1)

只需进行一些额外的清理工作。您需要通过使用 Ctrl + d (或在windoze上 Ctrl + z )生成手动EOF来确定如何处理用户取消输入。由于您返回了int值,因此您可以轻松返回-1 EOF案例。

此外,您需要拨打多少次printf来电?一个就足够了,例如

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 */
}

C switch函数提供 case-fallthrough ,直到遇到break语句。您可以在CMS函数中使用此功能,直到填写所有case:语句为止。您可以继续使用fallthrough来处理0 - exit并处理来自EOF的{​​{1}}(用户取消的输入)返回,例如

menu()

完全可以,你可以创建一个相当强大的小菜单系统,例如

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;
        }
    }
}

示例使用/输出

通过 Ctrl + d (或#include <stdio.h> 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; } ,检查用户在menu()exit program [y/n]:提示中取消输入的情况>在windoze上按Ctrl + z :

EOF

一切都好,现在检查一个正常的用例,回答No退出,然后是:

$ ./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).

最后一个无效的输入案例:

$ ./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

虽然不是错误,但C的标准编码样式避免使用$ ./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 >>> camelCase变量名来支持所有小写,同时保留用于宏和常量的大写名称。这是一个风格问题 - 所以它完全取决于你,但如果不遵循它可能会在某些圈子中产生错误的第一印象。