I am writing a program and I am having some trouble finishing it
Here is my prompt:
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. 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.
You must use an array to keep track of how many times each letter is seen in the text file. The array should have 26 elements (one for each letter in the alphabet). Index 0 should be used to track the number of A's in the file, index 1 to track the B's, index 2 to track the C's, etc, up to index 25 for the Z's. You could use a massive if/else block or switch statement, but the whole reason to use arrays is to make your programs easier. So, instead, think about how to convert each character you read into the correct index and then increment that value in the array. For example, if you read an A, then you should increment the value in index 0. Specifically, you will need to determine if the character is an uppercase letter (between 'A' and 'Z'), a lowercase letter (between 'a'and 'z'), or something else. If it is a letter, convert it into the appropriate index.
Here is what I have written so far:
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
|
#include <iostream>
#include <string>
#include <cctype>
#include <fstream>
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 << "Cannot open file." << endl;
return 0;
}
letterCount[char] += 1;
for (int i = 0; i < 26; i++)
{
cout << "Number 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;
}
}
|
Any help or tips are apperciated