Word and Letter counter

Hi guys, beginner here looking for help, the program is supposed to open a file and count the occurrences of each character as well as count the number of words in that file. but i cant seem to get the word counter to work with the character counter. any help would be 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
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
#include <cstdlib>
using namespace std;

const int Alph_Count = 26;
struct Letter_Count
{
	int count;
	char ch;
};

void Process(ifstream &in, Letter_Count stats[], int &i, char &ch) //Count characters
{
	for (i = 0; i<Alph_Count; i++)
	{
		stats[i].count = 0;
		stats[i].ch = ('a' + i);
	}
	while (!in.eof())
	{
		in.get(ch);

		if ('a' <= ch && ch <= 'z')

			stats[ch - 'a'].count++;

		else if ('A' <= ch && ch <= 'Z')

			stats[ch - 'A'].count++;
	}
}


int main()
{
	char ch;
	int i;
	int counter = 0;
	string words;
	Letter_Count stats[Alph_Count];

	ifstream in;
	in.open("mytext.dat");

	while (!in.eof())
	{
		/*while (in >> words) // WORD Counter
		{
			counter++;
		}
		cout << counter << " Words" << endl;*/

		Process(in, stats, i, ch);
		for (i = 0; i<Alph_Count; i++) //Print characters
		{
			if (stats[i].count>0)
			{
				cout << left << setw(4) << stats[i].count << stats[i].ch << endl;
			}
		}	
	}
	in.close();
	return 0;
}


Output should be something like this:

15 Words
6 a
3 d
6 e
3 g
3 h
3 i
15 l
6 o
6 r
3 s
3 t
3 w

Thanks!
Hello LastSeen,

It is hard to find a starting place since there are so many problems. I will start with line 48, Not a good idea to check for end of file there because it may notwork the way you expect. EOF is set when you try to read past end of file and by the time you check for EOF you have already tried to process a bad read. A better way to write the while loop is:while (std::getline(in, line)) this way when it tries to read past EOF it will fail and the while loop will become false and terminate.

At line 50 I would set a string stream equal to line and use the string stream in the current while loop at line 50 with the code:
1
2
3
4
while (getline(ss, words, ' '))
{
    counter++;
}

this way the string stream ss works just like reading the file, but you can reuse the ss and you will not have to back up the file pointer to reread something you have already read.

In line 56 you could pass the array and the string stream or the variable line to the function, the int i and char ch are not needed. I have not tried it yet, but the variable line might be a better choice in order to access each character in the string.

In line 15 the int i and char ch are not needed there and the reference if pointless because you are not changing these values. The char ch variable should be defined inside the function process because that is where it is need.. And the for loops anywhere should be written as for (int i = 0; i < Alph_Count; i++). This way you do not have to get the definition of i from main. If the function parameters only contain the array and string stream you will not have to deal with moving the file pointer back to reread what you have already read.

Line 22 do not test for EOF there. Line 24 I would replace this with
1
2
3
4
5
for (int i = 0; i < line.length(), i++)
{
    if (toupper(line[i]) >= 'a' && toupper(line[i]) <= 'z')
        stats[line[i] - 'A'].count++;
}

Then the check for lower case letters will not be needed.

The final loop looks like it should work, I have not tested it yet. Work on these changes and let us know what problems you have or any questions you might have.

Hope that helps,

Andy
Topic archived. No new replies allowed.