473,382 Members | 1,261 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,382 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 7748
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.