adding up how many letters of the alphabet are in a file

Write a program which analyzes a text file by counting the number of times each of the 26 letters in the alphabet occurs in that file. Uppercase and lowercase letters should be counted together (for example, both ‘A’ and ‘a’ should count as an A). Any characters that are not letters should be ignored. You must prompt the user to enter the file name to be analyzed. After the entire file has been read, print out the number of times each letter was seen. You will need to read the file one character at a time by using a char variable for input.


This is what I got thus far:

#include <iostream>
#include <fstream>
#include <cctype> // meaning c++ type, functions take the int equivalent of a char, then returnan int that could be another char.
#include <string>

using namespace std;

int main()
{
int letterCount[26] = { 0 };
string inf;
ifstream fin;
int index;

cout << "Enter the name of the input file: ";
cin >> inf;
fin.open(inf);

if (fin.fail())
{
cout << "File does not exist." << endl;
exit(1);
}

letterCount[char] += 1

for (int i = 0; i < 26; i++)
{
cout << "\nNumber of As: " << letterCount[i] << endl;
cout << "Number of Bs: " << letterCount[i] << endl;
cout << "Number of Cs: " << letterCount[i] << endl;
cout << "Number of Ds: " << letterCount[i] << endl;
cout << "Number of Es: " << letterCount[i] << endl;
cout << "Number of Fs: " << letterCount[i] << endl;
cout << "Number of Gs: " << letterCount[i] << endl;
cout << "Number of Hs: " << letterCount[i] << endl;
cout << "Number of Is: " << letterCount[i] << endl;
cout << "Number of Js: " << letterCount[i] << endl;
cout << "Number of Ks: " << letterCount[i] << endl;
cout << "Number of Ls: " << letterCount[i] << endl;
cout << "Number of Ms: " << letterCount[i] << endl;
cout << "Number of Ns: " << letterCount[i] << endl;
cout << "Number of Os: " << letterCount[i] << endl;
cout << "Number of Ps: " << letterCount[i] << endl;
cout << "Number of Qs: " << letterCount[i] << endl;
cout << "Number of Rs: " << letterCount[i] << endl;
cout << "Number of Ss: " << letterCount[i] << endl;
cout << "Number of Ts: " << letterCount[i] << endl;
cout << "Number of Us: " << letterCount[i] << endl;
cout << "Number of Vs: " << letterCount[i] << endl;
cout << "Number of Ws: " << letterCount[i] << endl;
cout << "Number of Xs: " << letterCount[i] << endl;
cout << "Number of Ys: " << letterCount[i] << endl;
cout << "Number of Zs: " << letterCount[i] << endl;
return 0;
}
/*char input;
while (!fin.eof()) {
fin.get(input);
tolower(input);
index = input - 'a';
letterCount[index] += 1;
}
for (int i = 0; i < 26; i++) {
cout << letterCount[i] << endl;
}*/
}
Here is your code with code tags so we can talk about line numbers:
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 <fstream>
#include <cctype>
#include <string>

using namespace std;

int main()
{
int letterCount[26] = { 0 };
string inf;
ifstream fin;
int index;

cout << "Enter the name of the input file: ";
cin >> inf;
fin.open(inf);

if (fin.fail())
{
cout << "File does not exist." << endl;
exit(1);
}

letterCount[char] += 1

for (int i = 0; i < 26; i++)
{
cout << "\nNumber of As: " << letterCount[i] << endl;
cout << "Number of Bs: " << letterCount[i] << endl;
cout << "Number of Cs: " << letterCount[i] << endl;
cout << "Number of Ds: " << letterCount[i] << endl;
cout << "Number of Es: " << letterCount[i] << endl;
cout << "Number of Fs: " << letterCount[i] << endl;
cout << "Number of Gs: " << letterCount[i] << endl;
cout << "Number of Hs: " << letterCount[i] << endl;
cout << "Number of Is: " << letterCount[i] << endl;
cout << "Number of Js: " << letterCount[i] << endl;
cout << "Number of Ks: " << letterCount[i] << endl;
cout << "Number of Ls: " << letterCount[i] << endl;
cout << "Number of Ms: " << letterCount[i] << endl;
cout << "Number of Ns: " << letterCount[i] << endl;
cout << "Number of Os: " << letterCount[i] << endl;
cout << "Number of Ps: " << letterCount[i] << endl;
cout << "Number of Qs: " << letterCount[i] << endl;
cout << "Number of Rs: " << letterCount[i] << endl;
cout << "Number of Ss: " << letterCount[i] << endl;
cout << "Number of Ts: " << letterCount[i] << endl;
cout << "Number of Us: " << letterCount[i] << endl;
cout << "Number of Vs: " << letterCount[i] << endl;
cout << "Number of Ws: " << letterCount[i] << endl;
cout << "Number of Xs: " << letterCount[i] << endl;
cout << "Number of Ys: " << letterCount[i] << endl;
cout << "Number of Zs: " << letterCount[i] << endl;
return 0;
}
/*char input;
while (!fin.eof()) {
fin.get(input);
tolower(input);
index = input - 'a';
letterCount[index] += 1;
}
for (int i = 0; i < 26; i++) {
cout << letterCount[i] << endl;
}*/
}


What you have in the comments at lines 57-66 is actually really close. THe only problem is that you need to ensure that the character is a letter before executing lines 60-62. Use isalpha(input) for this.
All set!! Thank you!
Topic archived. No new replies allowed.