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!

Recommended Answers

All 3 Replies

Pass thisCount, theCount and isCount to count() by reference rather than by value. HINT(use references or a pointers as parameters)

Initialize all values in letterCount[] to zero rather than a 26 digit value of zero. (I think a single zero in the parenthesis will do it, but it's easy enough for you to check whether it does or not).

i did what you said and now this is my code:

#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]={0};        //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") || strcmp(word,"This"))             //count this
                thisCount++;
                if (strcmp(word,"is") || strcmp(word,"Is"))               //count is
                isCount++;
                if (strcmp(word,"the") || strcmp(word,"The"))              //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;
}

and this is what i got running it (the poems file contain: This is the test):
///////////////
This is the statistic of the file poems.dat
3 e
2 h
2 i
4 s
6 t
there's 5 this
there's 5 the
there's 5 is
///////////////////
the numbers are wrong and i still don't know why

>>while(!infile.eof()) //begin while loop, not end of file

Fine sentiment, and will work, but is likely to cause an overread of file and may be contributing to your erroneous output. You would be better off doing this:

while(infile >> word)
{
   if(strcmp(word, "this") == 0)
   //etc.
}

Searching the board for why it isn't a good idea to use the return value of eof() as the conditional of a loop reading a file would be worth your while.

Having said that using infile >> word would be better in most situations for reading file contents doesn't point out the mild restriction inherent in this syntax. To wit, >> will return the equivalent of true if it is able to enter data in the lhs variable appropriate to that variable. It will return the equivalent of false if it encounters EOF (the end of file marker) or invalid input for the respective variable. When the >> operator returns false it also changes the state variable of the stream and in order to use the stream again you need to clear the stream using the clear() method built into the stream class interface. If you are done with the stream and won't be using it again the stream will clear on destruction. When a loop stops that has the return of >> as the conditional you should determine whether it stopped because it successfully read the entire file and found EOF (this is the appropriate place to use the return value of eof()) or for some other reason (invalid input from abnormal file contents or whatever). If it stopped because it found EOF, meaning eof() returns true, then you are happy and proceed with the program unimpeded.

If changing the conditional of the file reading loop fixes things, fine. If not then I would sprinkle a bunch of couts through the code reading the file to be sure you are reading things as you think you are and accumulating things where you think you are. This is a manual type of debugging that could be done more efficiently with a debugger if you have one and know how to use it. Learning how to debug your code manually or using a debugger will be just as useful for you as knowing how to use to write the rough draft of the code in the first place.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.