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
}
}
}