c - 如何制作一个在C中重复的菜单

标签 c menu

我正在用 C 编写一个文件压缩器程序,我正在创建一个显示在程序开头的友好菜单。

这是简化的代码:

压缩.c

int main()
{

    char directory[100];
    char option;
    bool exit = false;

    do
    {

        printf("|------------------------------------|\n");
        printf("|             Welcome!               |\n");
        printf("|                                    |\n");
        printf("|Select one of the following options:|\n");
        printf("|    1 - Compress a file             |\n");
        printf("|    2 - Decompress a file           |\n");
        printf("|    3 - Exit                        |\n");
        printf("|------------------------------------|\n");

        scanf(" %c", &option);

        switch(option)
        {
            case '1':
                printf("Enter the FULL directory or drag HERE the file you want to COMPRESS: \n");
                scanf(" %s", directory);

                /* COMPRESS FILE */

                break;

            case '2':
                printf("Enter the FULL directory or drag HERE the file you want to UNCOMPRESS: \n");
                scanf(" %s", directory);

                /* DECOMPRESS FILE */

                break;

            case '3':
                exit = true;
                break;

            default:
                printf("Invalid option!\n\n");
                break;
        }
    }while(exit == false);

    return 0;
}

例如,在输入“1”并输入正确的目录后,程序会打印 10 次菜单并返回 0。但是当我输入无效或不存在的目录时,它会起作用,因为它只打印 1 次菜单并等待用户再次输入选项!

为什么要这样做?

最佳答案

scanf("%s") 更改为此,成功了:

getchar();
gets(directory);

//AFTER READING THAT DIRECTORY, WE NEED TO CHECK IF IT HAS QUOTES ON IT:
if(directory[0] == '\"')  //IF IT HAS, ENTER HERE
{
    /* REMOVE ALL QUOTES IN THE DIRECTORY */
}

关于c - 如何制作一个在C中重复的菜单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58957287/

相关文章:

c++ - 在 C++ 中的指针数组中,内存过度使用是否会导致指针存储在非连续的内存块中?

c - recv() 没有从网络获取值

android - 像在 Navigation android 应用程序中一样滑动菜单

.net - 如何在所有者绘制的菜单项中提供动画?

android - 通过(弹出)菜单退出应用程序时窗口泄漏

c - 运行功能后菜单获得默认选项

c - 新手语法问题 : error: expected ';' before ')' token

c++ - 使用堆内存(malloc/new)会创建一个不确定的程序吗?

c - 如何对 Xlib 中的鼠标滚轮使用react?

c - 如何修复 C 语言中的 switch case 菜单错误?