This program will analyze a text file given by the user and report the frequency of letters within the file. The program is not case-sensitive, so all alphabetical characters will be counted and reported. A sample run could look something similar to the following.
Greetings! My name is Brent and I will be analyzing a text file for you.
Please enter the name of the file: dictionary.txt
Here is a report on the frequency of each alphabetical character in the file "dictionary.txt".
Letter Frequency
A 55
B 31
...
Z 2
Thank you for using this program. Goodbye.
So here is what I currently have. It is able to open the file but I don't know where to go from here.
/**
* File: Letter Frequency
* Author: Jake John
* Course: CS I
* Assignment: Letter Frequency
* Due Date: March 8, 2021
*
* This program will analyze a text file given by the user and report
* the frequency of letters within the file. The program is not
* case-sensitive, so all alphabetical characters will be counted and reported.
*/
#include <bits/stdc++.h>
usingnamespace std;
ifstream inFile;
int main(){
cout << "Greetings! My name is Kolton Johnson and I will be analyzing";
cout << " a text file for you." << endl;
cout << "Please enter the name of the file: \dictionary.txt\" ";
cout << "Here is a report on the frequency of each alphabetical character";
cout << "in the file: \"dictionary.txt\"" << endl;
inFile.open("dictionary.txt");
if(!inFile){
cout << "Error: Unable to open file.\n";
exit(0);
}
return 0;
}
????
How exactly did you get that output with that program? In your cout statement, you have several things that don't jibe with your output. The name, for one.
But, to count the frequency of letters in a text file, you can read in each letter, check it with either a switch or an if/else if statement, and use a loop with an accumulator to count up the frequency.
I have this code for reference. It find a word from the same file but I don't know who to change it from reading the words and finding it to reading each word and telling how many letters in it and how many of each.
#include <bits/stdc++.h>
usingnamespace std;
int main()
{
int freq[128]; // frequencies of letters
ifstream inFile; // input file
char ch;
inFile.open("dictionary.txt");
if (!inFile)
{
cout << "The input file could not be opened."<<endl;
return 1;
}
// initialize frequency counts to zero for each possible letter
for (int k = 0; k < 128; k++)
{
freq[k] = 0;
}
// Read the file, keeping track of frequency with which each letter occurs
ch = inFile.get();
while (ch != EOF)
{
cout << ch;
ch = toupper(ch);
freq[ch]++;
ch = inFile.get();
}
// Print the output table
cout << endl << "Letter frequencies in this file are as follows." << endl;
for (char ch = 'A'; ch <= 'Z'; ch++)
{
cout << ch << " : " << freq[ch] << endl;
}
return 0;
}
@jake john,
What do you mean? I thought you wanted to find the frequency of every letter in a text file? You can't do that without reading every word and analyzing each letter. Please, explain.
Also, why are you including <bits/stdc++.h>?? That is a nonstandard, platform-dependent library. There's no problem with using it, just be prepared for when you sell software to someone, and it crashes on them because they don't have that library.
>There's no problem with using it, just be prepared for when you sell software to someone, and it crashes on them because they don't have that library.
lolwut. It's not a matter of runtime/crashing, it just isn't a standard header and isn't portable. I agree it shouldn't be used.