can anyone debug my program and get it to run. - #include <fstream>
-
#include <iostream>
-
#include <string>
-
#include <cstdlib>
-
#include <map>
-
-
using namespace std;
-
-
-
-
string getInputFileName(); // a function to prompt for the complete file name
-
-
int numCharsInFile( ifstream &in, int &numLines ); // a function to count the
-
// number of characters and
-
// lines in a text file
-
-
int numWordsInFile( ifstream &in, int &numWords ); // a function to count words in file
-
-
string get_tokenInFile( ifstream &in, int &numWords ); // a function to count frequency of words in file
-
-
-
-
main ()
-
{
-
-
-
char c;
-
int nLines, // number of lines in the text file
-
nChars, // number of characters in the text file
-
avgCharsPerLine, // average number of characters per line
-
nWords;
-
string nWords1; // number of words in the text file
-
-
-
-
ifstream inFile; // handle for the input text file
-
-
string fileName; // complete file name including the path
-
-
fileName = getInputFileName(); // prompt and obtain the full file name
-
-
inFile.open(fileName.c_str()); // try to open the file
-
-
if( !inFile.is_open() ) // test for unsuccessfull file opening
-
{
-
cerr << "Cannot open file: " << fileName << endl << endl;
-
exit (0);
-
}
-
-
-
nChars = numCharsInFile( inFile, nLines ); // determine the number of lines
-
// and characters in the file
-
nWords = numWordsInFile( inFile, nWords ); // determine the number of words
-
-
nWords1 = get_tokenInFile( inFile, nWords1 ); // determine the frequency of characters
-
-
avgCharsPerLine = nChars / nLines;
-
-
-
-
-
cout << "The number of characters in the file: " << fileName
-
<< " is = " << nChars << endl << endl;
-
-
cout << "The number of lines in the file: " << fileName
-
<< " is = " << nLines << endl << endl;
-
-
-
cout << "The number of Words in the file: " << fileName
-
<< " is = " << nWords << endl << endl;
-
-
cout << "The average number of characters per line in the text file: "
-
<< fileName << " is: " << avgCharsPerLine << endl << endl;
-
-
cout << "The frequency of Words in the file: " << fileName
-
<< " is = " << nWords1 << endl << endl;
-
-
cin>>c;
-
inFile.close(); // close the input file
-
-
}
-
-
-
-
string getInputFileName()
-
{
-
string fName; // fully qualified name of the file
-
-
cout << "Please enter the fully qualified name of the " << endl
-
<< "input text file (i.e. including the path): ";
-
cin >> fName; // cannot handle blanks in a file name or path
-
cout << endl;
-
-
return fName;
-
}
-
-
-
-
-
-
int numCharsInFile( ifstream &in, int &numLines )
-
{
-
int numChars = 0;
-
-
char ch; // character holder;
-
-
numLines = 0; // initialize the number of lines to zero
-
-
while ( in.get(ch) ) // get the next character from the file
-
// the function get will also get whitespace
-
// i.e. blanks, tabs and end of line characters
-
-
{
-
if (ch != ' ' )
-
{
-
if(ch != '\n')
-
numChars++;// increase the count of characters by one if ch is NOT '\n' AND NOT a blank space
-
else
-
{
-
numLines++; // increase the count of lines by one if ch IS '\n'
-
}
-
}
-
}
-
numLines += 1; // for some reason it needs to add one and the results are correct
-
return numChars; // return the number of characters in the file
-
}
-
-
-
-
-
int numWordsInFile( ifstream &in, int &nWords)
-
{
-
in.clear();
-
-
in.seekg(0, ios_base::beg); // IS THIS CORRECT UBERGEEK?
-
-
int numWords = 0 ;
-
-
char ch;
-
-
-
while (in.get(ch))
-
{
-
-
if ( ch == ' ' || ch == '\n' || ch == '\t' )
-
numWords++;
-
-
-
}
-
-
return numWords+1; // for some reason again it needs to add one to work properly
-
}
-
-
string get_token( ifstream &in, string nWords1)
-
{
-
string token;
-
-
char ch;
-
-
while (in.get(ch))
-
{
-
char c;
-
in.get(ch)>> c;
-
if ( isalpha( c ) || c == '\'' )
-
token += c;
-
else if ( token.size() )
-
return token;
-
-
-
-
}
-
return token;
-
}
-
string token = get_token( infile );
-
if ( token.size() == 0 )
-
break;
-
map<string, int>::const_iterator ii = frequency.find( token );
-
if ( ii == frequency.end() )
-
frequency[ token ] = 1;
-
else
-
frequency[ token ]++;
-
-
multimap<int, string> counts;
-
for ( map<string, int>::iterator ii = frequency.begin() ;
-
ii != frequency.end();
-
ii++ )
-
counts.insert( pair<int,string>( (*ii).second, (*ii).first ) )
-
-
set<string> used_codes;
-
for ( multimap<int, string>::reverse_iterator jj = counts.rbegin() ;
-
jj != counts.rend() ;
-
jj++ )
-
{
-
string code = create_star_code( (*jj).second, used_codes );
-
used_codes.insert( code );
-
codes[ (*jj).second ] = code;
-
cout <<(*jj).second <<" " <<code <<" " <<(*jj).first <<endl;
-
}
3 7748
Help us help you.
Put your code in [ code ] tags and describe what problem you are having with this program.
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.
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?) -
#include <fstream>
-
#include <iostream>
-
#include <string>
-
#include <cstdlib>
-
#include <map>
-
-
using namespace std;
-
-
-
-
string getInputFileName(); // a function to prompt for the complete file name
-
-
int numCharsInFile( ifstream &in, int &numLines ); // a function to count the
-
// number of characters and
-
// lines in a text file
-
-
int numWordsInFile( ifstream &in, int &numWords ); // a function to count words in file
-
-
string get_tokenInFile( ifstream &in, int &numWords ); // a function to count frequency of words in file but this function isn't working
-
-
-
-
main ()
-
{
-
-
-
char c;
-
int nLines, // number of lines in the text file
-
nChars, // number of characters in the text file
-
avgCharsPerLine, // average number of characters per line
-
nWords;
-
string nWords1; // number of words in the text file
-
-
-
-
ifstream inFile; // handle for the input text file
-
-
string fileName; // complete file name including the path
-
-
fileName = getInputFileName(); // prompt and obtain the full file name
-
-
inFile.open(fileName.c_str()); // try to open the file
-
-
if( !inFile.is_open() ) // test for unsuccessfull file opening
-
{
-
cerr << "Cannot open file: " << fileName << endl << endl;
-
exit (0);
-
}
-
-
-
nChars = numCharsInFile( inFile, nLines ); // determine the number of lines
-
// and characters in the file
-
nWords = numWordsInFile( inFile, nWords ); // determine the number of words
-
-
nWords1 = get_tokenInFile( inFile, nWords1 ); // determine the frequency of characters
-
-
avgCharsPerLine = nChars / nLines;
-
-
-
-
-
cout << "The number of characters in the file: " << fileName
-
<< " is = " << nChars << endl << endl;
-
-
cout << "The number of lines in the file: " << fileName
-
<< " is = " << nLines << endl << endl;
-
-
-
cout << "The number of Words in the file: " << fileName
-
<< " is = " << nWords << endl << endl;
-
-
cout << "The average number of characters per line in the text file: "
-
<< fileName << " is: " << avgCharsPerLine << endl << endl;
-
-
cout << "The frequency of Words in the file: " << fileName
-
<< " is = " << nWords1 << endl << endl;
-
-
cin>>c;
-
inFile.close(); // close the input file
-
-
}
-
-
-
-
string getInputFileName()
-
{
-
string fName; // fully qualified name of the file
-
-
cout << "Please enter the fully qualified name of the " << endl
-
<< "input text file (i.e. including the path): ";
-
cin >> fName; // cannot handle blanks in a file name or path
-
cout << endl;
-
-
return fName;
-
}
-
-
-
-
-
-
int numCharsInFile( ifstream &in, int &numLines )
-
{
-
int numChars = 0;
-
-
char ch; // character holder;
-
-
numLines = 0; // initialize the number of lines to zero
-
-
while ( in.get(ch) ) // get the next character from the file
-
// the function get will also get whitespace
-
// i.e. blanks, tabs and end of line characters
-
-
{
-
if (ch != ' ' )
-
{
-
if(ch != '\n')
-
numChars++;// increase the count of characters by one if ch is NOT '\n' AND NOT a blank space
-
else
-
{
-
numLines++; // increase the count of lines by one if ch IS '\n'
-
}
-
}
-
}
-
numLines += 1; // for some reason it needs to add one and the results are correct
-
return numChars; // return the number of characters in the file
-
}
-
-
-
-
-
int numWordsInFile( ifstream &in, int &nWords)
-
{
-
in.clear();
-
-
in.seekg(0, ios_base::beg); // IS THIS CORRECT UBERGEEK?
-
-
int numWords = 0 ;
-
-
char ch;
-
-
-
while (in.get(ch))
-
{
-
-
if ( ch == ' ' || ch == '\n' || ch == '\t' )
-
numWords++;
-
-
-
}
-
-
return numWords+1; // for some reason again it needs to add one to work properly
-
}
-
-
string get_token( ifstream &in, string nWords1) //this is where i'm having the problems
-
{
-
string token;
-
-
char ch;
-
-
while (in.get(ch))
-
{
-
char c;
-
in.get(ch)>> c;
-
if ( isalpha( c ) || c == '\'' )
-
token += c;
-
else if ( token.size() )
-
return token;
-
-
-
-
}
-
return token;
-
}
-
string token = get_token( infile );
-
if ( token.size() == 0 )
-
break;
-
map<string, int>::const_iterator ii = frequency.find( token );
-
if ( ii == frequency.end() )
-
frequency[ token ] = 1;
-
else
-
frequency[ token ]++;
-
-
multimap<int, string> counts;
-
for ( map<string, int>::iterator ii = frequency.begin() ;
-
ii != frequency.end();
-
ii++ )
-
counts.insert( pair<int,string>( (*ii).second, (*ii).first ) )
-
-
set<string> used_codes;
-
for ( multimap<int, string>::reverse_iterator jj = counts.rbegin() ;
-
jj != counts.rend() ;
-
jj++ )
-
{
-
string code = create_star_code( (*jj).second, used_codes );
-
used_codes.insert( code );
-
codes[ (*jj).second ] = code;
-
cout <<(*jj).second <<" " <<code <<" " <<(*jj).first <<endl;
-
}
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?
Sign in to post your reply or Sign up for a free account.
Similar topics
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 =...
|
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...
|
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 "...
|
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...
|
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...
|
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...
|
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:...
|
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...
|
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>...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
| |