Stack-Based Calculator

I am writing a programmer for a basic C++ class that uses a 2 stacks in order to evaluate an equation. I have everything in order and even saw my teacher a couple of times and he helped me. But the problem is that whenever I input, nothing happens. It simply just either waits for me to type something else, or gives me a core dump error. Any Ideas? Here is the code.
#include "linkedStack.h"
#include <iostream>
#include <cmath>
#include <stdlib.h>
#include <string.h>
using namespace std;


struct operatorStruct
{
string op;
int precedence;
};


linkedStack<operatorStruct> operatorStack;
linkedStack<int> operandStack;

bool isoperator(string);
int getprecedence(string);
int calculate();

//Main Function
main()
{

cout<< "please enter an expression" << endl;





calculate();
return 0;
}

bool isoperator(string op)
{
if(op=="+"||op=="-"||op=="*"||op=="/")
{
return true;
}
return false;
}

int getprecedence(string op)
{
if(op=="+"||op=="-")
{
return 1;
}

else if(op=="*"||op=="/")
{
return 2;
}
}



int calculate()
{ string token;
cin >> token;
operatorStruct NewOp;
while(token != ";")
{
if(isdigit(token[0]))
{
int a=atoi(token.c_str());
operandStack.push(a);
}
else if(isoperator(token))
{
NewOp.op=token;
NewOp.precedence=getprecedence(token);



while(operatorStack.empty() != 0 &&
operatorStack.top().precedence >=
NewOp.precedence)
{
int num1=operandStack.top();
operandStack.pop();
int num2=operandStack.top();
operandStack.pop();
if(operatorStack.top().op =="*")
{
operatorStack.pop();
operandStack.push(num2*num1);
}
else if(operatorStack.top().op=="+")
{
operatorStack.pop();
operandStack.push(num2+num1);
}
else if(operatorStack.top().op=="-")
{
operatorStack.pop();
operandStack.push(num2-num1);
}
else if(operatorStack.top().op=="/")
{
operatorStack.pop();
operandStack.push(num2/num1);
}
}
operatorStack.push(NewOp);
}
}
int result= operandStack.top();
cout << result << endl;
}
could you please put your code in nice code boxes.
It makes it so much easier to read.
Allow me to indent that for you :-)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include "linkedStack.h"
#include <iostream>
#include <cmath>
#include <stdlib.h>
#include <string.h>
using namespace std;


struct operatorStruct
{
	string op;
	int precedence;
};


linkedStack<operatorStruct> operatorStack;
linkedStack<int> operandStack;

bool isoperator(string);
int getprecedence(string);
int calculate();

//Main Function
main()
{
	cout<< "please enter an expression" << endl;
	calculate();
	return 0;
}

bool isoperator(string op)
{
	if(op=="+"||op=="-"||op=="*"||op=="/")
	{
		return true;
	}
	return false;
}

int getprecedence(string op)
{
	if(op=="+"||op=="-")
	{
		return 1;
	}
	else if(op=="*"||op=="/")
	{
		return 2;
	}
}



int calculate()
{
	string token;
	cin >> token;
	operatorStruct NewOp;
	while(token != ";")
	{
		if(isdigit(token[0]))
		{
			int a=atoi(token.c_str());
			operandStack.push(a);
		}
		else if(isoperator(token))
		{
			NewOp.op=token;
			NewOp.precedence=getprecedence(token);
			while(operatorStack.empty() != 0 && operatorStack.top().precedence >= NewOp.precedence)
			{
				int num1=operandStack.top();
				operandStack.pop();
				int num2=operandStack.top();
				operandStack.pop();
				if(operatorStack.top().op =="*")
				{
					operatorStack.pop();
					operandStack.push(num2*num1);
				}
				else if(operatorStack.top().op=="+")
				{
					operatorStack.pop();
					operandStack.push(num2+num1);
				}
				else if(operatorStack.top().op=="-")
				{
					operatorStack.pop();
					operandStack.push(num2-num1);
				}
				else if(operatorStack.top().op=="/")
				{
					operatorStack.pop();
					operandStack.push(num2/num1);
				}
			}
			operatorStack.push(NewOp);
		}
	}
	int result= operandStack.top();
	cout << result << endl;
}


hmm. I've never heard of LinkedStack.h before. Did you write it yourself?
Topic archived. No new replies allowed.