### The fabric of reality is breaking down

Look at this.

I have a function that determines the number of characters in a mathematical expression.
So, 4+(9-2) would return 7

Now, when I put one expression into it, it returns one too many characters.

Then, immediately after, I put another expression into it, and all of a sudden it returns the correct number of characters.

No matter what, the function works correctly half the time and incorrectly the other half.

There is no explanation for this. It makes no sense. The fabric of reality must be breaking down.

I've bolded the relevant code.

 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980`` ``````#include using namespace std; #include "Stack.h" //CHECK DELETE AND NEW DYNAMICALLY ALLOCATONS static const int MAX = 5; static const char filler = '~'; static const string illegalExp = "Illegal Expression"; main() { //convert a infix expression to a postfix expression void postfixConversion(Stack, int, char[], char[]); //evaluate a postfix expression and return the result double evaluatePostFix(Stack, char[], int); //prompt user for expression, and perform basic error check void promptUser(char[]); //returns the number of characters in an expression int getExpressionLength(char[]); int pfLength = 0; int expressionLength = 0; char pfExpression[MAX]; char expression[MAX]; Stack aStack;//character stack for conversion from infix to postfix and evaluation of postfix fill_n(pfExpression, MAX, filler);//fill the character arrays with a const char fill_n(expression, MAX, filler); promptUser(expression); expressionLength = getExpressionLength(expression); //expressionLength -= 1; cout << "Expression length = " << expressionLength; cout << endl; postfixConversion(aStack, expressionLength, expression, pfExpression); pfLength = getExpressionLength(pfExpression); cout << "pfLength is " << pfLength << endl; double result = evaluatePostFix(aStack, pfExpression, pfLength); cout << "Answer is " << result << endl; } int getExpressionLength(char exp[]) { int Length = 0; for(int i = 0; i < MAX-1 && exp[i] != filler; i++) { Length++; } return (Length); } void promptUser(char expression[]) { cout << "Enter your expression. Include no spaces. "; cin >> expression; if(!isdigit(expression[0]) && expression[0] != '(') { cout << illegalExp << endl; exit(EXIT_FAILURE); } } ``````

So, if one were to input 7+9......expressionLength will (incorrectly) say FOUR, and pfExpression would be 79+ ------> and length for that will (correctly) say THREE

It's a glitch in the universe.
INeedAHero wrote:
I have a function that determines the number of characters in a mathematical expression.
So, 4+(9-2) would return 7
Are my maths skills currently below 0 or what? 9-2 is 7, 4 + 7 = 11 not 7. Or is your function only supposed to return the values in brackets (americans say "parentheses").

INeedAHero wrote:
So, if one were to input 7+9......expressionLength will (incorrectly) say FOUR
Maybe your code counts the null character '\0' at the end of the array?

About input, you might be interested in this http://www.cplusplus.com/forum/articles/6046/