簡體   English   中英

如何正確制作動態命令行菜單?

[英]How to properly make a dynamic command-line menu?

更准確地說,如何像所有其他軟件和游戲一樣添加“返回主菜單”功能?

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更有意義,但我認為它不能解決我的問題。

這是一個工作示例。 解決這個問題的方法就是簡單地制作一種“菜單內的菜單”,我確實使用了一些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;

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

    do{ 


        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(YN)
                {
                    case 'N':
                    {
                        goto caseL;
                    }

                    case '0':
                    {
                        break;
                    }

                    case 'Y':
                    {
                        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;
}

對於“意外”輸入,這可能仍需要進行大量錯誤檢查和處理,但可以解決問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM