I have a problem with the new program, it compiles and and it debugs, but when I get to the command lines and enter my function it gives me a debug assertion failure. We are supposed to be finding the sum of the area of rectangles used for close approximation. Any ideas on how I am getting this assertion failure is greatly appreciated!

#include <iostream>
#include <stack>
using namespace std;


enum token_code {REAL_VALUE, VAR_X, OPERATOR};

class token

{
	public:

	token(token_code code);

	token();

	token(const token &t);


	token_code code();

	float real_value();

	char op();

	char var_x();

	void set_code(token_code code);

	void set_real_value(float value);

	void set_op(char op);

	void set_var_x(char var_x);

	token& operator = (const token &t);

	void print();

	protected:

	struct token_record

	{

		token_code code;

		union

		{

			float real_value;

			char op;

			char var_x;

		};

	};

	token_record the_token;

};



#include <iostream>
#include "token.h"
using namespace std;

token::token()
{
}

void token::print()
{
	switch (the_token.code)
	{
		case REAL_VALUE: cout << the_token.real_value;
						break;
		case VAR_X: 	cout << the_token.var_x;
						break;
		case OPERATOR:	cout << the_token.op;
						break;
		default:	cout << "Don't know";
	}
	cout << " ";
}

token::token(token_code code)
{
	the_token.code = code;
	if (code == REAL_VALUE)
		the_token.real_value = 0;
}

token::token(const token &t)
{
	the_token.code = t.the_token.code;
	switch (the_token.code)
	{
		case REAL_VALUE: the_token.real_value = t.the_token.real_value;
						break;
		case VAR_X: 	the_token.var_x = t.the_token.var_x;
						break;
		case OPERATOR:	the_token.op = t.the_token.op;
						break;
	}
}

token& token::operator = (const token &t)
{
	the_token.code = t.the_token.code;
	switch (the_token.code)
	{
		case REAL_VALUE: the_token.real_value = t.the_token.real_value;
						break;
		case VAR_X: 	the_token.var_x = t.the_token.var_x;
						break;
		case OPERATOR:	the_token.op = t.the_token.op;
						break;
	}
	return *this;
}

token_code token::code()
{
	return the_token.code;
}

float token::real_value()
{
	return the_token.real_value;
}

char token::op()
{
	return the_token.op;
}

char token::var_x()
{
	return the_token.var_x;
}

void token::set_code(token_code code)
{
	the_token.code = code;
}

void token::set_real_value(float value)
{
	the_token.real_value = value;
}

void token::set_op(char op)
{
	the_token.op = op;
}

void token::set_var_x(char var_x)
{
	the_token.var_x = var_x;
}



#include "token.h"
#include <iostream>
#include <queue>
#include <iomanip>
#include <cmath>

using namespace std;

#pragma option -Jgx
const char END_TOKEN = '#';

void obtain_interval(float &a, float &b, int &number_of_rectangles);

int instack_priority(token t);

int infix_priority(token t);


void infix_to_postfix(queue<token> &postfix);

float eval_f(queue<token> postfix, float x);

void get_infix_token(token &from_infix);


void integrate(float a, float b, int number_of_rectangles, float &area);

float eval(float v1, float v2, char op);

bool is_op(char ch);




int main()
{

	float a, b;
		float area = 0;

	int number_of_rectangles;

	obtain_interval(a, b, number_of_rectangles);



	
		

	cout << setiosflags(ios::fixed | ios::showpoint) << setprecision(3);
	while (a < b)

	{

		integrate(a, b, number_of_rectangles, area);

		cout << "Approximation to area is " << setw(10) << area << endl;

		cout << endl;


	}

	return 0;
}

void obtain_interval(float &a, float &b, int &number_of_rectangles)

{	

	cout << "Enter left and right endpoints: (Type left >= right to quit): ";

	cin >> a >> b;
	

	if (a < b)

	{

		cout << "Enter number of rectangles for computing area: ";

		cin >> number_of_rectangles;
		

	}

}



void integrate(float a, float b, int number_of_rectangles, float &area)

{
	cout << " in integrate() " << endl;

	stack<token> token_stack;

	queue<token> postfix;

	int count;

	float width, x;



	width = (b - a) / number_of_rectangles;

	count = 0;

	x = a;

	infix_to_postfix(postfix);

	cout << "past infix_to_postfix" << endl;


	while (count < number_of_rectangles )

	{

		area = area + eval_f(postfix, x + width / 2.0) * width;

		x = x + width;

		++count;

	}

	

}

void infix_to_postfix(queue<token> &postfix)

