Letter Frequency counter c++

My output is looking weird...it does not show capitals or lower letters. Can some one help me?

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
#include<iostream>
#include<string>
#include<fstream>
#include<iomanip>

using namespace std;

int main()
{
	fstream inFile;
	char oneChar;
	char asciiCode[128]; //127 characters of the ASCII +1 
	int frequency[128];
	
	for(int asciiCode = 0; asciiCode < 128; asciiCode++) //Loop through all the ASCII characters to get the frequency of each
	{
		frequency[asciiCode] = 0;
	}

	inFile.open("101 Scientific Facts and Foreknowledge.txt", ios::in); //Opens file

	if(inFile.fail())
	{
		cout << "Error: File not found!" << endl << endl; //If the file fail to open this message is displayed
	}
	else
	{
		oneChar = inFile.get();

		while(inFile.eof() == false) //End of file
		{
			if(oneChar != ' ')
			{
				frequency[oneChar]++; //When not empty character
			}
			oneChar = inFile.get();
		}
	}

	inFile.close();

	cout << "The Frequency of the capital letters are: " << endl;
	for(int caps = 65; caps <= 90; caps++)
	{
		cout << "Letter " << asciiCode[caps] << " is " << setw(4) << frequency[caps] << " times." << endl;
	}

	cout << "\nThe Frequency of the lower case letters are: " << endl;
	for(int lower = 97; lower <= 122; lower++)
	{
		cout << "Letter " << asciiCode[lower] << " is " << setw(4) << frequency[lower] << " times." << endl;
	}

	return 0; 
}
OUTPUT:

The Frequency of the lower case letters are:
Letter ╠ is 2478 times.
Letter ╠ is  476 times.
Letter ╠ is 1033 times.
Letter ╠ is 1328 times.
Letter ╠ is 4519 times.
Letter ╠ is  653 times.
Letter ╠ is  590 times.
Letter ╠ is 1635 times.
Letter ╠ is 2717 times.
Letter ╠ is   22 times.
Letter ╠ is  162 times.
Last edited on
The problem is that you don't initialize your asciiCode array and therefore it has only random values.
I really don't understand why you have an array named asciiCode (or why you think having an int with the same name is a good idea) since it is completely superfluous as far as the logic goes.

1
2
3
4
5
6
7
8
9
10
11
cout << "The Frequency of the capital letters are: " << endl;
	for(char caps = 'A'; caps <= 'Z'; caps++)
	{
		cout << "Letter " << caps << " is " << setw(4) << frequency[caps] << " times." << endl;
	}

	cout << "\nThe Frequency of the lower case letters are: " << endl;
	for(char lower = 'a'; lower <= 'z'; lower++)
	{
		cout << "Letter " << lower << " is " << setw(4) << frequency[lower] << " times." << endl;
	}
Fixed! Thank you Thomas1965, jgg2002 and cire. ^-^

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
#include<iostream>
#include<fstream>
#include<iomanip>

using namespace std;

int main()
{
	fstream inFile;
	char oneChar;
	char ASCII[128] = {0}; //127 characters of the ASCII +1 
	int frequency[128];
	char code;
	
	for(int ASCII = 0; ASCII < 128; ASCII++) //Loop through all the ASCII characters to get the frequency of each
	{
		frequency[ASCII] = 0;
	}

	inFile.open("101 Scientific Facts and Foreknowledge.txt", ios::in); //Reads file

	if(inFile.fail()) //Check for error opening the file
	{
		cout << "Error: File not found!" << endl << endl; 
	}
	else
	{
		oneChar = inFile.get();

		while(inFile.eof() == false) //End of file
		{
			if(oneChar != ' ')
			{
				frequency[oneChar]++; //When not empty character
			}
			oneChar = inFile.get();
		}
	}

	inFile.close();

	cout << "The Frequency of the capital letters are: " << endl;
	for(char caps = 'A'; caps <= 'Z'; caps++)
	{
		cout << "Letter " << caps << " is " << setw(4) << frequency[caps] << " times." << endl;
	}

	cout << "The Frequency of the lower case letters are: " << endl;
	for(char lower = 'a'; lower <= 'z'; lower++)
	{
		cout << "Letter " << lower << " is " << setw(4) << frequency[lower] << " times." << endl;
	}

	return 0; 
}


The Frequency of the capital letters are:
Letter A is  116 times.
Letter B is  115 times.
Letter C is  172 times.
Letter D is   57 times.
Letter E is   64 times.
Letter F is   49 times.
Letter G is  143 times.
Letter H is  101 times.
Letter I is  143 times.
Letter J is   64 times.
Letter K is    8 times.
Letter L is   64 times.
Letter M is   63 times.
Letter N is   39 times.
Letter O is   59 times.
Letter P is   56 times.
Letter Q is    0 times.
Letter R is  109 times.
Letter S is   83 times.
Letter T is  208 times.
Letter U is   11 times.
Letter V is   70 times.
Letter W is   45 times.
Letter X is    0 times.
Letter Y is   20 times.
Letter Z is    2 times.
The Frequency of the lower case letters are:
Letter a is 2478 times.
Letter b is  476 times.
Letter c is 1033 times.
Letter d is 1328 times.
Letter e is 4519 times.
Letter f is  653 times.
Letter g is  590 times.
Letter h is 1635 times.
Letter i is 2717 times.
Letter j is   22 times.
Letter k is  162 times.
Letter l is 1370 times.
Letter m is  663 times.
Letter n is 2445 times.
Letter o is 2411 times.
Letter p is  462 times.
Letter q is   18 times.
Letter r is 2095 times.
Letter s is 2482 times.
Letter t is 2884 times.
Letter u is  846 times.
Letter v is  456 times.
Letter w is  575 times.
Letter x is   85 times.
Letter y is  522 times.
Letter z is   23 times.
Press any key to continue . . .
You still have a superfluous array ASCII that isn't used anywhere for anything.
Topic archived. No new replies allowed.