No data output/blank screen.

Hi, I am working on this program that counts the number of letters, words and lines in a given text file. So far, I have developed the code to count the number of letters and lines but I don't know where I messed up because I get a blank screen when I compile and run it. Any help would be much appreciated.

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


#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include <cctype>
using namespace std;

void initialize(int& lc, int list[]);//declaring prototypes
void characterCount (char ch, int list[]);
void readText(ifstream& intext, char& ch, int list[]);
void totalCount( int lc, int list[]);


int main()
{

int lineCount;
int letterCount[26]; //Because there are 26 letters in the english language, we create an array of 26 components.
char ch;
ifstream infile;

infile.open("C:\\mp6frequency.txt");

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

initialize(lineCount, letterCount);
infile.get(ch);

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

totalCount(lineCount, letterCount);

infile.close();



system("pause");
return 0;

}
//this function initializies the variable lineCount and the array letterCount to 0.
void initialize(int& lc, int list[])
{
	int j;
	lc = 0;

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

//this function increments the letter count. It also makes sure that it is only counting letters.
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]++;

}

//this function reads the text and counts the characters
void readText(ifstream& intext, char& ch, int list[])
{
while ( ch != '\n')
{
	characterCount (ch, list);

}
}

//this function displays data
void totalCount( int lc, int list[])
{
int index;

for ( index = 0; index < 26; index++)
	cout << static_cast<char>(index + static_cast<int>('A')) << ' ' << list[index] << endl;
cout << "The number of lines = " << lc << endl;



}
Come on guys, I know I messed up somewhere in this code.
Perhaps you could debug your code yourself. A common way is to stick a line such as
cout << "Entering function xxx" << endl; at the start of each function and
cout << "Leaving function xxx" << endl; at the end.
readText doesn't actually read any text.

ch never changes so you have an endless loop.
Thank you to both Moschops and cire. After using Moschops' method, I realized, as cire pointed out, that my program wasn't actually reading the characters. I took care of that now.


include below line at line number 80.
intext.get (ch);

You are blocked at while loop in line number 77 due to processing a single character always, you need to move forward to get next character and process.

Try to simplify the code and make use available functions to perform your required operation.
Hope you are beginner, my advise is learn every class / function before use. Think, analyze and write each and every line carefully which makes you a better programmer.

ALL THE BEST
Yep. Thank you richardforc. I do have a question for you, guys. I am working on a pseudocode for this same code. Now, I am going to make my program count the number of words in the text. I am going to create an array of strings but how do I make sure that I don't accidentally store a number in the array. What I mean by this is:

the input file sometimes has numbers in the middle of sentences. if I declare an array of strings and my program stores word after word and when it gets to that number, how do I make sure that it skips that number?
A number in this sense is usually a word whether it's spelled out or written with numeric digits, so ask yourself if you really need to treat it differently. And that's your answer by the way -- you look for the digits. If the 'word' consists of digits, don't store it.

get a word check with isalpha()/isdigit()/atoi().

The better you understand above functions before using any of those.
Topic archived. No new replies allowed.