int main(int argc, char *arv[])
{
printf("Enter sequence of numbers seperated by comma\n");
char input[200];
char *valueStr = NULL;
int total = 0;
int values[100];
int ctr = 0, i = 0;
if (fgets(input, sizeof(input), stdin) != NULL)
{ // split using comma
printf("printing %s", input);
valueStr = strtok(input, ",");
printf("printing %s", valueStr);
while (valueStr != NULL && sscanf(valueStr, "%d", values[ctr++]) != EOF);
{
printf("Got value %d\n", values[ctr - 1]);
valueStr = strtok(NULL, ",");
}
for (i = 0; i < ctr; i++)
total +=values[i];
if (total == 0)
printf("Equals to 0\n");
else
printf("Doesn't equal to 0\n");
}
}
Seems very simple to me... What am I doing wrong? Currently the output is the following:
bash-4.2$ ./subset
Enter sequence of numbers seperated by comma
-5,-3,-1,2,4,6
printing -5,-3,-1,2,4,6
Segmentation fault (core dumped)
It never gets passed strtok() and I don't see why.. I'm not passing a string literal into the function!