I am really struggling with calculator assignment

Hey im having problems with my program. I am not exactly sure what the problem is i can successfully build the code but when i actually run the program it pops up with the following error messages:
Unhandled exception at 0x771afbae in project.exe: Microsoft C++ exception: std::out_of_range at memory location 0x0023f7ac..
and this:
Run-Time Check Failure #0 - The value of ESP was not properly saved across a function call. This is usually a result of calling a function declared with one calling convention with a function pointer declared with a different calling convention.

here is my code i would be grateful if you could help me out:
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#include <iostream>
#include <stack>
#include<string>
using namespace std;
 

int OrderOfOpsCheck(char);
 

int main()
{
stack<int> aStack;
stack<char> bStack;
int temp1, temp2, temp ,optype, startposn ,tempnum ;
 
string express, eString,   mchac ,   tempstr ; 
cout<<"please enter an expression"<<endl;
cin>>express;  
 
optype =0 ;
startposn= -1 ;
for(int j = 0; j < express.length(); j++)
{
      if(isdigit(express[j]))
      {   
           if  ( startposn== -1 )   
           {    startposn = j ; 
           }
           optype=1 ; 
      }
      else if ((express[j] == '+') || (express[j] == '-') || (express[j] == '*') || (express[j] == '/'))
      {     
            if  ( optype==1 )      
            {        // when the last  entry was numeric then put number on stack 
                       tempstr=  express.substr(startposn , j-startposn-1)  ;      
                       tempnum = atoi(tempstr.c_str());;    
                       //aStack.push(  tempstr-'0');                
                      aStack.push(tempnum);                
                       startposn = -1 ; 
            }
              optype =0 ; 
              if(bStack.empty() == false)    
               { 
                   // if something exists already on operations stack, check if need to resolve order of operations 
                   while(OrderOfOpsCheck(bStack.top()) >= OrderOfOpsCheck(express[j]))   
                   {
                          if(bStack.top() == '+' ||bStack.top() == '-' || bStack.top() =='*' ||bStack.top() == '/')      
                          {  
                              // pop two entries off stack and process them then put result back on stack                               
                              temp2 = aStack.top();
                              aStack.pop();
                              temp1 = aStack.top();
                              aStack.pop();
                              switch(bStack.top())
                              {
                                  case '+': temp = temp1 + temp2;
                                    break;
                                  case '-': temp = temp1 - temp2;
                                    break;
                                  case '*': temp = temp1 * temp2;
                                  break;
                                    case '/': temp = temp1 / temp2;
                                  break;
                                  default: cout << "There was an invalid operator, press any key to terminate.";
                              } 
                          aStack.push(temp);
                       } 
                      bStack.pop(); 
                      if(bStack.empty() == true)
                      {
                          break;
                      }
                  }
              }     
              bStack.push(express[j]); // places items on top of stack 
      } 
      else if(express[j] == '(')
      {  
            if  ( optype==1 )      
            {        // when the last  entry was numeric then put number on stack 
                       tempstr=  express.substr(startposn , j-startposn-1)  ;      
                     tempnum = atoi(tempstr.c_str());
  
 //                       aStack.push(  tempstr-'0');                
                      aStack.push(tempnum);                
                       startposn = -1 ; 
            }
              optype =0 ; 
              bStack.push (express[j]);
      }
     else if(express[j] == ')')
     { 
         
            if  ( optype==1 )      
            {        // when the last  entry was numeric then put number on stack 
                       tempstr=  express.substr(startposn , j-startposn-1)  ;      
                     tempnum = atoi(tempstr.c_str());  
 //                       aStack.push(  tempstr-'0');                
                      aStack.push(tempnum);                
                       startposn = -1 ; 
            }
              optype =0 ; 
              if(bStack.empty() == false)
              { 
                  // process entries on stack until we reach open bracket
                  while(bStack.top() != '(')
                  {
                       if(bStack.top() == '+' ||bStack.top() == '-' || bStack.top() =='*' ||bStack.top() == '/')      
                       { 
                          temp2 = aStack.top();
                          aStack.pop();
                          temp1 = aStack.top();
                          aStack.pop();
                          switch(bStack.top())
                          {
                              case '+': temp = temp1 + temp2;
                              break;
                              case '-': temp = temp1 - temp2;
                              break;
                              case '*': temp = temp1 * temp2;
                              break;
                              case '/': temp = temp1 / temp2;
                              break;
                              default: cout << "There was an invalid operator, press any key to terminate.";
                          }
                          aStack.push(temp);
                       }
                       bStack.pop();
                       if(bStack.empty() == true)
                       {
                          break;
                       }                              //POP the content in Stack until “(“ is reached,
                  }
              }
              bStack.pop();                    //POP it ; break;
     } 
if  ( optype==1 )   // if at the end of string check if last item processed was number if so pop on stack 
	{
		tempstr=express.substr(startposn , j-startposn-1);   
        tempnum = atoi(tempstr.c_str());
		aStack.push(tempnum-'0');                       // place final number on top of stack
	}
}

      
while(!(bStack.empty()))
    {
 

        if(bStack.top() == '+' ||bStack.top() == '-' || bStack.top() =='*' ||bStack.top() == '/' )
        {
            temp2 = aStack.top();
            aStack.pop();
            temp1 = aStack.top();
            aStack.pop();
            switch(bStack.top())
            {
                case '*': temp = temp1 * temp2;
                    break;
                case '/': temp = temp1 / temp2;
                    break;
                case '+': temp = temp1 + temp2;
                    break;
                case '-': temp = temp1 - temp2;
                    break;
                default: cout << "There was an invalid operator, press any key to terminate.";
            }
 
            aStack.push(temp);
        }
        bStack.pop();
    }  
    
cout << endl << "The answer is: " << temp << endl;
cin.ignore();
cin.clear();
cin.get();
return 0;
}
 
int OrderOfOpsCheck(char operation)
{
    int check;
    switch(operation)
    {
        case '/': 
        case '*': check= 2;
        break;
        case '+':
        case '-': check= 1;
        break;
        case '(':
        case ')': check= 0;
        break;
    }
    return (check);
}


Last edited on
closed account (1yvXoG1T)
Please put your code between [code] tags (end one is in this format [/*code](remove the *) and try to be more specific with your question.
Last edited on
Okay i modified my post so that is easier to read and i also tried to explain my problem better i hope it helped a little
else if (express[j] == '+' || '-' || '*' || '/') this is wrong -> else if ((express[j] == '+') || (express[j] == '-') || (express[j] == '*') || (express[j] == '/')) or use switch

if(bStack.top() == '+' || '-' || '*' || '/') same as above

if ( optype=1 ) this is an assignment not a comparison (always true)
Last edited on
You're trying to access a variable that's outside of the range of some sequential data structure. Well... seeing as you have a debugger, why not use it?

Set a breakpoint somewhere in your largest loop, and run the program several times, and try to determine on what iteration you get the exception. See what you get from that.

-Albatross
Thank you i am not getting the run time error any more because of that stupid mistake that i made. The only other thing that i have to fix is that it is coming out with the wrong answer such as 1+1=-47 and 1+2=-46 and 1+3=-45. Now i am not sure what that means but my guess is that i'm screwing up one of the variables.
Topic archived. No new replies allowed.