Bubblesort causing runtime error while sorting a dynamically allocated multidimensional character array

This is a homework problem, but I am completely stumped as to what is causing this error. I have tried searching for a solution online but it is difficult to refine a key word search for this specific issue and has yielded no results.

On to the issue. My code is supposed to read in each word from a .txt file containing the Bill Cosby quote: "I don't know the key to success, but the key to failure is trying to please everybody." It then must store the data in a dynamic two-dimensional array. Once this is accomplished it counts how many times an individual word appears in the quote and then outputs a sorted list to the screen.

My bubblesort is causing a run-time error, and after hours of thinking and going through the code I simply cannot come up with a reason why. It starts sorting just fine until it tries to swap "to" with my placeholder, ",".

Any help is much appreciated, as well as feedback on my posting format.


The 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
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
122
#include <iostream>
#include <fstream>

using std::cin;
using std::cout;
using std::endl;
using std::ifstream;

void ReadData ( char **& names, char ** temp, char * buffer, int & num_words, int **& counter, int ** temp2 );
void PrintData (char ** names, int ** counter, int num_words );
void BubbleSort ( char ** names, int ** counter, int num_words );
	int RaCmp ( char ** temp, char * buffer, int num_words );

int main()
{
	int ** counter = 0;
	int ** temp2 = 0;
	char ** names = 0;
	char ** temp = 0;
	char buffer[256];
	int num_words = 0;

	ReadData ( names, temp, buffer, num_words, counter, temp2 );
	PrintData ( names, counter, num_words );

	return 0;
}
void ReadData ( char **& names, char ** temp, char * buffer, int & num_words, int **& counter, int ** temp2 )
{
	int check = 0;

	ifstream input ( "read.txt" );

	if ( input.is_open() )
	{
		while ( !input.eof() )
		{
			temp = new char * [num_words + 1];
			for ( int i = 0; i < num_words; ++i )
				temp[i] = names[i];
			temp2 = new int * [num_words + 1];
			for ( int i = 0; i < num_words; ++i )
				temp2[i] = counter[i];

			input >> buffer;
			check = RaCmp ( temp, buffer, num_words );
			if ( check == -1 )
			{
				temp[num_words] = new char[strlen( buffer ) + 1];
				strcpy ( temp[num_words], buffer );
				temp2[num_words] = new int[1];
				temp2[num_words][0] = 1;
			}
			else
			{
				temp[num_words] = new char[1];
				temp[num_words] = ",";
				temp2[num_words] = new int[1];
				temp2[num_words][0] = 1;
				temp2[check][0] += 1;
			}

			delete [] names;
			delete [] counter;

			names = temp;
			counter = temp2;
			++num_words;
		}
		input.close();
	}
}
int RaCmp ( char ** temp, char * buffer, int num_words )
{
	int check = -1;
	for ( int i = 0; i < num_words; i++ )
	{
		if ( strcmp ( temp[i], buffer ) == 0 )
		{
			check = i;
			i = num_words + 1;
		}
	}
	return check;
}
void PrintData (char ** names, int ** counter, int num_words )
{

	for ( int i = 0; i < num_words; i++ )
		cout << names[i] << " " << counter[i][0] << endl;

	BubbleSort ( names, counter, num_words );

	for ( int i = 0; i < num_words; i++ )
	{
		if ( isalpha ( names[i][0] ) )
		{
			cout << names[i] << " " << counter[i][0] << endl;
		}
	}
}
void BubbleSort ( char ** names, int ** counter, int num_words )
{
	char temp[256] = "lol";
	int temp2[1] = {0};

	for ( int i = 0; i < num_words; i++ )
		for ( int j = 0; j < num_words - 1; j++ )
		{
			if ( counter[j][0] > counter[j + 1][0] )
			{

				strcpy ( temp, names[j] );
				strcpy ( names[j], names[j + 1] );
				strcpy ( names[j + 1], temp );

				temp2[0] = counter[j][0];
				counter[j][0] = counter[j + 1][0];
				counter[j + 1][0] = temp2[0];
			}
		}
}
Topic archived. No new replies allowed.