Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
349 views
in Technique[技术] by (71.8m points)

c - Call menu function in another function

I'm trying to call menu function in the main function and let it prompt until the user decides to quit, but seems like it's not giving me response.. I'm new at this website coding, let me know if there's anything eg. post format I can improve!

clearkeyboard function:

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

The function to call menu:

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

    } while (done == 1);
}

Menu function:

int menu(void)
{
    int done = 1;
    int choice;
    char c;
    do {
        printf("Contact Management System
");
        printf("-------------------------
");
        printf("1. Display contacts
");
        printf("2. Add a contact
");
        printf("3. Update a contact
");
        printf("4. Delete a contact
");
        printf("5. Search contacts by cell phone number
");
        printf("6. Sort contacts by cell phone numbe
");
        printf("0. Exit

");
        printf("Select an option:> ");
        int rtn = scanf("%d%c", &choice, &c);

        if (rtn == EOF || rtn == 0 || c != '
')
            clearKeyboard();
        else if (choice >= 0 && choice <= 6 && c == '
')
            done = 0;
        else {
            clearKeyboard();
            printf("*** OUT OF RANGE *** <Enter a number between 0 and 6>: ");
            scanf("%d", &choice);
        }
    } while (done == 1);

    return choice;
}

Attached image below is where goes wrong, the correct version should do case 1 instead of keep prompt menu again.

Incorrect Part

enter image description here

Thanks in advance!!!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

there are two problem in your code.

  1. menu() function returns int and you are matching int with char in switch case case '1': it should be case 1:

  2. replace below line

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

hope this help you.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...