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.
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
|
#include <iostream>
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.