EXC BAD ACCESS error that takes me away from my code
Jul 29, 2011 at 12:35am UTC
I'm working on constructing a classic infix to postfix converter and im getting a EXC_BAD_ACCESS error that takes me away from my main.cpp(the only place with code).
Heres the 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
string infixToPostfix(string infix)
{
stack<double > numbers;
stack<double > tempSwitch; // used to output in the right order
char postfixTemp[MAX];
string postfix;
char *infixArray = (char *)infix.c_str(); //converts string infix to char array
int c = 0; //counter for postfix
for (int i = 0; i < sizeof (infixArray)/sizeof (char ); i++) //goes through entire infix Array
{
while (*infixArray == '_' )
{
infixArray++;
}
if (isdigit(*infixArray))
{
numbers.push(*infixArray - 48);
infixArray++;
}
else
{
switch (*infixArray)
{
case '+' :
for (int i =0; i < sizeof (numbers)/sizeof (double ); i++)
{ tempSwitch.push(numbers.top());
numbers.pop();
}
for (int i =0; i < tempSwitch.size(); i++)
{
postfixTemp[c] = tempSwitch.top();
tempSwitch.pop();
c++;
}
postfixTemp[c] = *infixArray;
c++;
infixArray++;
break ;
case '-' :
for (int i =0; i < sizeof (numbers)/sizeof (double ); i++)
{ tempSwitch.push(numbers.top());
numbers.pop();
}
for (int i =0; i < tempSwitch.size(); i++)
{
postfixTemp[c] = tempSwitch.top();
tempSwitch.pop();
c++;
}
postfixTemp[c] = *infixArray;
c++;
infixArray++;
break ;
case '*' :
for (int i =0; i < sizeof (numbers)/sizeof (double ); i++)
{ tempSwitch.push(numbers.top());
numbers.pop();
}
for (int i =0; i < tempSwitch.size(); i++)
{
postfixTemp[c] = tempSwitch.top();
tempSwitch.pop();
c++;
}
postfixTemp[c] = *infixArray;
c++;
infixArray++;
break ;
case '/' :
for (int i =0; i < sizeof (numbers)/sizeof (double ); i++)
{ tempSwitch.push(numbers.top());
numbers.pop();
}
for (int i =0; i < tempSwitch.size(); i++)
{
postfixTemp[c] = tempSwitch.top();
tempSwitch.pop();
c++;
}
postfixTemp[c] = *infixArray;
c++;
infixArray++;
break ;
break ;
default :
printf("\nInvalid Operator" );
return 0;
}
}
}
postfix = postfixTemp;
cout << postfix;
return postfix;
}
int main()
{
char infix[MAX];
printf("Enter Infix Expression (type quit or q to exit): " );
gets(infix);
infixToPostfix(infix);
}
Can anyone find the issue(s)? Thank you in advance!
Topic archived. No new replies allowed.