LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-15-2011, 09:42 AM   #1
poonam.gaigole
LQ Newbie
 
Registered: Nov 2010
Posts: 3

Rep: Reputation: 0
An error in my c++ program... please help urgent.


The following is the error in my program :


lab1.cpp:80: error: ISO C++ forbids comparison between pointer and integer


Here is my program...

//Convert an infix expression to postfix expression...

#include<iostream>
#include<string>
#include<cstdlib>
using namespace std;
char ifx[50],pfx[50],stk[50];
int top=-1,n;
void push(char ch)
{
if(top!=n)
{
ch=stk[top];
top++;
cout<<"\nElement added was "<<ch;
}
else
cout<<"\nThe stack is full.";
}
void pop()
{
char rmv;
if(top!=-1)
{
rmv=stk[top];
top--;
cout<<"\nElement removed was "<<rmv;
}
else
cout<<"\nThe stack is empty.";
}
void topele()
{
char t;
if(top!=-1)
t=stk[top];
else
{
t='#';
cout<<"\nThere are no elements in the stack.The stack is: "<<t;
}
}
int chkpres()
{
char ch;
switch(ch)
{
case '^': return 5;
break;
case '*': return 4;
break;
case '/': return 3;
break;
case '+': return 2;
break;
case '-': return 1;
break;
default: exit(1);
break;
}
}
int main()
{
char pre,pres,ele,elem,chk,poppy;
cout<<"\nEnter how many elements you want to enter in the infix expression: ";
cin>>n;
for(int j=0;j<n;++j)
{
cout<<"\nEnter the characters of the infix expression one by one: ";
cin>>ifx[j];
}
for(int i=0;ifx[i]='\0';++i)
{
if(ifx[i]=='(')
{
exit(1);
}
stk[-1]='#';
pfx[0]='#';
if(ifx[i]=='^'||ifx[i]=='*'||ifx[i]=='/'||ifx=='+'||ifx[i]=='-')
{
if(stk[top]=='^'||stk[top]=='*'||stk[top]=='/'||stk[top]=='+'||stk[top]=='-')
{
pre=ifx[i];
pres=stk[top];
chk=chkpres();
if(pre>pres)
{
stk[top]=pre;
top++;
}
else if(pres>=pre)
pfx[i]=pres;
}
}
else if(ifx[i]=')')
{
while(stk[top]!='#')
{
poppy=stk[top];
pfx[i]=poppy;
}
}
}
cout<<"\nThe postfix expression is: ";
for(int i=0;pfx[i]!='\0';++i)
{
cout<<pfx[i];
}
return 0;
}

Please help me soon...
 
Old 03-15-2011, 09:54 AM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
this is not urgent for us, please don't mark it as such.
 
1 members found this post helpful.
Old 03-15-2011, 10:05 AM   #3
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Quote:
if(ifx[i]=='^'||ifx[i]=='*'||ifx[i]=='/'||ifx=='+'||ifx[i]=='-')
Should have been
Code:
if(ifx[i]=='^'||ifx[i]=='*'||ifx[i]=='/'||ifx[i]=='+'||ifx[i]=='-')
Next time, please use CODE tags when posting code.

Also, look at the error messages yourself. It tells you the error was in a comparison on line 80. You could have found your typo faster yourself with a careful look at line 80, than by asking here.

I usually ignore the word "urgent" in a post or title. Others may ignore the whole post. Anyway, it doesn't help.

Quote:
for(int i=0;ifx[i]='\0';++i)
Unfortunately, some typos don't yield compile time errors. Run time errors are harder to track down.

Quote:
stk[-1]='#';
What do you expect that to accomplish?

Last edited by johnsfine; 03-15-2011 at 10:15 AM.
 
1 members found this post helpful.
Old 03-16-2011, 12:02 AM   #4
poonam.gaigole
LQ Newbie
 
Registered: Nov 2010
Posts: 3

Original Poster
Rep: Reputation: 0
Modified ... but still no output... please help...

The program is:

//Convert an infix expression to postfix expression...

