I am trying to write code for a program that gets a paragraph from a input file then uses cstrings to calculate the number of characters in the paragraph, number of words, number of sentences, average words in a sentence and number of to be verbs. This is my code so far:
#include <iostream>
#include <cstring>
#include <fstream>
#include <cstdlib>
#include <iomanip>
usingnamespace std;
constint MAX_WORD_CHARS = 50; // longest word = 50 chars
constint MAX_WORDS = 1000; // longest paragraph = 1000 words
constint MAX_PARAGRAPH_CHARS = 50000; // 50 * 1000
constint MAX_SENTENCES = 100; // max number of sentences in paragraph
constint MAX_SENTENCE_CHARS = 500; // max number chars in a sentence;
constint NUM_TO_BE_VERBS = 5;
constchar TO_BE_VERBS[NUM_TO_BE_VERBS][MAX_WORD_CHARS] = { "am", "are", "is", "was", "were" };
constchar BE[] = "be";
constchar TO[] = "to";
bool openFile( ifstream& );
int tokenizeParagraph( char p[], char tp[][MAX_WORD_CHARS] );
int main ()
{
char input_file; // name of the input file
char paragraph[ MAX_PARAGRAPH_CHARS ];
char tParagraph[ MAX_WORDS ][ MAX_WORD_CHARS ];
int numWords; // number of words in paragraph
int numCharacters; // number of characters in paragraph
int numSentences; // number of sentences in paragraph
double avgWords; // average words in a sentence
int numVerbs; // number of to be verbs in paragraphs
ifstream input;
if( openFile( input ) )
{
numWords = tokenizeParagraph( paragraph, tParagraph );
}
elseif( input.fail() )
{
cout << “Input file “ << input_file << “does not exist.\n”;
}
calcCharacters( tParagraph );
return( 0 );
}
/* openFile( ifstream& ): opens input file and reads info from it
Parameters:
input: ifstream object( input file )
Returns: true if file is opened successfully
*/
bool openFile( ifstream& input )
{
char input_file[80]; // name of the input file
char paragraph[] // paragraph read from file
int attempts = 0; // number of tries to open file
int i = 0;
ifstream input;
// prompt for input file and attempt to open
cout << “Please enter the name of the input file.\n”
cin.getline( input_file, 80 );
input.open( input_file );
attempts++;
while( input.fail() && attempts < 3 )
{
cout << “Could not open file. Please enter the name of the input file.\n”;
cin.getline( input_file, 80 );
input.open( input_file );
attempts++;
}
if( input.fail() )
{
returnfalse;
}
while( i < MAX_PARAGRAPH_CHARS && !input.eof() )
{
input.get( paragraph[i] );
}
i++;
/* tokenizeParagraph: takes an array of sentences and splits them
up by white space.
Pre-condition:
p has a valid paragraph in it and tp has been allocated space
Post-condition:
tp has been filled with all words/punctuation
Parameters:
p: the array holding the paragraph
tp: the "tokenized" paragraph. A 2D array of words with punctuation.
Returns: the number of words in paragraph
*/
int tokenizeParagraph( char [], char tp[][MAX_WORD_CHARS] )
{
int i = 0;
char* cPtr;
cPtr = strtok( p, " \n\t");
while( cPtr != NULL )
{
strcpy( tp[i], cPtr );
i++;
cPtr= strtok( NULL, " \n\t");
}
return( i );
}
I have 2 functions right now. one puts the paragraph into an array and the other one splits the sentences in the paragraph by white space and returns the number of words in the paragraph. I need help on what to do next. any ideas?