If i give my program this:
abcdefghijklmnopqrstuvwxyz
It answered with this?!
Invalid command a
Invalid command c
Invalid command e
Invalid command g
Invalid command i
Invalid command k
Invalid command m
Invalid command o
Invalid command q
Invalid command s
Invalid command u
Invalid command w
Invalid command y
Any idea why only every other letter is getting my default message, and why there is so many and not just one, how can i fix this so i only get one default message no matter how many invalid characters i input?
Here is that part of my code:
while (1) {
scanf("%c", &command);
getchar();
switch (command) {
case 'A': // Adds a new entry to the program database.
if (scanf("%s %f", name, &price) != 2) {
printf("A should be followed by exactly 2 arguments.\n");
// while (getchar() != '\n'); // Clear the input buffer
} else {
getchar(); // Consume the newline character
addGame(&games, &numGames, name, price); // Pass pointer to games
}
break;
case 'B': // Buy game command, which sells a specified number of games
if (scanf("%s %d", name, &count) != 2 || count <= 0) {
getchar(); // Consume the newline character
printf("Number of bought items cannot be less than 1.\n");
} else {
getchar(); // Consume the newline character
buyGame(games, numGames, name, count);
}
break;
case 'L': //Prints a list of the program database into the standard output
printDatabase(games, numGames);
break;
case 'W': //Writes the program database into a text file
scanf("%s", filename);
getchar();
saveToFile(games, numGames, filename);
break;
case 'O': //Loads the database from the specified text file
scanf("%s", filename);
getchar();
loadFromFile(&games, &numGames, filename); // Pass pointer
break;
case 'Q': //Quits the program after releasing the program database
releaseMemory(games, numGames);
return 0;
case '\n': //Not sure i need this?
break;
default:
printf("Invalid command %c\n", command);
}
}
I can send more of my code if someone think it's needed :)