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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
03-15-2011, 09:42 AM
|
#1
|
LQ Newbie
Registered: Nov 2010
Posts: 3
Rep:
|
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...
|
|
|
03-15-2011, 09:54 AM
|
#2
|
Moderator
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417
|
this is not urgent for us, please don't mark it as such.
|
|
1 members found this post helpful.
|
03-15-2011, 10:05 AM
|
#3
|
LQ Guru
Registered: Dec 2007
Distribution: Centos
Posts: 5,286
|
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.
What do you expect that to accomplish?
Last edited by johnsfine; 03-15-2011 at 10:15 AM.
|
|
1 members found this post helpful.
|
03-16-2011, 12:02 AM
|
#4
|
LQ Newbie
Registered: Nov 2010
Posts: 3
Original Poster
Rep:
|
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.
|
03-16-2011, 07:15 AM
|
#5
|
LQ Guru
Registered: Dec 2007
Distribution: Centos
Posts: 5,286
|
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.
|
03-16-2011, 08:40 AM
|
#6
|
LQ 5k Club
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
|
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.
|
03-16-2011, 08:01 PM
|
#7
|
LQ Newbie
Registered: May 2007
Distribution: Ubuntu
Posts: 16
Rep:
|
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.
|
|
|
03-16-2011, 08:12 PM
|
#8
|
Member
Registered: Dec 2009
Distribution: Slackware 12.2
Posts: 379
|
Quote:
Originally Posted by johnsfine
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?
|
|
|
All times are GMT -5. The time now is 10:33 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|