Infinite Loop in Counting Function

I'm writing a program for class. He wants us to count the number of instances of each lowercase and uppercase letter in a file, store the count of each letter in a struct member (array of structs 0-51 representing a-z and A-Z). But I've run into a problem.

In my count function, which is supposed to count each occurrence of these letters, it just goes into an infinite loop. I have no idea why. I made sure to type a sentence into the input file for testing purposes...

input.txt (what I type in) contains "This is a test of my letter counting program."

Here is 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
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
/*
	Project: CIS225 Chapter 11 - Programming Exercise 6
	Author: packetpirate
	Last Updated: 02/11/2011
*/

#include <iostream>
using std::cout;
using std::cin;
using std::endl;
#include <fstream>
using std::ofstream;
using std::ifstream;
#include <string>
using std::string;
#include <iomanip>
using std::setw;
using std::right;
using std::left;
using std::setfill;
#include <limits>

struct input_t {
	int charCount;
};

void openFile(ofstream& fout, ifstream& fin);
void count(ifstream& fin, input_t myInput[], int& low, int& upp);
void printResult(ofstream& fout, input_t myInput[], int& low, int& upp);

int main(int argc, char* argv[])
{
	input_t myInput[52];
	int low = 0;
	int upp = 0;

	ofstream fout;
	ifstream fin;

	openFile(fout, fin);
	count(fin, myInput, low, upp);

	fout.close();
	fin.close();

	cin.get();
	cout << endl << "Press <ENTER> to continue...";
	cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');

	return 0;
}

void openFile(ofstream& fout, ifstream& fin)
{
	string out_name;
	cout << "Output File: ";
	cin >> out_name;

	fout.open(out_name.c_str());
	if(!fout)
	{
		cout << "Invalid output file. Exiting...";
		return;
	}

	string in_name;
	cout << "Input File: ";
	cin >> in_name;

	fin.open(in_name.c_str());
	if(!fin)
	{
		cout << "Invalid input file. Exiting...";
		return;
	}
}

void count(ifstream& fin, input_t myInput[], int& low, int& upp)
{
	char loc;

	while(!fin.eof())
	{
		cin.get(loc);

		switch(loc)
		{
		case 'a':
			myInput[0].charCount += 1;
			low++;
			break;
		case 'b':
			myInput[1].charCount += 1;
			low++;
			break;
		case 'c':
			myInput[2].charCount += 1;
			low++;
			break;
		case 'd':
			myInput[3].charCount += 1;
			low++;
			break;
		case 'e':
			myInput[4].charCount += 1;
			low++;
			break;
		case 'f':
			myInput[5].charCount += 1;
			low++;
			break;
		case 'g':
			myInput[6].charCount += 1;
			low++;
			break;
		case 'h':
			myInput[7].charCount += 1;
			low++;
			break;
		case 'i':
			myInput[8].charCount += 1;
			low++;
			break;
		case 'j':
			myInput[9].charCount += 1;
			low++;
			break;
		case 'k':
			myInput[10].charCount += 1;
			low++;
			break;
		case 'l':
			myInput[11].charCount += 1;
			low++;
			break;
		case 'm':
			myInput[12].charCount += 1;
			low++;
			break;
		case 'n':
			myInput[13].charCount += 1;
			low++;
			break;
		case 'o':
			myInput[14].charCount += 1;
			low++;
			break;
		case 'p':
			myInput[15].charCount += 1;
			low++;
			break;
		case 'q':
			myInput[16].charCount += 1;
			low++;
			break;
		case 'r':
			myInput[17].charCount += 1;
			low++;
			break;
		case 's':
			myInput[18].charCount += 1;
			low++;
			break;
		case 't':
			myInput[19].charCount += 1;
			low++;
			break;
		case 'u':
			myInput[20].charCount += 1;
			low++;
			break;
		case 'v':
			myInput[21].charCount += 1;
			low++;
			break;
		case 'w':
			myInput[22].charCount += 1;
			low++;
			break;
		case 'x':
			myInput[23].charCount += 1;
			low++;
			break;
		case 'y':
			myInput[24].charCount += 1;
			low++;
			break;
		case 'z':
			myInput[25].charCount += 1;
			low++;
			break;
		case 'A':
			myInput[26].charCount += 1;
			upp++;
			break;
		case 'B':
			myInput[27].charCount += 1;
			upp++;
			break;
		case 'C':
			myInput[28].charCount += 1;
			upp++;
			break;
		case 'D':
			myInput[29].charCount += 1;
			upp++;
			break;
		case 'E':
			myInput[30].charCount += 1;
			upp++;
			break;
		case 'F':
			myInput[31].charCount += 1;
			upp++;
			break;
		case 'G':
			myInput[32].charCount += 1;
			upp++;
			break;
		case 'H':
			myInput[33].charCount += 1;
			upp++;
			break;
		case 'I':
			myInput[34].charCount += 1;
			upp++;
			break;
		case 'J':
			myInput[35].charCount += 1;
			upp++;
			break;
		case 'K':
			myInput[36].charCount += 1;
			upp++;
			break;
		case 'L':
			myInput[37].charCount += 1;
			upp++;
			break;
		case 'M':
			myInput[38].charCount += 1;
			upp++;
			break;
		case 'N':
			myInput[39].charCount += 1;
			upp++;
			break;
		case 'O':
			myInput[40].charCount += 1;
			upp++;
			break;
		case 'P':
			myInput[41].charCount += 1;
			upp++;
			break;
		case 'Q':
			myInput[42].charCount += 1;
			upp++;
			break;
		case 'R':
			myInput[43].charCount += 1;
			upp++;
			break;
		case 'S':
			myInput[44].charCount += 1;
			upp++;
			break;
		case 'T':
			myInput[45].charCount += 1;
			upp++;
			break;
		case 'U':
			myInput[46].charCount += 1;
			upp++;
			break;
		case 'V':
			myInput[47].charCount += 1;
			upp++;
			break;
		case 'W':
			myInput[48].charCount += 1;
			upp++;
			break;
		case 'X':
			myInput[49].charCount += 1;
			upp++;
			break;
		case 'Y':
			myInput[50].charCount += 1;
			upp++;
			break;
		case 'Z':
			myInput[51].charCount += 1;
			upp++;
			break;
		default:
			break;
		}
	}
}

