Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New Coder
    Join Date
    Aug 2011
    Location
    California
    Posts
    19
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Exclamation Expression Evaluator Error

    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"

    Code:
    #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.

    Code:
    //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.

  • #2
    Supreme Overlord Spookster's Avatar
    Join Date
    May 2002
    Location
    Marion, IA USA
    Posts
    6,273
    Thanks
    4
    Thanked 83 Times in 82 Posts
    Have you first tried to run it in a debugger?
    Spookster
    CodingForums Supreme Overlord
    All Hail Spookster

  • #3
    New Coder
    Join Date
    Aug 2011
    Location
    California
    Posts
    19
    Thanks
    5
    Thanked 0 Times in 0 Posts
    Yes I'm using visual studio 2010 and when you compile, it debugs the program first.

  • #4
    Supreme Overlord Spookster's Avatar
    Join Date
    May 2002
    Location
    Marion, IA USA
    Posts
    6,273
    Thanks
    4
    Thanked 83 Times in 82 Posts
    Quote Originally Posted by Javake View Post
    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.
    Spookster
    CodingForums Supreme Overlord
    All Hail Spookster

  • Users who have thanked Spookster for this post:

    Javake (04-08-2013)

  • #5
    New Coder
    Join Date
    Aug 2011
    Location
    California
    Posts
    19
    Thanks
    5
    Thanked 0 Times in 0 Posts
    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.

  • #6
    New Coder
    Join Date
    Jan 2013
    Posts
    23
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Great point discuss here; Thanks for sharing regarding Expression Evaluator Error, Please keep sharing insight.


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •