473,407 Members | 2,359 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

word count problem

can anyone debug my program and get it to run.

Expand|Select|Wrap|Line Numbers
  1. #include <fstream>  
  2. #include <iostream> 
  3. #include <string>   
  4. #include <cstdlib>
  5. #include <map> 
  6.  
  7. using namespace std; 
  8.  
  9.  
  10.  
  11. string getInputFileName(); // a function to prompt for the complete file name
  12.  
  13. int numCharsInFile( ifstream &in, int &numLines ); // a function to count the
  14.                                                    //    number of characters and
  15.                                                    //    lines in a text file
  16.  
  17. int numWordsInFile( ifstream &in, int &numWords ); // a function to count words in file
  18.  
  19. string get_tokenInFile( ifstream &in, int &numWords ); // a function to count frequency of words in file
  20.  
  21.  
  22.  
  23.  main ()
  24. {
  25.  
  26.  
  27.     char c;
  28.   int nLines,          // number of lines in the text file
  29.       nChars,          // number of characters in the text file
  30.       avgCharsPerLine, // average number of characters per line
  31.       nWords;
  32.   string nWords1;          // number of words in the text file
  33.  
  34.  
  35.  
  36.   ifstream inFile; // handle for the input text file
  37.  
  38.   string fileName; // complete file name including the path
  39.  
  40.   fileName = getInputFileName(); // prompt and obtain the full file name
  41.  
  42.   inFile.open(fileName.c_str()); // try to open the file
  43.  
  44.   if( !inFile.is_open() )    // test for unsuccessfull file opening
  45.    {
  46.      cerr << "Cannot open file: " << fileName << endl << endl;
  47.      exit (0);
  48.    }
  49.  
  50.  
  51.   nChars = numCharsInFile( inFile, nLines ); // determine the number of lines
  52.                                             //    and characters in the file
  53.   nWords = numWordsInFile( inFile, nWords ); // determine the number of words
  54.  
  55.   nWords1 = get_tokenInFile( inFile, nWords1 ); // determine the frequency of characters
  56.  
  57.   avgCharsPerLine = nChars / nLines;
  58.  
  59.  
  60.  
  61.  
  62.   cout << "The number of characters in the file: " << fileName
  63.        << " is = " << nChars << endl << endl;
  64.  
  65.   cout << "The number of lines in the file: " << fileName
  66.        << " is = " << nLines << endl << endl;
  67.  
  68.  
  69.   cout << "The number of Words in the file: " << fileName
  70.        << " is = " << nWords << endl << endl;
  71.  
  72.   cout << "The average number of characters per line in the text file: "
  73.        << fileName << " is: " << avgCharsPerLine << endl << endl;
  74.  
  75.   cout << "The frequency of Words in the file: " << fileName
  76.        << " is = " << nWords1 << endl << endl;
  77.  
  78.     cin>>c;
  79.   inFile.close(); // close the input file
  80.  
  81. }
  82.  
  83.  
  84.  
  85. string getInputFileName()
  86.  {
  87.    string fName; // fully qualified name of the file
  88.  
  89.    cout << "Please enter the fully qualified name of the " << endl
  90.         << "input text file (i.e. including the path): ";
  91.    cin >> fName; // cannot handle blanks in a file name or path
  92.    cout << endl; 
  93.  
  94.    return fName;
  95.  }
  96.  
  97.  
  98.  
  99.  
  100.  
  101. int numCharsInFile( ifstream &in, int &numLines )
  102.  {
  103.    int numChars = 0; 
  104.  
  105.    char ch; // character holder;
  106.  
  107.    numLines = 0; // initialize the number of lines to zero
  108.  
  109.    while ( in.get(ch) ) // get the next character from the file
  110.                         //   the function get will also get whitespace
  111.                         //   i.e. blanks, tabs and end of line characters
  112.  
  113.     {
  114.      if (ch != ' ' )
  115.      {
  116.        if(ch != '\n')
  117.        numChars++;// increase the count of characters by one if ch is NOT '\n' AND NOT a blank space
  118.        else
  119.        {
  120.        numLines++;     // increase the count of lines by one if ch IS '\n'
  121.        }
  122.      } 
  123.     }
  124.     numLines += 1; // for some reason it needs to add one and the results are correct
  125.    return numChars; // return the number of characters in the file
  126.  }
  127.  
  128.  
  129.  
  130.  
  131. int numWordsInFile( ifstream &in, int &nWords)
  132.  {
  133.     in.clear();
  134.  
  135.     in.seekg(0, ios_base::beg); // IS THIS CORRECT UBERGEEK?
  136.  
  137.     int numWords = 0 ; 
  138.  
  139.    char ch; 
  140.  
  141.  
  142.    while (in.get(ch)) 
  143.    {      
  144.  
  145.     if ( ch == ' ' || ch == '\n' || ch == '\t' ) 
  146.        numWords++;    
  147.  
  148.  
  149.     }
  150.  
  151.    return numWords+1; // for some reason again it needs to add one to work properly
  152.  }  
  153.  
  154.   string get_token( ifstream &in, string nWords1)
  155. {
  156.     string token;
  157.  
  158.     char ch;
  159.  
  160.     while (in.get(ch))
  161.     {
  162.         char c;
  163.         in.get(ch)>> c;
  164.         if ( isalpha( c ) || c == '\'' )
  165.             token += c;
  166.         else if ( token.size() )
  167.         return token;
  168.  
  169.  
  170.  
  171.     }
  172.     return token;
  173. }
  174. string token = get_token( infile );
  175.     if ( token.size() == 0 )
  176.         break;
  177.     map<string, int>::const_iterator ii = frequency.find( token );
  178.     if ( ii == frequency.end() )
  179.         frequency[ token ] = 1;
  180.     else
  181.         frequency[ token ]++;
  182.  
  183.     multimap<int, string> counts;
  184.     for ( map<string, int>::iterator ii = frequency.begin() ; 
  185.           ii != frequency.end();
  186.           ii++ )
  187.       counts.insert( pair<int,string>( (*ii).second, (*ii).first ) )
  188.  
  189. set<string> used_codes;
  190. for ( multimap<int, string>::reverse_iterator jj = counts.rbegin() ; 
  191.       jj != counts.rend() ; 
  192.       jj++ )
  193. {
  194.   string code = create_star_code( (*jj).second, used_codes );
  195.   used_codes.insert( code );
  196.   codes[ (*jj).second ] = code;
  197.   cout <<(*jj).second <<" " <<code <<" " <<(*jj).first <<endl;
  198. }
Nov 7 '07 #1
3 7749
scruggsy
147 100+
Help us help you.
Put your code in [ code ] tags and describe what problem you are having with this program.
Nov 7 '07 #2
Help us help you.
Put your code in [ code ] tags and describe what problem you are having with this program.
The code works but my last function to count the frequency of each word in a txt file is not working can you please help me i need this program to graduate.
Nov 8 '07 #3
scruggsy
147 100+
Seriously:

Help us help you.
Put your code in [ code ] tags and describe what problem you are having with this
program.
What, specifically, is happening when you try to run and/or compile this program? Compiler errors? Incorrect results?

Look at your code in the [ code] [/code ] tags below (much easier to read that way, isn't it?)
Expand|Select|Wrap|Line Numbers
  1. #include <fstream>  
  2. #include <iostream> 
  3. #include <string>   
  4. #include <cstdlib>
  5. #include <map> 
  6.  
  7. using namespace std; 
  8.  
  9.  
  10.  
  11. string getInputFileName(); // a function to prompt for the complete file name
  12.  
  13. int numCharsInFile( ifstream &in, int &numLines ); // a function to count the
  14.                                                    //    number of characters and
  15.                                                    //    lines in a text file
  16.  
  17. int numWordsInFile( ifstream &in, int &numWords ); // a function to count words in file
  18.  
  19. string get_tokenInFile( ifstream &in, int &numWords ); // a function to count frequency of words in file but this function isn't working
  20.  
  21.  
  22.  
  23.  main ()
  24. {
  25.  
  26.  
  27.     char c;
  28.   int nLines,          // number of lines in the text file
  29.       nChars,          // number of characters in the text file
  30.       avgCharsPerLine, // average number of characters per line
  31.       nWords;
  32.   string nWords1;          // number of words in the text file
  33.  
  34.  
  35.  
  36.   ifstream inFile; // handle for the input text file
  37.  
  38.   string fileName; // complete file name including the path
  39.  
  40.   fileName = getInputFileName(); // prompt and obtain the full file name
  41.  
  42.   inFile.open(fileName.c_str()); // try to open the file
  43.  
  44.   if( !inFile.is_open() )    // test for unsuccessfull file opening
  45.    {
  46.      cerr << "Cannot open file: " << fileName << endl << endl;
  47.      exit (0);
  48.    }
  49.  
  50.  
  51.   nChars = numCharsInFile( inFile, nLines ); // determine the number of lines
  52.                                             //    and characters in the file
  53.   nWords = numWordsInFile( inFile, nWords ); // determine the number of words
  54.  
  55.   nWords1 = get_tokenInFile( inFile, nWords1 ); // determine the frequency of characters
  56.  
  57.   avgCharsPerLine = nChars / nLines;
  58.  
  59.  
  60.  
  61.  
  62.   cout << "The number of characters in the file: " << fileName
  63.        << " is = " << nChars << endl << endl;
  64.  
  65.   cout << "The number of lines in the file: " << fileName
  66.        << " is = " << nLines << endl << endl;
  67.  
  68.  
  69.   cout << "The number of Words in the file: " << fileName
  70.        << " is = " << nWords << endl << endl;
  71.  
  72.   cout << "The average number of characters per line in the text file: "
  73.        << fileName << " is: " << avgCharsPerLine << endl << endl;
  74.  
  75.   cout << "The frequency of Words in the file: " << fileName
  76.        << " is = " << nWords1 << endl << endl;
  77.  
  78.     cin>>c;
  79.   inFile.close(); // close the input file
  80.  
  81. }
  82.  
  83.  
  84.  
  85. string getInputFileName()
  86.  {
  87.    string fName; // fully qualified name of the file
  88.  
  89.    cout << "Please enter the fully qualified name of the " << endl
  90.         << "input text file (i.e. including the path): ";
  91.    cin >> fName; // cannot handle blanks in a file name or path
  92.    cout << endl; 
  93.  
  94.    return fName;
  95.  }
  96.  
  97.  
  98.  
  99.  
  100.  
  101. int numCharsInFile( ifstream &in, int &numLines )
  102.  {
  103.    int numChars = 0; 
  104.  
  105.    char ch; // character holder;
  106.  
  107.    numLines = 0; // initialize the number of lines to zero
  108.  
  109.    while ( in.get(ch) ) // get the next character from the file
  110.                         //   the function get will also get whitespace
  111.                         //   i.e. blanks, tabs and end of line characters
  112.  
  113.     {
  114.      if (ch != ' ' )
  115.      {
  116.        if(ch != '\n')
  117.        numChars++;// increase the count of characters by one if ch is NOT '\n' AND NOT a blank space
  118.        else
  119.        {
  120.        numLines++;     // increase the count of lines by one if ch IS '\n'
  121.        }
  122.      } 
  123.     }
  124.     numLines += 1; // for some reason it needs to add one and the results are correct
  125.    return numChars; // return the number of characters in the file
  126.  }
  127.  
  128.  
  129.  
  130.  
  131. int numWordsInFile( ifstream &in, int &nWords)
  132.  {
  133.     in.clear();
  134.  
  135.     in.seekg(0, ios_base::beg); // IS THIS CORRECT UBERGEEK?
  136.  
  137.     int numWords = 0 ; 
  138.  
  139.    char ch; 
  140.  
  141.  
  142.    while (in.get(ch)) 
  143.    {      
  144.  
  145.     if ( ch == ' ' || ch == '\n' || ch == '\t' ) 
  146.        numWords++;    
  147.  
  148.  
  149.     }
  150.  
  151.    return numWords+1; // for some reason again it needs to add one to work properly
  152.  }  
  153.  
  154.   string get_token( ifstream &in, string nWords1) //this is where i'm having the problems
  155. {
  156.     string token;
  157.  
  158.     char ch;
  159.  
  160.     while (in.get(ch))
  161.     {
  162.         char c;
  163.         in.get(ch)>> c;
  164.         if ( isalpha( c ) || c == '\'' )
  165.             token += c;
  166.         else if ( token.size() )
  167.         return token;
  168.  
  169.  
  170.  
  171.     }
  172.     return token;
  173. }
  174. string token = get_token( infile );
  175.     if ( token.size() == 0 )
  176.         break;
  177.     map<string, int>::const_iterator ii = frequency.find( token );
  178.     if ( ii == frequency.end() )
  179.         frequency[ token ] = 1;
  180.     else
  181.         frequency[ token ]++;
  182.  
  183.     multimap<int, string> counts;
  184.     for ( map<string, int>::iterator ii = frequency.begin() ; 
  185.           ii != frequency.end();
  186.           ii++ )
  187.       counts.insert( pair<int,string>( (*ii).second, (*ii).first ) )
  188.  
  189. set<string> used_codes;
  190. for ( multimap<int, string>::reverse_iterator jj = counts.rbegin() ; 
  191.       jj != counts.rend() ; 
  192.       jj++ )
  193. {
  194.   string code = create_star_code( (*jj).second, used_codes );
  195.   used_codes.insert( code );
  196.   codes[ (*jj).second ] = code;
  197.   cout <<(*jj).second <<" " <<code <<" " <<(*jj).first <<endl;
  198. }
get_token() ends at the closing brace immediately following return token. All the code beyond that brace is outside of any function. Surely that's not what you want?
Nov 8 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: The Roys | last post by:
Hi Im doing something wrong in quitting the Word.Application in my VB program. I have General Declarations Dim AppWord As Word.Application Form_Load() Set AppWord =...
5
by: jester.dev | last post by:
Hello, I'm learning Python from Python Bible, and having some problems with this code below. When I run it, I get nothing. It should open the file poem.txt (which exists in the current...
3
by: agent mike | last post by:
I am trying to count words in a text file. I am using the following code: in_stream.get(c); if(c == ' ' || c == '.' || c == ',') word_count++; and the word count is too low. If I include "...
5
by: STeve | last post by:
Hey guys, I currently have a 100 page word document filled with various "articles". These articles are delimited by the Style of the text (IE. Heading 1 for the various titles) These articles...
2
by: Mikey | last post by:
Sample VB .NET source code to create mailing labels or customized letters using MS Word MailMerge This VB .NET source code will start MS Word and call methods and set properties in MS Word to...
1
by: Adam Faulkner via DotNetMonster.com | last post by:
I had a problem before extracting pages from an existing word document and then inserting the content into a new word document. The following code below works with Microsoft Word 2000 Function...
1
by: vmoreau | last post by:
I have a text and I need to find a Word that are not enclosed in paranthesis. Can it be done with a regex? Is someone could help me? I am not familar with regex... Example looking for WORD:...
4
by: etuncer | last post by:
Hello All, I have Access 2003, and am trying to build a database for my small company. I want to be able to create a word document based on the data entered through a form. the real question is...
2
by: beanie | last post by:
i am a beginer in c programming and i am trying to Create a Concordance of Word Count for a Text File but my code is not working.pls can anyone helpme out.here is my code: #include <stdio.h>...
6
by: boyindie86 | last post by:
Hi I have been fighting with this lump of code for the last week what I am trying to do is that I am passing words into passages of texts, and I want the system to go and find exact word matches...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.