{

	cout << " in infix_to_postfix() " << endl;

	token bottom_stack(OPERATOR);

	token from_stack, from_infix;

	stack<token> token_stack;

	char ch;

	bottom_stack.set_op(END_TOKEN);

	token_stack.push(bottom_stack);

	cout << "Enter function with spaces between tokens, then <ENTER>";

	cin.get(ch);

	do

	{

		get_infix_token(from_infix);
//
		from_infix.print();
//
		if ((from_infix.code() == REAL_VALUE) ||  

			(from_infix.code() == VAR_X)) 

		{cout << "1" << endl;
			postfix.push (from_infix);
		}

		else if (from_infix.op() == ')') 

		{
			cout << "2" << endl;
			from_stack = token_stack.top();
			token_stack.pop();

			while (from_stack.op() != '(') 

			{
				cout << "2.5" << endl;

				postfix.push (from_stack);

				from_stack = token_stack.top();
				token_stack.pop();

			}

		}

		else if (from_infix.op() == END_TOKEN) 

			while (! token_stack.empty())

			{
				cout << "3" << endl;

				from_stack = token_stack.top();
				token_stack.pop();

				postfix.push(from_stack);

			}

		else


		{
			cout << "4" << endl;

			from_stack = token_stack.top();
			token_stack.pop();

			while (instack_priority(from_stack) >=

				infix_priority(from_infix))

 			{

				postfix.push(from_stack);

				from_stack = token_stack.top();
				token_stack.pop();

			}

			token_stack.push(from_stack);

			token_stack.push(from_infix);

		}

	} while (! ((from_infix.code() == OPERATOR) &&

		(from_infix.op() == END_TOKEN)));

}


void get_infix_token(token &from_infix)

{

	const char SPACE = ' ';

	char ch = ' ';

	float multiplier = 0.1;

	bool left_of_decimal = true;

	int column = 0;



	do

	{

		++column;

		cin.get(ch);

		if (is_op(ch))

		{

			from_infix.set_code(OPERATOR);

			from_infix.set_op(ch);

		}		

		else if (isdigit(ch))

		{

			if (column == 1)

			{

				from_infix.set_code(REAL_VALUE);

				from_infix.set_real_value(0);

			}

			if (left_of_decimal)

				from_infix.set_real_value(from_infix.real_value() *

					10.0 + (ch - '0'));

			else

			{

				from_infix.set_real_value(from_infix.real_value() *

					multiplier  + (ch - '0'));

				multiplier = multiplier / 10.0;

			}

		}

		else if (ch == '.')

		{

			left_of_decimal = false;

			multiplier = 0.1;

		}

		else if (ch == 'X')

		{

			from_infix.set_code(VAR_X);

			from_infix.set_var_x(ch);

		}

	} while ((ch != '\n') && (ch != SPACE) && (ch != END_TOKEN));

}


int infix_priority(token t)

{

	int priority = 0;

	

	switch (t.op())

	{

		case '^':			priority = 3;

						break;

		case '*':

		case '/':			priority = 2;

						break;

		case '+':

		case '-':			priority = 1;

						break;

		case '(':			priority = 4;

						break;

		case ')':

		case END_TOKEN:	priority = 0;

	}

	return priority;

}


int instack_priority(token t)

{

	int priority = 0;



	switch (t.op())

	{

		case '^' :			priority = 3; 

						break;

		case '*': 

		case '/' :			priority = 2;

						break;

		case '+':

		case '-':			priority = 1;

						break;

		case '(':

		case END_TOKEN:	priority = 0;

	}

	return priority;

}


float eval_f(queue<token> postfix, float x)

{

	token result(REAL_VALUE); 

	token t1, t2, t3;

	stack<token> token_stack;


	t1 = postfix.front();
	postfix.pop();


	postfix.push(t1);

	while (! ((t1.code() == OPERATOR) && (t1.op() == END_TOKEN)))

	{

		if (t1.code() != OPERATOR)

			if (t1.code() == REAL_VALUE)

				token_stack.push(t1);
		

			else

			{

				t1.set_code(REAL_VALUE);

				t1.set_real_value(x);

				token_stack.push(t1);

			}

		else

		{

			t2 = token_stack.top();
			token_stack.pop();

			t3 = token_stack.top();
			token_stack.pop();

			result.set_real_value(eval(t3.real_value(), t2.real_value(), 

				t1.op()));

			token_stack.push(result);

		}

		t1 = postfix.front();
		postfix.pop();

		postfix.push(t1);

	}

	result = token_stack.top();
	token_stack.pop();

	return result.real_value();

}




float eval(float v1, float v2, char op)

{

	float result = 1;

	

	switch (op)

	{

		case '+' :	result = v1 + v2;

				break;

		case '-':	result = v1 - v2;

				break;

		case '*':	result = v1 * v2;

				break;

		case '/':	result = v1 / v2;

				break;

		case '^':	result = pow(v1, v2);

				break;

	}

	return result;

}


bool is_op(char ch)

{

	return (ch == '+') || (ch == '-') || (ch == '*') || (ch == '/') ||

		(ch == '^') || (ch == '(') || (ch == ')') || (ch == END_TOKEN);

}

Well I don't have time to go through all that code, but assertion errors are usually caused by attempting to delete a value that doesn't exist. It doesn't seem like your code uses any DMA though, so it could be internal (included libraries). Try stepping through/into your program (f10/f11) to try and isolate where exactly the error occurs. Then I may be of more help to you.

Yes, the majority of the problem seems to occur in this part of the code:
void infix_to_postfix(queue<token> &postfix)
which is located in the main. Hope this helps in some way.

If you run the code in the debugger, wait for the assert, then use the stack trace to track back to your code, it should help you figure out the problem.

Hmmm...Well I have no idea what the queue library is. But I sugguest stepping through to that point, then stepping 'in' (f11 in MSDS) to the queue library on the line where the error occurs. You can then step 'through' this code (f10) until you find the exact line within this library that the error occurs. This, coupled with a watch on some key variables, is usually the best way to pinpoint an error.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.