Write a program that, given a file of text (called poems.dat), will read in several lines of text, until end of file is reached and display a table indicating the number of occurrences of each letter in the alphabet in the text. For example, the phrase “This is great fun! and 1 ‘a’, and 2 ‘i’s, etc. Expand this to also count the number of occurrences of 3 commonly used words: “This, “the, and “is. This word search should “not be case sensitive (this should match to This) and should ignore punctuation.
Requirements: Write at least 1 function with arguments in this program. Do not use any global variables. Use at least one array of characters and one array of integers in this program.
You will want to use arrays of character(s), and read in at least a word at a time to process. Please use the subscript operator and strcmp to solve this programming problem.
Make sure your program still works, even if poems.dat is empty or does not exist in your current working directory.
NEVER give path names for where a file is to be located – always assume your external data files will be located in your current working directory
And i came up with this so far:
#include <fstream>
#include <iostream>
using namespace std;
/////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
// This program read the file called "poems.dat' and display the occurence
//of a letter in the file. It also display the occurence of three words
//'this', 'the', and 'is'.
//
// This program assumming that the file contains no digit number.
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
void count(int letterCount[], int thisCount, int isCount, int theCount);
void output(int letterCount[],char letter[], int thisCount, int isCount, int theCount);
//////////////////////////////////////////////////////////////////////////
int main() //start main
{
int thisCount=0; //variable counting this
int theCount=0; //variable counting the
int isCount=0; // varibale count is
int letterCount[26]={00000000000000000000000000}; //interger array for storing the occurency
char letter[27]="abcdefghigklmnopqrstuvwxyz"; //char array for storing the alphabet
count (letterCount, thisCount, isCount, theCount);
output (letterCount, letter, thisCount, isCount, theCount);
cout<<endl;
return 0;
} //end main
/////////////////////////////////////////////////////////////////////////
void count(int letterCount[], int thisCount, int isCount, int theCount)
{
char word[80]; //array for storing intake words
char c; //variable for comparing letter
ifstream infile;
infile.open("poems.dat"); //open file
while(!infile.eof()) //begin while loop, not end of file
{
infile>>word; //intake word
if (strcmp(word,"this")==0) //count this
thisCount++;
if (strcmp(word,"is")==0) //count is
isCount++;
if (strcmp(word,"the")==0) //count the
theCount++;
for (int i=0; i<strlen(word); i++) //begin for loop
{
c=word[i];
c=tolower(c);
//using ASCII, assumming the file contents no digit number
++letterCount[c - 'a'];
} //end for loop
}
infile.close(); //close file
}
/////////////////////////////////////////////////////////////////////////
//Function for displaying the result
////////////////////////////////////////////////////////////////////////
void output(int letterCount[], char letter[], int thisCount, int isCount, int theCount)
{
cout<<"This is the statistic of the file poems.dat"<<endl;
for (int i = 0; i < 26; i++) //begin for loop;
{
if (letterCount[i] > 0) //begin if
{
//display the number of occurence and the letter
cout << letterCount[i]<<"\t" <<letter[i] << endl;
} //end if
}
//Display the occurence of this, the and is
cout<<"there's "<<thisCount<<" this"<<endl;
cout<<"there's "<<theCount<<" the"<<endl;
cout<<"there's "<<isCount<<" is"<<endl;
}
the number it came up is worng and it doesn't count "this" "the " and "is"
Please help me!