void printResult(ofstream& fout, input_t myInput[], int low, int upp)
{

}


So does anyone know what's causing this infinite loop?
Yes

Your problem is you're never actually incrementing your file's position pointer, in count()

At first glance, i'd say change cin.get(blahblah) to fin.get(blahblah)
<facepalm>

Thanks...
Ok, new problem...

Now I'm having trouble with my print function...

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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
/*
	Project: CIS225 Chapter 11 - Programming Exercise 6
	Author: packetpirate
	Last Updated: 02/11/2011
*/

#include <iostream>
using std::cout;
using std::cin;
using std::endl;
#include <fstream>
using std::ofstream;
using std::ifstream;
#include <string>
using std::string;
#include <iomanip>
using std::setw;
using std::right;
using std::left;
using std::setfill;
#include <limits>

struct input_t {
	char x;
	int charCount;
};

void openFile(ofstream& fout, ifstream& fin);
void count(ifstream& fin, input_t myInput[], int& low, int& upp);
void printResult(ofstream& fout, input_t myInput[], int& low, int& upp);

int main(int argc, char* argv[])
{
	input_t myInput[52];
	char letters[] = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z',
	'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};

	for(int i = 0;i < 52;i++)
	{
		myInput[i].x = letters[i];
	}

	int low = 0;
	int upp = 0;

	ofstream fout;
	ifstream fin;

	openFile(fout, fin);
	count(fin, myInput, low, upp);
	printResult(fout, myInput, low, upp);

	fout.close();
	fin.close();

	cin.get();
	cout << endl << "Press <ENTER> to continue...";
	cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');

	return 0;
}

void openFile(ofstream& fout, ifstream& fin)
{
	string out_name;
	cout << "Output File: ";
	cin >> out_name;

	fout.open(out_name.c_str());
	if(!fout)
	{
		cout << "Invalid output file. Exiting...";
		return;
	}

	string in_name;
	cout << "Input File: ";
	cin >> in_name;

	fin.open(in_name.c_str());
	if(!fin)
	{
		cout << "Invalid input file. Exiting...";
		return;
	}
}

