Xender1

February 22nd, 2010, 08:10 PM

I am writing, the user enters data from stdin and it is read into a char* variable. I then use strtok on this variable with what it gets the chars between is a space. For some reason on the last character, it reads it in but then even though I have if statements to have it do something out, puts it on the stack. Here is the code.

(the stack just has an array of int contents[100] and a variable top to keep track of where in the array we are.)

stack thestack; init(&thestack);

char *input = NULL;

int maxin = 100; //for the getline function

printf("Enter equation in RPN: ");

getline(&input,&maxin,stdin);

fillstack(&thestack,input);

this is the fillstack function

void fillstack(stack *thestack, char *input) {

char *result = NULL; //variable to read in each part of input

result = strtok(input, " ");

while (result != NULL) {

printf("result = %s\n",result);

if (strcmp(result,"+")==0) {add(thestack);}

else if (strcmp(result,"-")==0) {subt(thestack);}

else if (strcmp(result,"*")==0) {mult(thestack);}

else if (strcmp(result,"/")==0) {divi(thestack);}

//if not a operator push number on the stack

else {push(thestack,atoi(result));}

result = strtok(NULL," ");

}

}

now lets say the user enters 1 2 + 2 -

this is what result will show

result = 1

result = 2

result = + , ADD

result = 2

result = -

so i know that it reads in the - sign, but when looking at the stack we have

3 (from the 1 + 2), then 2 (from the second 2), then 0. where does that 0 come from and why is the subtraction call not being called?

Thanks for any time and help given.

(the stack just has an array of int contents[100] and a variable top to keep track of where in the array we are.)

stack thestack; init(&thestack);

char *input = NULL;

int maxin = 100; //for the getline function

printf("Enter equation in RPN: ");

getline(&input,&maxin,stdin);

fillstack(&thestack,input);

this is the fillstack function

void fillstack(stack *thestack, char *input) {

char *result = NULL; //variable to read in each part of input

result = strtok(input, " ");

while (result != NULL) {

printf("result = %s\n",result);

if (strcmp(result,"+")==0) {add(thestack);}

else if (strcmp(result,"-")==0) {subt(thestack);}

else if (strcmp(result,"*")==0) {mult(thestack);}

else if (strcmp(result,"/")==0) {divi(thestack);}

//if not a operator push number on the stack

else {push(thestack,atoi(result));}

result = strtok(NULL," ");

}

}

now lets say the user enters 1 2 + 2 -

this is what result will show

result = 1

result = 2

result = + , ADD

result = 2

result = -

so i know that it reads in the - sign, but when looking at the stack we have

3 (from the 1 + 2), then 2 (from the second 2), then 0. where does that 0 come from and why is the subtraction call not being called?

Thanks for any time and help given.