Problems with converting string to an Integer in a calculator program

Hey im having problems with my program and i am not sure whats wrong.i will underline what i think is not working I do know that error messages pop up telling me that i can not convert string to const char can you help me out, here is my code:
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
tempnum = atoi(tempstr.c_str());
Thanks that helped solve one of the problems. It now builds correctly but now i get a error when it executes i enter in the expression and press enter and i get this error:

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.
These don't do what you might think they do:
else if (express[j] == '+' || '-' || '*' || '/')

if ( optype=1 )

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

All expressions return true.
Last edited on
Thanks that solved that problem now its coming out with the wrong answer:)
such as 1+1 is -47
and 1+2 is -46 so their is probably some kind of reason that when i add to the next number(1+3=-45) so the math is wrong right?
Have you tried debugging your program?
Topic archived. No new replies allowed.