void repl (){ while ( 1 ){ while ( words [ a ]!= \'\\0\' ){ if ( isNumeric ( wor
ID: 3551532 • Letter: V
Question
void repl(){ while(1){ while(words[a]!=''){ if(isNumeric(words[a])){ push(words[a]); }else if(strcmp(words[a],"add")==0){ strcpy(value1, pop()); strcpy(value2, pop()); sum2 = (int)atoi(value1) + (int)atoi(value2); itoa(sum2,sum); printf("%d",sum2); } a++; if(words[a] == ''){ printStack(); } }
what I need to care about this code is to receive numbers as char temp[] and check them that they are numeric or not.
however I succeed that part of thing, but what I need to solve now is to check if the words[a] is add
then pop last 2 value to calculate sum of them and push back sum into stack.
while I did on this code I received segmentation fault
repl>1 2 3 4 4 3 2 1 repl>add Segmentation fault
Explanation / Answer
Since I do not have access to your code, I will list down possible sources of errors:
1) Please ensure that the datatype are as:
char* value1; char* value1; int sum2; char* sum;
2) I am not sure if you can call your pop() function directly (depends on your implementation).
for example: calling pop() for stack s would generally be s.pop(), but here since you are implementing stack using array (words[]), it depends on how you have implemented. So, in absence of complete code with function definitions, I suspect you get segmentation fault in pop() statement.
WHAT should you do: try using pop before somewhere else and store the result in some variable. Print that variable to check if pop() is giving results correctly i.e. debug pop() first.
3) Also, after calculating sum, you forgot to push back the result onto the stack (that is not the error in this case but something that may bother you later).
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.