how do i overcome Stack Exception processor fault in ths program........?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Sai Vamshi
    New Member
    • Apr 2015
    • 1

    how do i overcome Stack Exception processor fault in ths program........?

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    void main()
    {
    char istr[100],tstack[100],ostr[100];
    int i=0,top=0,j=0;
    printf("Enter the infix expression...\n");
    scanf("%s",&istr);
    printf("The infix expression is :%s\n",istr);
    while(istr[i]!='\0')
    {
    switch(istr[i])
    {
    case '^':
    case '%':
    case '/':
    case '*':
    			if(tstack[top]=='+'||tstack[top]=='-'||tstack[top]=='(')
    			{
    			top++;
    			tstack[top]=istr[i];
    			}
    			else if(tstack[top]=='\0')
    			tstack[top]=istr[i];
    			else
    			{
    			while(tstack[top]!='\0')
    			{
    			ostr[j]=tstack[top];
    			top--;
    			j++;
    			}
    			if(top==-1)
    			top++;
    			tstack[top]=istr[i];
    			}
    			break;
    case '+':
    case '-':
    			if(tstack[top]=='(')
    			{
    			top++;
    			tstack[top]=istr[i];
    			}
             else if(tstack[top]=='\0')
    			tstack[top]=istr[i];
    			else
    			{
    			while(tstack[top]!='\0')
    			{
    			ostr[j]=tstack[top];
    			top--;
             j++;
    			}
    			if(top==-1)
    			top++;
    			tstack[top]=istr[i];
    			}
    			break;
    case '(':if(tstack[top]=='\0')
    			tstack[top]=istr[i];
    			else
    			{
    			top++;
    			tstack[top]=istr[i];
    			}
    			break;
    case ')':while(tstack[top]!='(')
    			{
    			ostr[j]=tstack[top];
    			top--;
    			j++;
    			}
    			tstack[top]='\0';
    			top--;
    			break;
    default :ostr[j]=istr[i];
    			j++;
    			break;
    }
    i++;
    }
    while(top!=-1)
    {
    ostr[j]=tstack[top];
    top--;
    j++;
    }
    printf("The postfix expression is :%s",ostr);
    }
    Last edited by zmbd; Apr 23 '15, 04:09 PM. Reason: [z{in the future please use the [CODE/] format for your posted script.}]
  • zmbd
    Recognized Expert Moderator Expert
    • Mar 2012
    • 5501

    #2
    We do not have your data nor CPU to compile and run so:
    What troubleshooting , if any, have you already done?
    What is the error code you are receiving - several reasons for stack exceptions?
    At what line is your code faulting?

    Comment

    • weaknessforcats
      Recognized Expert Expert
      • Mar 2007
      • 9214

      #3
      The first thing I notice is all the arrays are 100 elements but nowhere in the code is there a check to see if top++, i++, or j++ goes over 99. Also, top--is never checked to see a negative value occurs.

      This is an excellent time to use your debugger and step through the code.

      Comment

      Working...