You can't assign a value to pos. pos can't equal a digit or a char. pos is a constant address to the char array named (tada!) pos.
I believe you meant to use pos[i], everywhere you assign or check the value of pos. .
I can't see why your compiler isn't giving you warnings and errors. Do you have your alerts shut off or turned way down?
Anyway, welcome to the forum, and please highlight your code in the future, and click on the # icon near the smilies selector, at the top of the post reply (full) window. That will put your code into the forum as a program, rather than just plain text, and make it look much better.
Originally Posted by
Prateekr10
hey everyone, i have written a C program to evaluate the value of a postfix expression, but im not getting the desired output. The output always comes out to be ZERO at runtime, there is no compilation error,
please help me out quickly to identify the error, the program is as follows:->
Code:
#include<stdio.h>
#include<conio.h>
#include<ctype.h>
#define MAX 50
float stack[MAX];
int top=-1;
void push(float a)
{
if(top==MAX-1)
return;
else
stack[++top]=a;
}
float pop()
{
if(top==-1)
return -999;
else
return stack[top--];
}
int main() //not void main()
{
char pos[50];
float a, b, c,p;
int i=0, j=0;
clrscr();
printf("\n Enter the postfix expression");
gets(pos);
while(pos!='\0') //should be pos[i]
{
if(isdigit(pos)) //again pos[i]
{
p = (float)pos - 48; //(pos[i] - 48)
push(p);
}
else if( (pos=='*') || (pos=='/') || (pos=='-') || (pos=='+')) //all pos[i]
{
a = pop();
b = pop();
if(pos=='+') //pos[i]
c = b+a;
if(pos=='-') //ditto
c = b-a;
if(pos=='*') //etc.
c = b*a;
if(pos=='/')
c = b/a;
push(c);
}
else if(pos==32)
{
++i;
continue;
}
else
{
printf("\n Enter only digits and 4 operators");
j=1;
break;
}
++i;
}
if(j==1)
exit();
else
printf("\n The value of the postfix expression is = %f",stack[0]); //thank JacobN for this correction
getch();
return 0;
}
I have a large program running atm and can't run your program, yet. Have no idea if it works or not.