Assertion Failure

when I try to return from my get_total_weight function it is breaking with an
error that says:

Debug Assertion Failed!
Expression: _BLOCK_TYPE_IS_VALID(pHead->nBlockUse)

Any help at all would be very much appreciated!

line 54 in the driver is where i am having issues i think(from the debugging i knew how to do) (fixed when I used &)

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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#pragma once
#include <cctype>      // Provides toupper
#include <cstdlib>     // Provides EXIT_SUCCESS and size_t
#include <iostream>    // Provides cin, cout
#include "table2.h"    // Provides the table class
#include <fstream>
#include <cstring>    // Provides strchr
#include <string>
#include <sstream>
using namespace std;

//get total weight
//preconditions: pass in preconstructed table, a string to parse and find weight of, and the start of the string
//postcondition: calculates one line from file and does appropriate math for molecular weights
double get_total_weight(table& hash_table, string line_input, int i);
//reads in the table
//precondition: pass in a preconstructed hash table by reference and a string to read in
//post condition: reads in one line of code and parses it and puts in the table
void read_in_table(table& hash_table, string line_input);

int main()
{
	string line_input;
	table hash_table;
	string pathway = "PeriodicTableElements.txt";
	ifstream input;
	input.open(pathway);
	if (input.fail( ))
	{
		cerr << "Could not open input file." << endl;
		system("PAUSE");
		exit(0); 
	}
	while(input.peek()!= EOF)
	{
		getline(input, line_input, '\n');
		read_in_table(hash_table, line_input);
	}
	input.close( );//end of inserting the table of elements into hash table.
	//*************************************************************************************************************************************
	//start of the formula section!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
	string pathway1 = "formulas.txt";
	ifstream input1;
	input1.open(pathway1);
	if (input1.fail( ))
	{
		cerr << "Could not open formula file." << endl;
		system("PAUSE");
		exit(0); 
	}
	while(input1.peek()!=EOF)
	{
		getline(input1, line_input, '\n');
		double total_weight = get_total_weight(hash_table, line_input, 0);
		cout<<line_input<<"="<<total_weight<<endl;
	}
	input1.close();
	cout<<endl;
	system("PAUSE");
	return EXIT_SUCCESS;
}
void read_in_table(table& hash_table, string line_input)
{
		double weight;
		int i = 0;
		while(line_input[i] != ' ')
			++i;
		string element = line_input.substr (0,i);
		int element_number = element[0]-0;
		int weight_length = line_input.size() - i;
		string weight_string = line_input.substr(i,weight_length);
		istringstream convert(weight_string); 
		if ( !(convert >> weight) )
			weight = 0;
		hash_table.insert(element, weight, element_number);
}
double get_total_weight(table& hash_table, string line_input, int i)
{
	int j;
	int multiplier;
	double total_weight=0.0;
	double weight;
	while(line_input[i] != '\0')
	{
		j=i;
		if(line_input[i] == '(')
		{
			++i;
			int k = i;
			while(line_input[k+1]!=')')
				k++;
			string line_help = line_input.substr(i,k-i+1);
			weight = get_total_weight(hash_table, line_help, 0);
			i = k+2;
			if(line_input[i] == '\0')
				total_weight = total_weight+ weight*1;
		}
		else
		{
			while(islower(line_input[i+1]))
				i++;
			int k = i-j+1;
			string element = line_input.substr (j,k);
			double element_number = element[0]-0;
			weight = hash_table.retrieve(element_number, element);
			++i;
			if(!(isdigit(line_input[i])))
				total_weight = total_weight + weight*1;
		}
		j=i;
		while(isdigit(line_input[i]))
			i++;
		int k = i-j;
		string line_input_passer = line_input.substr(j,k);
		istringstream convert(line_input_passer); 
		if ( !(convert >> multiplier) ) //give the value to weight using the characters in the stream
			multiplier = 0;
		total_weight = total_weight + weight*multiplier;
	}
	return total_weight;
}


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
#pragma once
#include "Table2.h"
#include <iostream>

table::table()
{
	for(int i = 0; i<SIZE; i++)
		array[i] = new node(-1, "");
}
void table::decon_help(node* n)
{
	if(n->next != NULL)
		decon_help(n->next);
	delete n;
	return;	
}
table::~table()
{
	for(int i=0; i<SIZE; i++)
	{
		if(array[i]->next != NULL)
			decon_help(array[i]->next);	
		delete array[i];
	}
}
void table::insert(string passed_ele, double passed_weight, double key)
{
	//cout<<"in first insert"<<endl;
	int index = hash(key);
	//cout<<index<<"  ";
	if(array[index]->data == -1 || array[index]->data == -2)
	{
		array[index]->data = passed_weight;
		array[index]->element = passed_ele;
	}
	else
	{
		//cout<<"   going into next insert";
		insert(array[index]->next, passed_ele, passed_weight);
	}
}
void table::insert(node*& n, string passed_ele, double passed_weight)
{
	if(n==NULL)
		n = new node(passed_weight, passed_ele);
	else
		insert(n->next, passed_ele, passed_weight);
}
double table::retrieve(double d, string element)
{
	int index = hash(d);
	if(array[index]->element == element)
		return array[index]->data;
	else
		return retrieve(d, element, array[index]->next);
}
double table::retrieve(double d, string element, node* n)
{
	if(n->element==element)
		return n->data;
	return retrieve(d, element, n->next);
}
int table::hash(double d)
{
	int ret = (int)(d /.05555)%SIZE;
	return ret;
}



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
// FILE: table2.h

