-1

I have to take text input in the form of "add # # ... #" (# just being a number) and create and store the values as vertices in a struct. Whenever I input "add" I get a segmentation fault. I'm confident it's cuz my usage of pointers is wrong, but I'm not sure how. This is for an intro to C course so I'd appreciate help that follows that level of simplicity.

Below is my main file that calls my add function (handleAdd)

int main() {
    printf("Create a polygon using the add command. Valid commands are add, summary, turn, shift, and quit.\n");
    while (1) { //infinite while loop...
        char* cmdLine;
        char* command;
        char* input;
        printf(">>");
        gets(cmdLine);
        command = strtok(cmdLine, " "); //first word
        input = strtok(NULL, ""); //dont understand

        if (strcmp(command, "quit") == 0) //if command == "quit"
             break;
        else if (!strcmp(command, "add")) { //if command == "add"
            if ((input) == NULL) //add\n
                printf("Too few aguments for add command! It must be in the form of add x1 y1 x2 y2 xn yn.\n");
            else
                printf("test1");

            handleAdd(input);
        }

The next code is the actual handleAdd function

handleAdd(char* addCommand) {
   printf("test1");
   char addCmds[50];
   printf("test2");

   while (addCommand != NULL) {
       printf("test1");
       int i = 0;
       //addCmds[i];
       addCommand = strtok(NULL," ");
   }

   size_t cmdLength = strlen(addCommand);

   for (int i = 1; i <= cmdLength; i++) { //i = 1 becaue 0 has "add"
       if (*(addCommand + i) % 2 == 0) { //if this is even, assign to yCoords
           *(yCoords+(i-1)) = *(addCommand+i); //doubtful it works
       }

       if (*(addCommand + i) % 2 != 0) { //if this is odd, assign to xCoords
           *(xCoords+(i-1)) = *(addCommand+i); //doubtful it works
       }
   } 
}
Chris
  • 26,361
  • 5
  • 21
  • 42
Jack
  • 9
  • 1
  • 1
  • 2
    `char* cmdLine` . That's an uninitialised pointer. You need to point it to a valid memory buffer before attempting to use it for writing into. Aside: [Why is the gets function so dangerous that it should not be used?](https://stackoverflow.com/questions/1694036/why-is-the-gets-function-so-dangerous-that-it-should-not-be-used). – kaylum Nov 04 '21 at 21:42
  • 1
    Never use `gets`. It's been deprecated for many years and was actually removed from the language in C11 because it's _impossible_ to use it safely. It's not just hard - it's impossible. – Ted Lyngmo Nov 04 '21 at 21:45
  • 1
    You should turn on all compiler warnings. They will often point out problems for you. `-Wall` even warns about misleading indentation. – Raymond Chen Nov 04 '21 at 21:50
  • Your code is very difficult to read. You should format your code like the samples in your learning material. It's very hard to work with poorly formatted code, even for professionals. – Jabberwocky Nov 04 '21 at 21:53

1 Answers1

0

You declared cmdLine but you did not specify it as an array or assign some memory using malloc. Make it an array (its the simplest solution) like this: char cmdLine[100];. And read the input using fgets(cmdLine, 99, stdin); I used 99 to leave room for the trailing NUL, part of every string.
Also, don't make command and input into arrays. Leave them be because strtok allocates memory on its own.

ADBeveridge
  • 650
  • 3
  • 15