944,017 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 3457
  • C++ RSS
Mar 15th, 2006
0

trouble with counting letter from file, please help.

Expand Post »
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:

C++ Syntax (Toggle Plain Text)
  1. #include <fstream>
  2. #include <iostream>
  3. using namespace std;
  4. /////////////////////////////////////////////////////////////////////////////
  5. ////////////////////////////////////////////////////////////////////////////
  6. // This program read the file called "poems.dat' and display the occurence
  7. //of a letter in the file. It also display the occurence of three words
  8. //'this', 'the', and 'is'.
  9. //
  10. // This program assumming that the file contains no digit number.
  11. //////////////////////////////////////////////////////////////////////////////
  12. //////////////////////////////////////////////////////////////////////////////
  13. void count(int letterCount[], int thisCount, int isCount, int theCount);
  14. void output(int letterCount[],char letter[], int thisCount, int isCount, int theCount);
  15.  
  16. //////////////////////////////////////////////////////////////////////////
  17. int main() //start main
  18. {
  19. int thisCount=0; //variable counting this
  20. int theCount=0; //variable counting the
  21. int isCount=0; // varibale count is
  22. int letterCount[26]={00000000000000000000000000}; //interger array for storing the occurency
  23. char letter[27]="abcdefghigklmnopqrstuvwxyz"; //char array for storing the alphabet
  24. count (letterCount, thisCount, isCount, theCount);
  25. output (letterCount, letter, thisCount, isCount, theCount);
  26. cout<<endl;
  27. return 0;
  28. } //end main
  29.  
  30. /////////////////////////////////////////////////////////////////////////
  31. void count(int letterCount[], int thisCount, int isCount, int theCount)
  32. {
  33. char word[80]; //array for storing intake words
  34. char c; //variable for comparing letter
  35. ifstream infile;
  36. infile.open("poems.dat"); //open file
  37. while(!infile.eof()) //begin while loop, not end of file
  38. {
  39. infile>>word; //intake word
  40. if (strcmp(word,"this")==0) //count this
  41. thisCount++;
  42. if (strcmp(word,"is")==0) //count is
  43. isCount++;
  44. if (strcmp(word,"the")==0) //count the
  45. theCount++;
  46.  
  47. for (int i=0; i<strlen(word); i++) //begin for loop
  48. {
  49. c=word[i];
  50. c=tolower(c);
  51. //using ASCII, assumming the file contents no digit number
  52. ++letterCount[c - 'a'];
  53. } //end for loop
  54. }
  55. infile.close(); //close file
  56.  
  57. }
  58.  
  59. /////////////////////////////////////////////////////////////////////////
  60. //Function for displaying the result
  61. ////////////////////////////////////////////////////////////////////////
  62. void output(int letterCount[], char letter[], int thisCount, int isCount, int theCount)
  63. {
  64. cout<<"This is the statistic of the file poems.dat"<<endl;
  65. for (int i = 0; i < 26; i++) //begin for loop;
  66. {
  67. if (letterCount[i] > 0) //begin if
  68. {
  69. //display the number of occurence and the letter
  70. cout << letterCount[i]<<"\t" <<letter[i] << endl;
  71. } //end if
  72. }
  73. //Display the occurence of this, the and is
  74. cout<<"there's "<<thisCount<<" this"<<endl;
  75. cout<<"there's "<<theCount<<" the"<<endl;
  76. cout<<"there's "<<isCount<<" is"<<endl;
  77. }

the number it came up is worng and it doesn't count "this" "the " and "is"
Please help me!
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Kanvas is offline Offline
9 posts
since Mar 2006
Mar 15th, 2006
0

Re: trouble with counting letter from file, please help.

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).
Reputation Points: 718
Solved Threads: 373
Nearly a Posting Maven
Lerner is offline Offline
2,253 posts
since Jul 2005
Mar 16th, 2006
0

Re: trouble with counting letter from file, please help.

