using cstrings to evaluate a paragraph

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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
  #include <iostream>
#include <cstring>
#include <fstream>
#include <cstdlib>
#include <iomanip>
using namespace std;
const int MAX_WORD_CHARS = 50;         // longest word = 50 chars
const int MAX_WORDS = 1000;	// longest paragraph = 1000 words
const int MAX_PARAGRAPH_CHARS = 50000;         // 50 * 1000
const int MAX_SENTENCES = 100;      // max number of sentences in paragraph
const int MAX_SENTENCE_CHARS = 500;     // max number chars in a sentence;
const int NUM_TO_BE_VERBS = 5;
const char TO_BE_VERBS[NUM_TO_BE_VERBS][MAX_WORD_CHARS] = { "am", "are", "is", "was", "were" };
const char BE[] = "be";
const char 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 );
	}
	else if( 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() )
	{
		return false;
	}
	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?
Last edited on
Topic archived. No new replies allowed.