I'm making a file compressor program in C, and I'm creating a friendly menu that appears in the beginning of the program.
That's the simplified code:
Compress.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;
}
After typing '1', for example, and typing a CORRECT directory, the program prints 10 times the menu and returns 0. But when I type an INVALID or NONEXISTENT directory, it works, because it prints only 1 time the menu and waits for user types the option again!
Why is it doing that?
Changing scanf(" %s") to this, did the trick:
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 */
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With