Looping issue

Hey guys and gals. I'm currently a student in an advanced c++ class and I'm working on a functions and arrays lab, while I can say the lab's complete I've been going through the book doing examples. We're using C++ PROGRAMMING (Program Design Including Data Structures) 5th Edition written by D.S. Malik

Any who, when trying to explain one of the examples to a fellow class mate we encountered a problem while running the program.

What the overall program does is reads a text file, outputs the texts as is, and also prints the number of lines and the number of times each letter appears in the text. Upper and lower case letters are treated as being the same; that is, they are tallied together.

Here's the code for it, it runs an endless loop and I can't figure out why. When I manually close the console window, I open the output text document and it hangs up while loading then displays to me the original input document followed by an infinite amount of """""""""

I've already tweaked the author's code because he had some variables spelled incorrectly and in another instance had a single & in his "if (0 <= index && index < 26)" statement! I'm thinking there's something wrong with his while loop or something with in a function that I just can't see.
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
#include <iostream>
#include <fstream>
#include <cctype>

using namespace std;

void initialize (int& lc, int list[]);
void copyText (ifstream& intext, ofstream& outtext, char& ch, int list[]);
void characterCount (char ch, int list[]);
void writeTotal (ofstream& outtext, int lc, int list[]);

int main()
{
	//Declare variables
	int lineCount;
	int letterCount[26];
	char ch;
	ifstream infile;
	ofstream outfile;

	infile.open("textin.txt");

	if (!infile)
	{
		cout << "Cannot open the input file." << endl;
		return 1;
	}

	outfile.open("textout.txt");

	initialize(lineCount, letterCount);

	infile.get(ch);

	while (infile)
	{
		copyText(infile, outfile, ch, letterCount);
		lineCount++;
		infile.get(ch);
	}

	writeTotal(outfile, lineCount, letterCount);

	infile.close();
	outfile.close();

	return 0;
}

void initialize (int& lc, int list[])
{
	int j;
	lc = 0;

	for (j = 0; j < 26; j++)
		list[j] = 0;
}

void copyText (ifstream& intext, ofstream& outtext, char& ch, int list[])
{
	while (ch != '\n')
	{
		outtext << ch;

		characterCount (ch, list);

		intext.get(ch);
	}
	outtext << ch;
}
void characterCount (char ch, int list[])
{
	int index; 

	ch = toupper(ch);

	index = static_cast<int>(ch) - static_cast<int>('A');

	if (0 <= index && index < 26)
		list [index]++;
}
void writeTotal (ofstream& outtext, int lc, int list[])
{
	int index;

	outtext << endl << endl;
	outtext << "The number of lines = " << lc << endl;

	for (index = 0; index < 26; index++)
		outtext << static_cast<char>(index + static_cast<int>('A')) << "count = " << list[index] << endl;
}
Last edited on
Never mind guys, I found the issue. I flagged the EOF loop and it both pulled in my data and got rid of my warning then ran like I wanted it to!

Thanks
Topic archived. No new replies allowed.