| | |
trouble with counting letter from file, please help.
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Mar 2006
Posts: 9
Reputation:
Solved Threads: 0
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:
the number it came up is worng and it doesn't count "this" "the " and "is"
Please help me!
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:
C++ Syntax (Toggle Plain Text)
#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!
•
•
Join Date: Jul 2005
Posts: 1,671
Reputation:
Solved Threads: 261
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).
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).
•
•
Join Date: Mar 2006
Posts: 9
Reputation:
Solved Threads: 0
i did what you said and now this is my code:
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
C++ Syntax (Toggle Plain Text)
#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
•
•
Join Date: Jul 2005
Posts: 1,671
Reputation:
Solved Threads: 261
>>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:
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.
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:
C++ Syntax (Toggle Plain Text)
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.
![]() |
Similar Threads
- Capturing data from txt file using JavaScript/HTML (JavaScript / DHTML / AJAX)
- hangman revised (VB.NET)
- Missing File with office 2003??? (Windows Software)
- configuring samba networking on windows (Kernels and Modules)
- help for program involving switch loops and file (C++)
Other Threads in the C++ Forum
- Previous Thread: Visual C++.NET programming problem
- Next Thread: Creating dynamic array structures
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char char* class code coding compile compiler console conversion count database delete deploy desktop developer dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker list loop looping loops map math memory multiple news node number numbertoword output parameter pointer problem program programming project python random read recursion recursive reference rpg sorting string strings struct temperature template test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