i did what you said and now this is my code:
C++ Syntax (Toggle Plain Text)
  1. #include <fstream>
  2. #include <iostream>
  3. using namespace std;
  4. /////////////////////////////////////////////////////////////////////////////
  5. ////////////////////////////////////////////////////////////////////////////
  6. // This program read the file called "poems.dat' and display the occurence
  7. //of a letter in the file. It also display the occurence of three words
  8. //'this', 'the', and 'is'.
  9. //
  10. // This program assumming that the file contains no digit number.
  11. //////////////////////////////////////////////////////////////////////////////
  12. //////////////////////////////////////////////////////////////////////////////
  13. void count(int letterCount[], int &thisCount, int &isCount, int &theCount);
  14. void output(int letterCount[],char letter[], int &thisCount, int &isCount, int &theCount);
  15.  
  16. //////////////////////////////////////////////////////////////////////////
  17. int main() //start main
  18. {
  19. int thisCount=0; //variable counting this
  20. int theCount=0; //variable counting the
  21. int isCount=0; // varibale count is
  22. int letterCount[26]={0}; //interger array for storing the occurency
  23. char letter[27]="abcdefghigklmnopqrstuvwxyz"; //char array for storing the alphabet
  24. count (letterCount, thisCount, isCount, theCount);
  25. output (letterCount, letter, thisCount, isCount, theCount);
  26. cout<<endl;
  27. return 0;
  28. } //end main
  29.  
  30. ////////////////////////////////////////////////////////////////////
  31. void count(int letterCount[], int &thisCount, int &isCount, int &theCount)
  32. {
  33. char word[80]; //array for storing intake words
  34. char c; //variable for comparing letter
  35. ifstream infile;
  36. infile.open("poems.dat"); //open file
  37. while(!infile.eof()) //begin while loop, not end of file
  38. {
  39. infile>>word; //intake word
  40. if (strcmp(word,"this") || strcmp(word,"This")) //count this
  41. thisCount++;
  42. if (strcmp(word,"is") || strcmp(word,"Is")) //count is
  43. isCount++;
  44. if (strcmp(word,"the") || strcmp(word,"The")) //count the
  45. theCount++;
  46.  
  47. for (int i=0; i<strlen(word); i++) //begin for loop
  48. {
  49. c=word[i];
  50. c=tolower(c);
  51. //using ASCII, assumming the file contents no digit number
  52. ++letterCount[c - 'a'];
  53. } //end for loop
  54. }
  55. infile.close(); //close file
  56.  
  57. }
  58.  
  59. /////////////////////////////////////////////////////////////////////////
  60. //Function for displaying the result
  61. ////////////////////////////////////////////////////////////////////////
  62. void output(int letterCount[], char letter[], int &thisCount, int &isCount, int &theCount)
  63. {
  64. cout<<"This is the statistic of the file poems.dat"<<endl;
  65. for (int i = 0; i < 26; i++) //begin for loop;
  66. {
  67. if (letterCount[i] > 0) //begin if
  68. {
  69. //display the number of occurence and the letter
  70. cout << letterCount[i]<<"\t" <<letter[i] << endl;
  71. } //end if
  72. }
  73. //Display the occurence of this, the and is
  74. cout<<"there's "<<thisCount<<" this"<<endl;
  75. cout<<"there's "<<theCount<<" the"<<endl;
  76. cout<<"there's "<<isCount<<" is"<<endl;
  77. }

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Kanvas is offline Offline
9 posts
since Mar 2006
Mar 17th, 2006
0

Re: trouble with counting letter from file, please help.

>>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:

C++ Syntax (Toggle Plain Text)
  1. while(infile >> word)
  2. {
  3. if(strcmp(word, "this") == 0)
  4. //etc.
  5. }

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.
Reputation Points: 718
Solved Threads: 373
Nearly a Posting Maven
Lerner is offline Offline
2,253 posts
since Jul 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Visual C++.NET programming problem
Next Thread in C++ Forum Timeline: Creating dynamic array structures





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC