Final Exam Code

This is due tomorrow night and the code compiles but when I run the program fails to work.
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
#include <fstream>
#include <iostream>
#include <iomanip>
#include <cctype>

using namespace std;

static int counter = 0;
static int swap_counter = 0;

void add_word(char list[][30], char temp[], int count[], char holder[], char hold[]);
//adds word to list, or increments counter if found
void sort_words(char list[][30], int count[], char holder[], char hold[]);
//sorts the list, starting at the high end for 20 words
bool read_line(ifstream &fin, char line[]);
//reads a line, returns false at end of file
bool extract_word(char line[], char temp[], int &index);
//gets a word, returns false if no word found
void process_line(char list[][30], char line[], char temp[], int &index, int count[], char holder[], char hold[]);
//uses extract_word(), add_word()
bool write_list(ofstream &fout, char list[][30], int count[], char *argv[]);
//writes list to output file, returns false if file fails

int main(int argc, char * argv[])
{
	char list[10000][30] = { " " };
	char temp[30] = { " " };
	int count[10000] = { 0 };
	char line[100] = { " " };
	int index = 0;
	char swap[10000][30] = { " " };
	char hold[30] = { " " };
	bool readline = true;
	char holder[30] = { " " };

	ifstream fin;
	ofstream fout;

	if (argc != 3 && argc != 4)
	{
		cout << "Incorect usage. Must provide input and output file names. Quitting." << endl;
		return 1;
	}

	fin.open(argv[1]);

	if (!fin)

	{
		fin.close();
		cout << "There was an error opening one of the files" << endl;
		return 1;
	}

	while (readline == true)

	{
		readline = read_line(fin, line);

		process_line(list, line, temp, index, count, holder, hold);
	}

	if (readline == false)

	{
		sort_words(list, count, holder, hold);
	}

	write_list(fout, list, count, argv);
		
}

void add_word(char list[][30], char temp[], int count[], char holder[], char hold[])
{
	int i = 0;
	int j = 0;
	int k = 0;
	bool found = false;
	//check for word if existence in list

	//separate searches for first swap

	if (swap_counter == 0)
	{
		//if found increment counter 

		for (i = 0; i < 20; i++)
		{
			if (_stricmp(list[i], temp) == 0)
			{
				count[i]++;
				found = true;
				break;
			}

		}

		//if not found yet increment a counter, maybe static variable, for resorting list at 20 new words, and increment counter for sort function

		if (found == false)
		{
			for (j = 0; j < 10000 && found == false; j++)
			{
				if (list[j] == " " )
				{
					for (k = 0; k < 30; k++)
					{
						list[j][k] = temp[k];
					}
					found = true;
					counter++;
				}
			}
		}
	}
	if (counter == 20)
	{
		counter = 0;
		sort_words(list, count, holder, hold);
		swap_counter++;

	}

	//separate searches for swaps after the frist

	if (swap_counter != 0)
	{
		//if found increment counter 
		for (i = 0; i < swap_counter * 20 && found == false; i++)
		{
			if (_stricmp(list[i], temp) == 0)
			{
				found = true;
				count[i]++;
			}

			if (_stricmp(list[i], temp) > 0)
			{
				break;
			}
		}
		if (found == false)
		{
			for (j = 0; j < 10000 && found == false; j++)
			{
				if (list[j] == " ")
				{
					for (k = 0; k < 30; k++)
					{
						list[j][k] = temp[k];
					}
					found = true;
					counter++;
				}
			}
		}
		}
	if (counter == 20)
	{
		counter = 0;
		sort_words(list, count, holder, hold);

		swap_counter++;
	}



		//if not found yet increment a counter, maybe static variable, for resorting list at 20 new words, and increment counter for sort function
}
//adds word to list, or increments counter if found
void sort_words(char list[][30], int count[], char holder[], char hold[])
{
	int swap = 0;
	int i = 0;
	int j = 0;
	int k = 0; //for filling array
	int max = 20 * swap_counter;
	for (i = max; i > 0; i--)
	{
		for (j = max; j > 0; j--)
		{
			if (_stricmp(list[j], list[j - 1]) > 0)
			{
				swap = count[j];
				count[j] = count[j - 1];
				count[j - 1] = swap;
				hold = list[j];

				for (k = 0; k < 30; k++)
				{
					list[j][k] = list[j - 1][k];
				}

				for (k = 0; k < 30; k++)
				{
					list[j - 1][k] = hold[k];
				}
				
			}
		}
	}
}
//sorts the list, starting at the high end for 20 words
bool read_line(ifstream &fin, char line[])
{
	
	while (fin.getline(line, 100))
	{
		
		return true;
	}
	return false;
}
//reads a line, returns false at end of file
bool extract_word(char line[], char temp[], int &index)
{
	int i = 0;
	int j = 0;

	for (i = 0; i < 100; i++)
	{
		if (isalpha(line[i + index]))
		{
			temp[i] = line[index + i];
			index = i + index;
		}

		else if (!isalpha(line[i + index]))
		{
			break;
		}
	}

	for (j = i; !isalpha(line[i + index]); j--)
		{
				temp[j] = '\0';
		}

	return true;
}
//gets a word, returns false if no word found
void process_line(char list[][30], char line[], char temp[], int &index, int count[], char holder[], char hold[])
{

	extract_word(line, temp, index);
	add_word(list, temp, count, holder, hold);

}
//uses extract_word(), add_word()
bool write_list(ofstream &fout, char list[][30], int count[], char *argv[])
{
	int i = 0;
	fout.open(argv[2]);
	
	for (i = 0; i < 10000 && count[i] != 0; i++)
	{
		fout << list[i] << "\t" << count[i] << endl;
	}

	if (!fout)
	{
		return false;
	}
	return true;
}
//writes list to output file, returns false if file fails 
Last edited on
What do yo mean by "fails to work"? In other words what do you expect of the program, and what does it do instead?
Topic archived. No new replies allowed.