I'm having to learn c++ on the fly with my data structure class. I only previously have experience with java which is why my error feels trivial but I am struggling to figure it out. I am getting an 'expected expression' error and am unsure what it is telling me. I have tried swapping out my literal expressions out for ASCii code values and that sort of thing. The error occurs on all of my if statements. I do have all of my needed '#include' statements. Thanks for any help.
stack<double> doublestack;
char *input = new char[255]();
char *token = new char[255]();
cout << "Enter a valid postfix expression" << endl;
cin.getline(input, 255);
token = strtok(input, " ");
while (token != NULL) {
if (token* == '*')
{
double b = doublestack.top();
double a = doublestack.top();
doublestack.pop();
doublestack.pop();
doublestack.push(b * a);
}
else if (token* == '/')
{
double b = doublestack.top();
double a = doublestack.top();
doublestack.pop();
doublestack.pop();
doublestack.push(b / a);
}
else if (token* == '+')
{
double b = doublestack.top();
double a = doublestack.top();
doublestack.pop();
doublestack.pop();
doublestack.push(b + a);
}
else if (token* == '-')
{
double b = doublestack.top();
double a = doublestack.top();
doublestack.pop();
doublestack.pop();
doublestack.push(b - a);
}
else
{
doublestack.push(atof(token));
}
token = strtok(NULL, " ");
}
If it helps to know, I am getting a postfix expression and pushing the numbers into a stack. When I find an operational character, I will pop the top two numbers to perform the operation. Once the postfix is finished, I should be left with one number and that is the answer to the postfix expression.
The error messages are as follows...
stack1.cpp:24:20: error: expected expression
if (token* == '*')
^
stack1.cpp:32:25: error: expected expression
else if (token* == '/')
^
stack1.cpp:40:25: error: expected expression
else if (token* == '+')
^
stack1.cpp:48:25: error: expected expression
else if (token* == '-')
In this case, a = b
double b = doublestack.top();
double a = doublestack.top();
doublestack.pop();
doublestack.pop();
guess you u want get last value(top()), then remove it from stack(pop()), then get next last value and so on
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.