Warning, I'm super new with C language (and coding) so please explain to me like I'm simple :) So, I'm trying to get my case 'A' and case 'B' to give this message "printf("A(or B) should be followed by exactly 2 arguments.\n");" if zero arguments are given(just pressing enter), or only one, or more than 2. Now it acts like this:
From my terminal:
O Pelit_tiedosto.txt
SUCCESS
L
Mario 10.99 131.88
GameName....WithSomeDots 10.99 120.89
Tetris 5.99 59.90
Street_Fighter 6.99 41.94 SUCCESS
B Mario 5 SUCCESS
B Mario
7
SUCCESS
B
(pressing enter multiple times)
mario
6
Game not found: mario
B (pressing enter multiple times)
Mario
h
Number of bought items cannot be less than 1.
Invalid command h
Here is that part of my code:
while (1) {
scanf(" %c", &command);
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");
} 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':
printf("SUCCESS\n");
releaseMemory(games, numGames);
return 0;
case '\n':
break;
default:
printf("Invalid command %c\n", command);
// Clear the input buffer
while (getchar() != '\n');
}
}
Hopefully my explanation was understandable, English is not my first language :) Thank you for your help in advance!