int main( int argc, char *argv[] )
{
==> This function has over 60 lines.
You need to split it into smaller functions
Everytime I try to split it up I just cause errors (it compiles but locks up when I run it through the test system so I think I'm doing something wrong). If anyone could have a look at it I would appreciate it.
#include <iostream>
#include <string>
#include <fstream>
#include "bintree.h"
using namespace std;
class word
{
public:
word();
int getCount() { return count; }
const char *getWord() const { return sWord.c_str(); }
void setWord( const char* word )
{
sWord = word;
buildPrintString();
}
void setCount( int n )
{
count = n;
buildPrintString();
}
bool operator== (const word& wrd ) const
{
return !sWord.compare( wrd.getWord() );
}
bool operator< (word &wrd ) const
{
return ( sWord.compare( wrd.getWord() ) < 0 );
}
const char *toString() const
{
return printStr.c_str();
}
private:
string sWord;
string printStr;
int count;
void buildPrintString()
{
char count[10];
sprintf( count, "%d", this->count );
printStr = " ";
if ( this->count < 9 )
printStr += " ";
printStr += count;
printStr += " | " + sWord;
}
};
word::word()
{
count = 0;
}
int main( int argc, char *argv[] )
{
if ( argc < 2 )
{
cout<<"ERROR - Incorrect number of arguments"<endl;
cout<<"Syntax : wordcount filename"<<endl<<endl;
return 0;
}
ifstream file( argv[1] );
if ( !file.is_open() )
{
cout<<"ERROR - Unable to access "<<argv[1]<<endl;
return 0;
}
int totalWords = 0, uniqueWords = 0;
bintree<word> tree;
while ( !file.eof() )
{
char buff[256];
file.getline( buff, 226 );
char *newword = strtok( buff, " \t\r\n,;:”~!#%^*()=+[]{}\\|<>?/." );
while ( newword )
{
// strip any word of double quotes
string strWord = newword;
size_t found;
found = strWord.find_first_of("\"");
while ( found != string::npos )
{
strWord.erase( found, 1 );
found = strWord.find_first_of( "\"", found+1 );
}
if ( strWord.length() < 1 )
{
newword = strtok( NULL, " \t\r\n,;:”~!#%^*()=+[]{}\\|<>?/." );
continue;
}
word temp;
temp.setWord( strWord.c_str() );
word *find = tree.find( temp );
// Not in tree, means first occurence of the word
if ( !find )
{
temp.setCount( 1 );
tree.insert( temp );
uniqueWords++;
}
else
{
find->setCount( find->getCount() + 1 );
}
totalWords++;
newword = strtok( NULL, " \t\r\n,;:”~!#%^*()=+[]{}\\|<>?/." );
}
}
cout<<"\nfilename : "<<argv[1]<<endl<<endl;
cout<<"COUNT | WORD"<<endl;
cout<<"------+------"<<endl;
tree.print();
cout<<endl;
cout<<"Number of unique words in "<<argv[1]<<" is "<<uniqueWords<<endl;
cout<<"Total number of words in "<<argv[1]<<" is "<<totalWords<<endl;
return 0;
}
This is my attempt, it compiles but when I go to run it it just locks up and doesn't run and I can't use global variables.
#include <iostream>
#include <string>
#include <fstream>
#include "bintree.h"
using namespace std;
int main(int, char**);
void checkWords();
void printWords();
class word
{
public:
word();
int getCount() { return count; }
const char *getWord() const { return sWord.c_str(); }
void setWord( const char* word )
{
sWord = word;
buildPrintString();
}
void setCount( int n )
{
count = n;
buildPrintString();
}
bool operator== (const word& wrd ) const
{
return !sWord.compare( wrd.getWord() );
}
bool operator< (word &wrd ) const
{
return ( sWord.compare( wrd.getWord() ) < 0 );
}
const char *toString() const
{
return printStr.c_str();
}
private:
string sWord;
string printStr;
int count;
void buildPrintString()
{
char count[10];
sprintf( count, "%d", this->count );
printStr = " ";
if ( this->count < 9 )
printStr += " ";
printStr += count;
printStr += " | " + sWord;
}
};
word::word()
{
count = 0;
}
int main( int argc, char *argv[] )
{
if ( argc < 2 )
{
cout<<"ERROR - Incorrect number of arguments"<<endl;
cout<<"Syntax : wordcount filename"<<endl<<endl;
return 0;
}
ifstream file( argv[1] );
if ( !file.is_open() )
{
cout<<"ERROR - Unable to access "<<argv[1]<<endl;
return 0;
}
checkWords();
return 0;
}
void checkWords()
{
int totalWords = 0, uniqueWords = 0;
char *argv[1];
bintree<word> tree;
ifstream file( argv[1] );
while ( !file.eof() )
{
char buff[256];
file.getline( buff, 226 );
char *newword = strtok( buff, " \t\r\n,;:”~!#%^*()=+[]{}\\|<>?/." );
while ( newword )
{
// strip any word of double quotes
string strWord = newword;
size_t found;
found = strWord.find_first_of("\"");
while ( found != string::npos )
{
strWord.erase( found, 1 );
found = strWord.find_first_of( "\"", found+1 );
}
if ( strWord.length() < 1 )
{
newword = strtok( NULL, " \t\r\n,;:”~!#%^*()=+[]{}\\|<>?/." );
continue;
}
word temp;
temp.setWord( strWord.c_str() );
word *find = tree.find( temp );
// Not in tree, means first occurence of the word
if ( !find )
{
temp.setCount( 1 );
tree.insert( temp );
uniqueWords++;
}
else
{
find->setCount( find->getCount() + 1 );
}
totalWords++;
newword = strtok( NULL, " \t\r\n,;:”~!#%^*()=+[]{}\\|<>?/." );
}
}
printWords();
}
void printWords()
{
int totalWords;
int uniqueWords;
char *argv[1];
bintree<word> tree;
cout<<endl<<"filename : "<<argv[1]<<endl<<endl;
cout<<"COUNT | WORD"<<endl;
cout<<"------+------"<<endl;
tree.print();
cout<<endl;
cout<<"Number of unique words in "<<argv[1]<<" is "<<uniqueWords<<endl;
cout<<"Total number of words in "<<argv[1]<<" is "<<totalWords<<endl;
}