如何正确制作动态命令行菜单?

时间:2019-06-25 17:09:39

标签: c data-structures menu submenu

更精确..我如何像所有其他软件和游戏一样添加“返回主菜单”功能?

void showMenu()
{
    puts( "1. Create school\n" 
           "2. Add room\n" 
           "3.Add student to room\n"
           "4.Find student\n"
           "5. Show students in room\n"
           "\n" "6. Exit");
}


int main()
{
    clrscr();

    studentList *foundStudent;

    int input;
    showMenu();

    while( scanf("%d", &input) )
    {   

        if(input == 6)
        {
            periods("Exiting");
            break;
        }

        if(input == 1)
        {
            school *school;
            school = createSchool();
        }

        if(input == 2)
        {
            int room, roomNr;
            printf("Enter room Nr. and Class:");
            scanf("%d %d", &room, &roomNr);

        }
    }

    return 0;
}

我尝试的任何方法都无效,只是创建了更多的冗余,从未想到goto会如此令人困惑。

尽管switch更有意义,但我认为它不能解决我的问题。

1 个答案:

答案 0 :(得分:0)

这是一个可行的示例。 解决此问题的方法只是简单地制作一种“菜单内的菜单”,我确实使用了一些goto,但是就命令行菜单而言,可能仅此而已。

#define clrscr() printf("\e[1;1H\e[2J")

void periods(char* message)
{   
    const int trigger = 500; // ms
    const int numDots = 3;

    while (1) 
    {
        // Return and clear with spaces, then return and print prompt.
        printf("\r%*s\r%s", sizeof(message) - 1 + numDots, "", message);
        fflush(stdout);

        // Print numDots number of dots, one every trigger milliseconds.
        for (int i = 0; i < numDots; i++) 
        {
            usleep(trigger * 1000);
            fputc('.', stdout);
            fflush(stdout);
        }
        break;
    }
}

void showmenu()
{
    clrscr();
    puts("1. New Game\n"
         "2. Load Game\n"
         "3. Credits\n\n"
         "4. Exit\n" );

}

int checkString(char *str)
{
    int status = 0;
    int ln = strlen(str);

    for(int i = 0; i < ln; i++)
    {
        if(isdigit( str[i] ) )
        status = 1; 
        break;
    }

    return status; 
}

int main(){

    char choice; 

    clrscr();
    int status, isNum = -101;
    char *name, *buffer, YN;


    do{ 
        showmenu();
         while(1)
        {
            scanf(" %c", &choice);
            if( isdigit(choice) )
            {
                break;     
            }
            else
            {
                fflush(stdin);
                printf("Please only enter numbers!\n");
                sleep(1);
                showmenu();
            }
        }


        switch(choice)
        {
            case '1':
            {
                createG:;
                clrscr();

                printf("Enter name or press 0 to return\n");
                scanf("%s", buffer);
                status = checkString(buffer);
                if(status == 1)
                {
                    clrscr();
                    break;
                }
                clrscr();       
                name = (char*)malloc(strlen(buffer)+1);
                strcpy(name, buffer);
                printf("New game created, welocome %s!\n");
                for(int i = 0; i < 5; i++)
                {
                    sleep(1);
                    printf("%d\n", i);
                }
                break;
            }

            case '2': 
            {
                int what;
                caseL:;
                clrscr();
                printf("No saves!\n Create new game? [Y/N] \n to return press 0\n");
                scanf(" %c", &YN);

                if(YN == 'N') what = 0;
                if(YN == '0') what = -1;
                if(YN == 'Y') what = 1;
                switch(what)
                {
                    case 0:
                    {
                        goto caseL;
                    }

                    case -1:
                    {
                        break;
                    }

                    case 1:
                    {
                        choice = 1;
                        goto createG;
                        break;
                    }

                }

                break;
            }

            case '3':
            {
                periods("Hello World");
                break;
            }

            case '4': 
            {
                clrscr();
                periods("Goodbye");
                clrscr();
                exit(1);
            }

            default:
            {
                printf("Wrong input\n Try again");
                sleep(1);
                break; 
            }
        }

    }while(choice != -2);


    return 0;
}

对于“意外”输入,这可能仍需要进行大量错误检查和处理,但可以解决问题。