#pragma once
#include <cstdlib>    // Provides size_t
#include <string>
using namespace std;
struct node
{
	double data;
	string element;
	node* next;
	node(double d, string ele)
	{
		data = d;
		element = ele;
		next=NULL;
	}
};
const int SIZE = 50;
class table
{
	node* array[SIZE];
public:
	//constructor
	//pre:none
	//post: creates array of nodes of size 50 and initialized each value to -1
	table();
	//deconstructor
	//deletes all dynamic memory
	~table();
	//helps to delete dynamic alloc memory
	void decon_help(node* n);
	//inserts an element from periodic table into a hash table
	void insert(string, double, double);
	//inserts an element from periodic table into hash table when there is a collision
	void insert(node*& n, string passed_ele, double passed_weight);
	//returns the value for the element
	double retrieve(double d, string element);
	//returns value for element when there is a collision
	double table::retrieve(double d, string element, node* n);
	//hash function to spread out the entries
	int hash(double);
	//overload the [] brackets
	table operator[](int);
	//Clear (remove all from hash table)
	void clear(node* array[]){table::~table();}

};

};


Table file includes in a txt file:
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
H 1.008
He 4.0026
Li 6.94
Be 9.0122
B 10.81
C 12.011
N 14.007
O 15.999
F 18.998
Ne 20.180
Na 22.990
Mg 24.305
Al 26.982
Si 28.085
P 30.974
S 32.06
Cl 35.45
Ar 39.948
K 39.098
Ca 40.078
Sc 44.956
Ti 47.867
V 50.942
Cr 51.996
Mn 54.938
Fe 55.845
Co 58.933
Ni 58.693
Cu 63.546
Zn 65.38
Ga 69.723
Ge 72.63
As 74.922
Se 78.96
Br 79.904
Kr 83.798
Rb 85.468
Sr 87.62
Y 88.906
Zr 91.224
Nb 92.906
Mo 95.96
Tc 97.91
Ru 101.07
Rh 102.91
Pd 106.42
Ag 107.87
Cd 112.41
In 114.82
Sn 118.71
Sb 121.76
Te 127.60
I 126.90
Xe 131.29
Cs 132.91
Ba 137.33
La 138.91
Ce 140.12
Pr 140.91
Nd 144.24
Pm 144.91
Sm 150.36
Eu 151.96
Gd 157.25
Tb 158.92
Dy 162.50
Ho 164.93
Er 167.26
Tm 168.93
Yb 173.05
Lu 174.97
Hf 178.49
Ta 180.95
W 183.84
Re 186.21
Os 190.23
Ir 192.22
Pt 195.08
Au 196.97
Hg 200.59
Tl 204.38
Pb 207.20
Bi 208.98
Po 208.98
At 209.99
Rn 222.02
Fr 223.02
Ra 226.03
Ac 22.03
Th 232.04
Pa 231.04
U 238.03
Np 237.05
Pu 244.06
Am 243.06
Cm 247.07
Bk 247.07
Cf 251.08
Es 252.08
Fm 257.10
Md 258.10
No 259.10
Lr 262.11
Rf 265.12
Db 268.13
Sg 271.13
Bh 270.00
Hs 277.15
Mt 276.15
Ds 281.16
Rg 280.16
Cn 285.17
Uut 284.18
Fl 289.19
Uup 288.19
Lv 293
Uus 294


and the formulas txt:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
H2SO4
Al2(SO4)3
H2O
CH4
C6H12O6
(CH3)3  
CH
C3H7
AlCl4Cs
AuI3
Bi2O3
Ga(C2H3O2)3
Cu3(PO4)2
In(OH)3
Li(AlSi2O6)
Sb2OS2
Last edited on
The table is passed by value to get_total_weight and get_total_weight_help, meaning the table will be copied and the function will operate on the copy. At the end of the function the copy will be destroyed and the destructor called.

The table class has not been implemented to handle copying properly. See the rule of three.
http://en.wikipedia.org/wiki/Rule_of_three_%28C%2B%2B_programming%29

Now you probably don't want these functions to copy the table anyway so you better pass the table by reference instead.
Thanks a bunch! Changing to the reference fixed the issues.
Topic archived. No new replies allowed.