#include <stdio.h>
#include <string.h>
int main(){
char *command="0";
do {
printf("[A]dd, [P]rint, [Q]uit\n");
scanf("%s", command);
while (strcmp(command, "a") != 0 && strcmp(command, "A") != 0 && strcmp(command, "p") != 0 && strcmp(command, "P") != 0){
printf("Invalid input. Please enter one of the commands listed above.\n");
scanf("%s", command);
}
if (strcmp(command, "a") == 0 || strcmp(command, "A") == 0){
printf("You selected add.\n");
}
else if (strcmp(command, "p") == 0 || strcmp(command, "P") == 0){
printf("You selected print.\n");
}
}while (strcmp(command, "q") != 0 && strcmp(command, "Q")!= 0);
return 0;
}
I want the program to take in a letter from the user from one of the specified commands printed in the beginning. I want the program to exit if they enter q or Q. Took me a while simply to figure out how to do comparisons with strings for the loops and ifs. now when i run the program it crashes though. Looking for insight as to why its crashing.
Yes, make your buffer
commandinto a regular array, and make it larger:No need to initialize it,
scanfwill take care of that. Also, it won’t affect the crashing but if you’re only checking one letter, you can do it like this:Note the single quotes: This is a character comparison.