我正在用 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/