void count(ifstream& fin, input_t myInput[], int& low, int& upp)
{
	char loc;

	while(fin)
	{
		fin.get(loc);

		switch(loc)
		{
		case 'a':
			myInput[0].charCount += 1;
			low++;
			break;
		case 'b':
			myInput[1].charCount += 1;
			low++;
			break;
		case 'c':
			myInput[2].charCount += 1;
			low++;
			break;
		case 'd':
			myInput[3].charCount += 1;
			low++;
			break;
		case 'e':
			myInput[4].charCount += 1;
			low++;
			break;
		case 'f':
			myInput[5].charCount += 1;
			low++;
			break;
		case 'g':
			myInput[6].charCount += 1;
			low++;
			break;
		case 'h':
			myInput[7].charCount += 1;
			low++;
			break;
		case 'i':
			myInput[8].charCount += 1;
			low++;
			break;
		case 'j':
			myInput[9].charCount += 1;
			low++;
			break;
		case 'k':
			myInput[10].charCount += 1;
			low++;
			break;
		case 'l':
			myInput[11].charCount += 1;
			low++;
			break;
		case 'm':
			myInput[12].charCount += 1;
			low++;
			break;
		case 'n':
			myInput[13].charCount += 1;
			low++;
			break;
		case 'o':
			myInput[14].charCount += 1;
			low++;
			break;
		case 'p':
			myInput[15].charCount += 1;
			low++;
			break;
		case 'q':
			myInput[16].charCount += 1;
			low++;
			break;
		case 'r':
			myInput[17].charCount += 1;
			low++;
			break;
		case 's':
			myInput[18].charCount += 1;
			low++;
			break;
		case 't':
			myInput[19].charCount += 1;
			low++;
			break;
		case 'u':
			myInput[20].charCount += 1;
			low++;
			break;
		case 'v':
			myInput[21].charCount += 1;
			low++;
			break;
		case 'w':
			myInput[22].charCount += 1;
			low++;
			break;
		case 'x':
			myInput[23].charCount += 1;
			low++;
			break;
		case 'y':
			myInput[24].charCount += 1;
			low++;
			break;
		case 'z':
			myInput[25].charCount += 1;
			low++;
			break;
		case 'A':
			myInput[26].charCount += 1;
			upp++;
			break;
		case 'B':
			myInput[27].charCount += 1;
			upp++;
			break;
		case 'C':
			myInput[28].charCount += 1;
			upp++;
			break;
		case 'D':
			myInput[29].charCount += 1;
			upp++;
			break;
		case 'E':
			myInput[30].charCount += 1;
			upp++;
			break;
		case 'F':
			myInput[31].charCount += 1;
			upp++;
			break;
		case 'G':
			myInput[32].charCount += 1;
			upp++;
			break;
		case 'H':
			myInput[33].charCount += 1;
			upp++;
			break;
		case 'I':
			myInput[34].charCount += 1;
			upp++;
			break;
		case 'J':
			myInput[35].charCount += 1;
			upp++;
			break;
		case 'K':
			myInput[36].charCount += 1;
			upp++;
			break;
		case 'L':
			myInput[37].charCount += 1;
			upp++;
			break;
		case 'M':
			myInput[38].charCount += 1;
			upp++;
			break;
		case 'N':
			myInput[39].charCount += 1;
			upp++;
			break;
		case 'O':
			myInput[40].charCount += 1;
			upp++;
			break;
		case 'P':
			myInput[41].charCount += 1;
			upp++;
			break;
		case 'Q':
			myInput[42].charCount += 1;
			upp++;
			break;
		case 'R':
			myInput[43].charCount += 1;
			upp++;
			break;
		case 'S':
			myInput[44].charCount += 1;
			upp++;
			break;
		case 'T':
			myInput[45].charCount += 1;
			upp++;
			break;
		case 'U':
			myInput[46].charCount += 1;
			upp++;
			break;
		case 'V':
			myInput[47].charCount += 1;
			upp++;
			break;
		case 'W':
			myInput[48].charCount += 1;
			upp++;
			break;
		case 'X':
			myInput[49].charCount += 1;
			upp++;
			break;
		case 'Y':
			myInput[50].charCount += 1;
			upp++;
			break;
		case 'Z':
			myInput[51].charCount += 1;
			upp++;
			break;
		default:
			break;
		}
	}
}

void printResult(ofstream& fout, input_t myInput[], int& low, int& upp)
{
	for(int i = 0;i < 52;i++)
	{
		fout << setfill(' ');
		fout << left << setw(5) << myInput[i].x << right << setw(5) << myInput[i].charCount << endl;
	}
}


I'm getting the following output.


a -858993458
b -858993460
c -858993459
d -858993460
e -858993457
f -858993459
g -858993458
h -858993459
i -858993457
j -858993460
k -858993460
l -858993459
m -858993458
n -858993458
o -858993457
p -858993459
q -858993460
r -858993457
s -858993457
t -858993455
u -858993459
v -858993460
w -858993460
x -858993460
y -858993459
z -858993460
A -858993460
B -858993460
C -858993460
D -858993460
E -858993460
F -858993460
G -858993460
H -858993460
I -858993460
J -858993460
K -858993460
L -858993460
M -858993460
N -858993460
O -858993460
P -858993460
Q -858993460
R -858993460
S -858993460
T -858993459
U -858993460
V -858993460
W -858993460
X -858993460
Y -858993460
Z -858993460


Why is it giving me those weird numbers?
Last edited on
You never initialize the count member variable of your type so they are garbage.
interesting cases.. why not convert ur letters into decimal values using ascii table like...

'A to Z' = 65 to 90
'a to z' = 97 to 122

then offset it accordingly?
Ultima, someone at ProgrammingForums.org already suggested that. I'm not counting instances of letters in a string. I'm counting them from a file.
I'd like to hear alternative suggestions, though.
Don't be so narrow-minded
I'm not counting instances of letters in a string. I'm counting them from a file.
You are counting letters, period.
Besides, if you know how to count letters in a string then you just pass all the content of your file to a string and count the letters.
Do you see the beauty of modularity?
Except that the assignment tells me not to do that... it tells me to store the individual characters in an array of structs.
Topic archived. No new replies allowed.