...

View Full Version : Expression Evaluator Error



Javake
03-30-2013, 05:49 AM
I've been working on program in c++ that takes in a fully parenthesized infix expression & can convert and evaluate that same expression in prefix and postfix notation.

When I compile it runs but, when I used the interface to select an option, it gives me two run time errors. I don't know what is causing them because it says the error is in the .exe and I don't understand the code that's in it.

Any help would be great.

Here are the two error messages

Error #1: "Expression: deque iterator not dereferencable"
Error #2: "Expression: Standard c++ Librearies out of range && 0"

This is the file it says the error is in. Its called "dbghook.c"



#include <dbgint.h>

#ifdef _DEBUG

#include <internal.h>
#include <limits.h>
#include <mtdll.h>
#include <malloc.h>
#include <stdlib.h>

_CRT_ALLOC_HOOK _pfnAllocHook = _CrtDefaultAllocHook;

/***
*int _CrtDefaultAllocHook() - allow allocation
*
*Purpose:
* allow allocation
*
*Entry:
* all parameters ignored
*
*Exit:
* returns TRUE
*
*Exceptions:
*
*******************************************************************************/
int __cdecl _CrtDefaultAllocHook(
int nAllocType,
void * pvData,
size_t nSize,
int nBlockUse,
long lRequest,
const unsigned char * szFileName,
int nLine
)
{
return 1; /* allow all allocs/reallocs/frees */
}

#endif /* _DEBUG */

int _debugger_hook_dummy;

#ifdef _M_IA64
#undef _CRT_DEBUGGER_HOOK
#define _CRT_DEBUGGER_HOOK __crt_debugger_hook
#endif /* _M_IA64 */

__declspec(noinline)
void __cdecl _CRT_DEBUGGER_HOOK(int _Reserved)
{
/* assign 0 to _debugger_hook_dummy so that the function is not folded in retail */
(_Reserved);
_debugger_hook_dummy = 0;
}


Here is my function implementation since this is most likely where the problem is.



//expConverter_imp.cpp
//A .cpp file that stores all the function definitions
#include <string>
#include <stack>
#include "expConverter.h"
using namespace std;
string expConverter::infix_to_postfix(string& ins)
{
stack<char> store;
size_t i = 0;
for(i < ins.length(); i++;)
{
switch (ins[i])
{
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9': postfix += ins[i];
break;
case '(': store.push(ins[i]);
break;
case '*':
case 'x':
case 'X':
case '/':
case '-':
case '+': store.push(ins[i]);
break;
case ')':
while(store.top() != '(')
{
postfix += store.top();
store.pop();
};
break;
case ' ':
break;
default: store.pop();
break;
}
}
return postfix;
};

bool expConverter::is_balanced(string& ins)
{
stack<char> store;
size_t i = 0;
for (!ins.empty(); i++;)
{
switch (ins[i])
{
case '(': store.push(ins[i]);
break;
case ')': store.pop();
break;
default: i = i;
}
}
if (store.empty())
return true;
else
return false;
};

double expConverter::choose(size_t ins)
{
switch(ins)
{
case 1: return evaluate_postfix(infix_to_postfix(infix));
break;
case 2: return infix_to_prefix();
break;
case 3: return evaluate_infix(infix);
break;
default:
cout << "That was not one of the choices.\n";
};
};

void expConverter::set_infix(string s)
{
infix = s;
};

double expConverter::evaluate_postfix(string& ins)
{ int number1,
number2;
size_t i = 0;
stack<double> cstack;

for(i < ins.length(); i++;)
{
if (isdigit(ins[i]))
cstack.push(ins[i]);
else if(!isdigit(ins[i]))
switch(ins[i])
{
case '+': number1 = cstack.top();
cstack.pop();
number2 = cstack.top();
cstack.pop();
cstack.push(number1 + number2);
break;
case '*': number1 = cstack.top();
cstack.pop();
number2 = cstack.top();
cstack.pop();
cstack.push(number1 * number2);
break;
case '-': number1 = cstack.top();
cstack.pop();
number2 = cstack.top();
cstack.pop();
cstack.push(number1 - number2);
break;
case '/': number1 = cstack.top();
cstack.pop();
number2 = cstack.top();
cstack.pop();
cstack.push(number1 / number2);
};
}
return cstack.top();
};
double expConverter::evaluate_infix(string& ins)
{
infix_to_postfix(ins);
return evaluate_postfix(postfix);
};

double expConverter::infix_to_prefix()
{
stack<char> nstack;
stack<char> ostack;
stack<char> cstack;
int number1,
number2;
size_t i = 0;
double answer;
string s;
while(i < infix.length())
{
cstack.push(infix[i]);
s += cstack.top();
i++;
}
i = 0;
while ( i < s.length())
{
if (isdigit(s[i]))
nstack.push(s[i]);
else
{
switch(s[i])
{
case ')':
case '*':
case '+':
case '-':
case '/':
ostack.push(s[i]);
break;
case '(':
{
while(ostack.top() != ')')
{
number1 = nstack.top();
nstack.pop();
number2 = nstack.top();
nstack.pop();
switch(ostack.top())
{
case '+':
cstack.push(number1 + number2);
break;
case '*':
cstack.push(number1 * number2);
break;
case '-':
cstack.push(number1 - number2);
break;
case '/':
cstack.push(number1 / number2);
break;
}
ostack.pop();
}
}
break;
}
};
i++;
};
answer += nstack.top();
return answer;
}


If needed i'll post my header and main.

Spookster
03-31-2013, 07:52 AM
Have you first tried to run it in a debugger?

Javake
04-08-2013, 01:16 AM
Yes I'm using visual studio 2010 and when you compile, it debugs the program first.

Spookster
04-08-2013, 02:57 AM
Yes I'm using visual studio 2010 and when you compile, it debugs the program first.

I also use VS. It doesn't debug your program when you compile. That doesn't even make sense. It compiles and links your program. You can link a release or debug version of your program. The debug version builds in additional symbols within the exe that allows you to run your program in the debugger.

So since you are using VS then you should be able to debug this yourself. Run your program in the debugger and put breakpoints in the code that will get executed when you select those options in your GUI then step through the code line by line and see why you are getting a run time error.

Javake
04-08-2013, 07:07 AM
Alright sorry about the mistake, I thought that the fact that it throws error messages means that it was debugging.

I'll try what you suggested and report back.

Thanks spookster.

mobileapplover
04-24-2013, 01:56 PM
Great point discuss here; Thanks for sharing regarding Expression Evaluator Error, Please keep sharing insight.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum