comments creating a problem

I know it sounds weird but when i uncomment statements in the following ,it works perfectly fine for infix to postfix conversion.although comments are simple cout statements,,execution with cout statements as comments breaks in b/w.
I have found this particularly with a^b-c.
DevCPP is also showing similar behavior.
I can't understand what's going on.I am working on code blocks. Please help!!

#include<iostream>
#include<cstdio>
#include<conio.h>
using namespace std;
char stk[15];
int top=-1;
void push(char c)
{
top++;
stk[top]=c;
}
char pop()
{
char c=stk[top];
top--;
return c;
}
int prec(char a)
{
switch(a)
{
case '^':
return 5;
break;
case '/':
return 4;
break;
case '*':
return 3;
break;
case '+':
return 2;
break;
case '-':
return 1;
}
}

int main()
{
char in[20],out[20];
cin.getline(in,20);
//cout<<endl<<in;
int i,j=-1,a;
for(i=0;i<20;i++)
out[i]=NULL;
for(i=0;in[i]!=NULL;i++)
{
if(in[i]>='a'&&in[i]<='z')
{
j++;
out[j]=in[i];
// cout<<endl<<out;
}
else{
a=prec(in[i]);
// cout<<endl<<a;
if(top>=0)
{
while(1)
{
if(prec(stk[top])>a)
{
j++;
out[j]=pop();
// cout<<endl<<out;
}
else
break;
}
}
push(in[i]);
}
}
while(top>=0)
{
j++;out[j]=pop();
// cout<<endl<<out;
}
cout<<endl<<out;
getch();
return 0;
}

Thanx for any help i get in advance..
Last edited on
Put you code in ["code"]["/code"] tags please
You will also receive more help if you said what your program was doing

1
2
3
4
5
6
7
8
char stk[15];

char pop()
{
	char c=stk[top];
	top--;
	return c;
}


Look at the above code and ask yourself why it is most likely not to work

1
2
for(i=0;i<20;i++)
  out[i]=NULL;


If you want to do this, you have to define NULL as zero then do it:
#define NULL 0 . Then you can also do this for(i=0;in[i]!=NULL;i++)

Another way to do it is for(i=0;i < (int)(sizeof in / sizeof *in);i++)
Thanx..
The program is doing "infix to postfix" conversion.
I am really not able to find either syntactical or logical error in the code you have asked to do so.
["code"]
#define NULL 0
["\code"]
is not helping. And moreover i have been using NULL since long as a keyword.
It is really a confusion how the code works correct by some cout statements as you see uncommenting the comments in the above code makes it work well,specifically for a^b-c as input.
please help!!!
Last edited on
Topic archived. No new replies allowed.