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:
#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] == '+' || '-' || '*' || '/')
{
if ( optype=1 )
{ // when the last entry was numeric then put number on stack
tempstr= express.substr(startposn , j-startposn-1) ; tempnum= atoi(tempstr) ;
//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() == '+' || '-' || '*' || '/')
{
// 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) ;
// 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) ;
// 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() == '+' || '-' || '*' || '/')
{
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); aStack.push(tempstr-'0'); // place final number on top of stack
}
}
while(!(bStack.empty()))
{
if(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);
}
Looks like you are trying to pass a string object to the function atoi() which only works with c strings. Because of that tempnum is never getting initialized.