wrong answer//thanks guy I solved

1. Evaluate a postfix expression
0. Exit
Enter the number for the option: 1
Evaluate a postfix expression
Enter the expression: 34*873-//4+

The Postfix Expression entered is: 34*873-//4+ and the value of the Postfix Expression is: 3.95833 but the answer must be 10, what is wrong with the calculations



#include"stack.h"
#include<string>
#include<iostream>
using namespace std;

int main()
{
int i, choice;
string postfixExp;
char token;
float value, value1, value2;
//Stack <char> stack_one;
Stack <float> stack_one;



while (choice != 0)
{
cout << "1. Evaluate a postfix expression" << endl;
cout << "0. Exit " << endl;
cout << "Enter the number for the option: ";

cin >> choice;

switch(choice)
{
case 1: cout << "Evaluate a postfix expression\n";
cout << "Enter the expression: ";
cin >> postfixExp;
i = 0;
token = postfixExp[i];

while((i < postfixExp.size()) && (token != '='))
{
if(isdigit(token))
{
value = token;
stack_one.push(value);
}
else
{
value2 = stack_one.peek(value);
stack_one.pop(value2);
value1 = stack_one.peek(value);
stack_one.pop(value1);

switch(token)
{
case '+': value = value1 + value2;
break;
case '-': value = value1 - value2;
break;
case '*': value = value1*value2;
break;
case '/': value = value1/value2;
break;
}
stack_one.push(value);
}

i++;

token = postfixExp[i];
}

stack_one.isEmpty();
value = stack_one.peek(value);
stack_one.pop(value);

cout <<"\nThe Postfix Expression entered is: " << postfixExp <<" and the value of the Postfix Expression is: "<< value << endl;
break;

case 0: cout << "Exiting the program\n";
break;

default: cout << "Invalid option\n";
break;
}
cout << endl;
}
}
Last edited on
Please use [code][/code].
Check stack.h because seems 'stack' wasn't declared.

I don't quite understand the following part of your code:
1
2
3
4
5
6
7
8
while (choice != 0)
#include"stack.cpp"
#include<string>
#include<iostream>
using namespace std;

int main()
{
Was that a copy/paste error?
You should NEVER #include .cpp files
Topic archived. No new replies allowed.