#include<iostream>
#include<string>
#include<cstdlib>
using namespace std;
char ifx[50],pfx[50],stk[50];
int top=-1,n;
char push(char ch)
{
if(top!=n)
{
ch=stk[top];
top++;
return ch;
}
else
cout<<"\nThe stack is full.";
}
void pop()
{
char rmv;
if(top!=-1)
{
rmv=stk[top];
top--;
cout<<"\nElement removed was "<<rmv;
}
else
cout<<"\nThe stack is empty.";
}
char topele()
{
char t;
if(top==-1)
t='#';
else
t=stk[top];
return t;
}
int chkpres()
{
char ch;
switch(ch)
{
case '^': return 5;
break;
case '*': return 4;
break;
case '/': return 3;
break;
case '+': return 2;
break;
case '-': return 1;
break;
default: exit(1);
break;
}
}
int main()
{
char pre,pres,ele,elem,chk,popp,topp;
cout<<"\nEnter how many elements you want to enter in the infix expression: ";
cin>>n;
topp=stk[top];
cout<<"\nEnter the characters of the infix expression one by one: ";
for(int i=0;i<n;++i)
{
cin>>ifx[i];
//for(int i=0;ifx[i]='\0';++i)
//{
if(ifx[i]!='^' && ifx[i]!='*' && ifx[i]!='/' && ifx[i]!='+' && ifx[i]!='-')
pfx[i]=ifx[i];
else if(ele=='(')
{
ele=ifx[i];
push(ele);
top++;
}
else if(ifx[i]=='^'||ifx[i]=='*'||ifx[i]=='/'||ifx[i]=='+'||ifx[i]=='-')
{
if(topp=='^'||topp=='*'||topp=='/'||topp=='+'||topp=='-')
{
pre=ele;
pres=topp;
chk=chkpres();
if(pre>pres)
{
topp=pre;
top++;
}
else if(pres>=pre)
pfx[i]=pres;
}
}
else if(ele=')')
{
while(topp!='#')
{
popp=topp;
pfx[i]=popp;
}
}
// }
}
cout<<"\nThe postfix expression is: ";
for(int i=0;pfx[i]!='\0';++i)
{
cout<<pfx[i];
}
return 0;
}



OUTPUT is:



Enter how many elements you want to enter in the infix expression: 9

Enter the characters of the infix expression one by one: a
*
(
b
+
c
)
/
d

The postfix expression is: a

Could you please help me to fix the error...
 
0 members found this post helpful.
Old 03-16-2011, 07:15 AM   #5
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
You seem to have ignored most of what I said in post #3.

You should examine your whole program for where you have still misused = as if it were a comparison operator.

Also take a careful look at what your push function does and how you use it. There appear to be at least three different bugs in that. With so many bugs, it is hard to estimate what you really intended, so hard to say exactly which specific places are the bugs.

I expect there is a lot more than that wrong. But without CODE tags and proper indenting, I have no interest in taking a closer look to tell you more than is obvious at first glance.

Last edited by johnsfine; 03-16-2011 at 07:24 AM.
 
4 members found this post helpful.
Old 03-16-2011, 08:40 AM   #6
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
For something with such urgency, you would think it would be worthwhile to at least post in code tags to speed up scrutiny of the code. Sheesh.
--- rod.
 
1 members found this post helpful.
Old 03-16-2011, 08:01 PM   #7
HiredGun555
LQ Newbie
 
Registered: May 2007
Distribution: Ubuntu
Posts: 16

Rep: Reputation: 0
char pre,pres,ele,elem,chk,popp,topp,str;
cout<<"\nEnter how many elements you want to enter in the infix expression: ";
cin>>n;
topp=stk[top];
cout<<"\nEnter the characters of the infix expression one by one: ";
for(int i=0;i<n;++i)
{
cin>>ifx[i];
//for(int i=0;ifx[i]='\0';++i)
//{
if(ifx[i]!='^' && ifx[i]!='*' && ifx[i]!='/' && ifx[i]!='+' && ifx[i]!='-')
pfx[i]=ifx[i];
else if(ele=='(')
{
ele=ifx[i];
push(ele);
top++;
}

when all else fails, use cout to output the variable values to see what's going on. if you insert the line
cout << ele << endl;
after your 2 commented lines, you will see that 'ele' is never assigned. also, good form dictates initializing variables after the declaration, not in the middle of code where it's difficult to understand.

i'm not going to help you with the logic (it's slightly convoluted), you need to learn that on your own through many examples, but cout a variable when in doubt. it's refer to as a trace variable.
 
Old 03-16-2011, 08:12 PM   #8
SigTerm
Member
 
Registered: Dec 2009
Distribution: Slackware 12.2
Posts: 379

Rep: Reputation: 234Reputation: 234Reputation: 234
Quote:
Originally Posted by johnsfine View Post
Should have been
Code:
if(ifx[i]=='^'||ifx[i]=='*'||ifx[i]=='/'||ifx[i]=='+'||ifx[i]=='-')
Why not use
Code:
if (strchr("^*/+-", ifx[i]) != NULL)
instead?
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
need Exploit codes for an echo program in C(very urgent) jimmy1300 Programming 2 03-14-2011 02:27 PM
Urgent : how do I know which program generates network traffic ? jonaskellens Linux - Networking 7 12-08-2008 06:50 AM
Strange C program -- urgent! vinay_s_s Programming 9 02-17-2004 09:44 PM
need same urgent help with c program black111 Programming 2 05-25-2003 05:06 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:28 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration