0
#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.

4
  • May help to include the error message you're getting. Commented Mar 4, 2012 at 22:34
  • Where does it crash? When you attach a debugger, what is the state of the program when it crashes? Does the actual state match your expected state? Commented Mar 4, 2012 at 22:35
  • You can't change the contents of command: it points to a string literal. Try an array instead: char command[] = "0";. and be sure to limit the length of the string read with the scanf: scanf("%1s") Commented Mar 4, 2012 at 22:35
  • scanf is trying to write N bytes to the address pointed by command, which is a string-literal (read-only). You need to allocate enough memory to store these N bytes, or declare a fixed-length array of chars. Example: char command[255]; scanf("%254s", command); Commented Mar 4, 2012 at 22:37

4 Answers 4

1

Yes, make your buffer command into a regular array, and make it larger:

char command[256];

No need to initialize it, scanf will 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:

if command[0] != 'a' && command[0] != 'A' (etc.)

Note the single quotes: This is a character comparison.

Sign up to request clarification or add additional context in comments.

Comments

0
char *command="0";
scanf("%s", command);

you are writing to a string literal (of only 2 bytes) and string literals are not required to be modifiable.

Use something like this instead:

char command[256];
scanf("%s", command);

or better use fgets + sscanf to avoid potential buffer overflows.

Comments

0

This is a pointer to read-only memory:

char *command="0";

Which means that this is wrong:

scanf("%s", command);

The "quick hack", which is the bad way to do it, is change the definition of command so it gets some read-write memory instead of read-only memory:

char command[256] = "0";

But this still puts you in grave danger of buffer overflow. Don't use scanf.

The "better way" is to use fgets instead of scanf:

char command[256];
char *p;
p = fgets(command, sizeof(command), stdin);
if (!p) { handle error or EOF }
/* Don't forget that 'command' will probably have '\n' at the end,
   you will have to strip it off */

4 Comments

fgets and sccanf better in this case?
The format string passed to scanf allows limiting the string size. Example: "%255s"
@jweyrich: That's nice, but there are still 99 other reasons not to use scanf.
@TristanPearce: Avoid sscanf, scanf, fscanf, vfscanf, vscanf, and vsscanf. Using them correctly is too tricky.
0

Well, the first thing that pops out at me is that you're using scanf to read data into read-only memory -- i.e., a string constant. What happens when you try this is undefined, but a program crash is likely. You need to allocate some read/write memory for command; declare it as, for example,

char command[10];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.