H
Henry Jordon
This is a piece of code that I have left to complete my project. I
have hopefully one small error that needs to be fixed. This portion of
the code evaluates the postfix notation that is passed to it. I have
marked the error line. Thank you very much for your help.
void evaluates(char *postfix)
{
int position;
char number1, number2, number3=0;
char symbol, answer;
stack number_stack;
for(position=0; postfix[position] !='\0'; position++)
{
symbol=postfix[position];
if(symbol=='0'||symbol=='1'||symbol=='2'||symbol=='3'||symbol=='4'||symbol=='5'||symbol=='6'||symbol=='7'||symbol=='8'||symbol=='9')
{
number_stack.push(symbol);
}
else
{
number2=number_stack.pop();
number1=number_stack.pop();
if(symbol=='+')
{
number3=number1+number2;
}
else if(symbol=='-')
{
number3=number1-number2;
}
else if(symbol=='*')
{
number3=number1*number2;
}
else if(symbol=='/')
{
number3=number1/number2;
}
else if(symbol=='^')
{
number3=number2*number2;
}
number_stack.push(number3);
}
}
answer=number_stack.pop();
The error comes here cannot conver char to const char*. I want to
convert the answer into an integer value, not ASCII values
cout<<"The answer evaluates to: "<<atoi(answer)<<endl;
}
again thank you very much for your help.
have hopefully one small error that needs to be fixed. This portion of
the code evaluates the postfix notation that is passed to it. I have
marked the error line. Thank you very much for your help.
void evaluates(char *postfix)
{
int position;
char number1, number2, number3=0;
char symbol, answer;
stack number_stack;
for(position=0; postfix[position] !='\0'; position++)
{
symbol=postfix[position];
if(symbol=='0'||symbol=='1'||symbol=='2'||symbol=='3'||symbol=='4'||symbol=='5'||symbol=='6'||symbol=='7'||symbol=='8'||symbol=='9')
{
number_stack.push(symbol);
}
else
{
number2=number_stack.pop();
number1=number_stack.pop();
if(symbol=='+')
{
number3=number1+number2;
}
else if(symbol=='-')
{
number3=number1-number2;
}
else if(symbol=='*')
{
number3=number1*number2;
}
else if(symbol=='/')
{
number3=number1/number2;
}
else if(symbol=='^')
{
number3=number2*number2;
}
number_stack.push(number3);
}
}
answer=number_stack.pop();
The error comes here cannot conver char to const char*. I want to
convert the answer into an integer value, not ASCII values
cout<<"The answer evaluates to: "<<atoi(answer)<<endl;
}
again thank you very much for your help.