返回主菜单时的问题-C编程

问题描述 投票:0回答:2

执行该程序并选择情况1时,将显示一个子菜单。在此子菜单中,有一个选项可以返回到主菜单,现在选择该菜单时无法正确显示,因为它似乎显示了主菜单和默认菜单两次。这是代码:

    #include <stdio.h>
    #include "functions.h"   //calls the functions.h file

    int switch1();
    int switch2();

    int main() {
        //Declaring variable
        char command;

        do {
            do {
                printf(SPLIT);
                printf("\nEnter choice to operate one of the following functions.\n");
                printf("1 - Operate using integer representation\n");
                printf("2 - Operate using textual representation\n");
                printf("0 - Quit\n");
                printf("Choice:");
                scanf("%c", &command);

                switch (command) {
                    case '1':
                        switch1(); //Executes function switch1
                        break;
                    case '2':
                        switch2(); //Executes function switch2
                        break;
                    case '0':
                        printf(SPLIT);
                        printf("\nQuitting.");
                        printf(SPLIT);
                        return 0; //Program stops
                    default: //Switch statement reaches default if no other cases are reached
                        printf("\nIncorrect input, please re-try.\nEnter choice\n");
                        break;
                }
            } while(command != '0');
        } while (command < '0' || command > '2');
        return 0;
    }

int switch1() {
    //Declaring variables
    char command;
    int *array, *iZeroed = NULL;

    printf(SPLIT);
    printf("\nInteger representation will be used!\n");

    array = generate(); //Executes function generate and sets the return to be array

    do {
        do {
            printf("\nChoose an option:\n");
            printf("1) Shuffle the array\n");
            printf("2) Sort the array\n");
            printf("3) Zero an element from the array\n");
            printf("4) Display previous zeroed out element\n");
            printf("0) Go back to main menu\n");
            printf("Choice:");

            scanf(" %c", &command);
            switch (command) {
                case '1':
                    array = shuffle(array); //Executes function shuffle and sets the return to be the new array
                    break;
                case '2':
                    array = sort(array); //Executes function shuffle and sets the return to be the new array
                    break;
                case '3':
                    iZeroed = shoot(array); //Executes function shoot and sets the return to be the element changed to 0
                    break;
                case '4':
                    target(iZeroed); //Executes function target
                    break;
                case '0':
                    return 0;
                default:  //Switch statement reaches default if no other cases are reached
                    printf(SPLIT);
                    printf("\nIncorrect input. Please re-enter an option\n");
                    break;
            }
        } while (command != '0');
    } while (command < '0' || command > '1');

    return 0;

}

这是我选择返回主菜单时得到的输出:

====================================================================
Choose an option:
1) Shuffle the array
2) Sort the array
3) Zero an element from the array
4) Display previous zeroed out element
0) Go back to main menu
Choice:0

====================================================================
Enter choice to operate one of the following functions.
1 - Operate using integer representation
2 - Operate using textual representation
0 - Quit
Choice:
Incorrect input, please re-try.
Enter choice

====================================================================
Enter choice to operate one of the following functions.
1 - Operate using integer representation
2 - Operate using textual representation
0 - Quit
Choice: 
c menu switch-statement do-while submenu
2个回答
1
投票

在输入说明符scanf()的前面,在%c的格式字符串中插入空格字符,这意味着scanf()应该在其中放置空白字符。

您已经在switch1()函数中这样做了,所以您也应该在main()函数中这样做。


1
投票
 printf("Choice:");
 scanf("%c", &command);

设为" %c"

© www.soinside.com 2019 - 2024. All rights reserved.