Need expert to review C++ code for word count

Mar 16, 2009 at 4:17am
Hi there! I am new to C++ and I will be straight forward and say that this is an assignment and I want someone to review my progress to see if I am headed in the right direction!!!
The assignment is as follows: (below the assignment, I will post my code)

Thanks to anyone for any help or pointers!!!

Phase 1) Arrays of characters



· Read in a paragraph (or more) from the user (hint: read one word at a time)

· As you read, count the number of times they use “but”, “and”

· Count the number of times words end in ‘;’

· Count the number of words.

· Do not echo the paragraph!

· Stop reading when the user has entered a # sign as the first character on a line

· Output the results to the user



Phase 2) External Data Files

· Once you are comfortable with Phase 1, add the ability to write the paragraph to an external data file

· The filename should be read in from the user (prompt/read/echo/confirm)

· Near the end of the program, Phase 2 should add the ability to read the text back into the program. This time you are interested in counting the number of sentences (ending in period only). (hint: read using the 3 argument version of cin.get)

· Output the paper back to the user as you read. Make sure as you do this to capitalize the first letter of each sentence.





Important things to keep in mind



1. Write at least three functions with arguments for this assignment.

2. Allow the user to enter in the name of the external data file.

3. Watch out. When using the 3 argument version of cin.get you need to make sure to remove all delimiter (.) from the input buffer.

4. You should use the cstring library strcmp, strcpy, and strlen functions.

5. The program should experiment with BOTH cin >> array as well as cin.get( 3 argument);


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
#include <iostream>
#include <cstring> 

using namespace std;

int main() {

    char *allwords[1000]; // array of POINTERS to char strings
    char word[500];       // input buffer for longest possible word.
    int n = 0;            
    int wordcount;
    int butcount;
    int andcount;
    int semicoloncount;
    int periodcount;

  // Prompt for sentence and return number of words entered
do
{
cout << " Please enter a sentence: ";
cin >> (str, 30);
cin.ignore();
++wordcount;
But (str, butcount);
cout << "back from But" << endl;
And (str, andcount);
cout << "back from And" << endl;
Semicolon (str, semicoloncount);
cout << "back from Semicolon" << endl;
Period (str, periodcount);
cout << " back from Period" << endl;
}
while (str[0] != '#');   

Mar 16, 2009 at 1:18pm
1. You need to read a word at a time.
2. Store words as strings
3. Store collections of word in an STL container.
4. The above use of But, And, Semicolon and Period doesn't look quite right.
5. You haven't declared str, but used it quite a bit.
Mar 17, 2009 at 6:13am
Well, I made an attempt to write more code, but now it only reads from an output file. I think I am getting closer now. Thanks for the reply kbw, does this code look a little more promising?
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#include <iostream>
#include <fstream>
#include <cctype>
#include <cstring>

#define DELIMS " .\n"

using namespace std;

int main(int argc, char** argv)

{

char word[100][16]; //Stores Words
char line[101]; //Temporary stores lines
int count[100]; //Counts the no times word is repeated
char butword[16]; //Stores but
char andword[16]; //Stores and
char semicolonword[16];//Stores semicolon
int wbut = 0;
int wand=0;
int wsemi=0;
char *p;
int i=0;
int totalnosofwords=0; //Count of total no of words
int noofuniq=0; //Count total no of unique words
int nosoflines=0; //Counts no of lines

ifstream fin; // fin is like cin.
fin.open(txt.txt); // opens the file

if(!fin) //Checking if input file is Valid

{
cout << "While opening a file an error is encountered" << endl;

exit(1);

}

ofstream fout; // fin is like cin.

fout.open(output.txt); // opens the file

if(!fout) //Checks if output file is valid

{
cout << "While opening a file an error is encountered" << endl;

exit(1);

}
while(!fin.eof()) //Reads the input file till the end

{
fin.getline(line, 101); //Reads one line at a time from input file
fout<<line<<endl; //Writes one line to the output file
nosoflines++; //Count the number of lines

vector<string> words; // an array of words
while( fin.getline(line, 100) )
{
p = strtok(line, DELIMS );
while( !p )
{
words.push_back(p);
p = strtok(NULL, DELIMS);
}
}

}

fin.close(); //Close input file

totalnosofwords=i; //Get the total no of words

noofuniq=i; //Get the Max no of words

for(int j=0; j<i; j++)//Check for repeated words, but , and semicolon

{

if(count[j]>0) //Check is that word has been previously checked

{

if(strlen(word[j])>butword) // Comapres but to previous word

{

strcpy(wbut,word[j]); //If it is but it in word[j]

butword=strlen(word[j]); //Get the length of but word

}

if(strlen(word[j])<andword)//Compares and to previous word

{

strcpy(wand,word[j]); //If it is shortest store it in word[j]
andword=strlen(word[j]); //Get the length of and word

}

strcpy(wsemi,word[j]); //If it is semicolon in word[j]
semicolonword=strlen(word[j]); //Get the length of and word

}

for(int z=j+1; z<i; z++)//Check if the word is repeated

{

if((strcmp(word[j],word[z]))==0)//Compar... word[j] with other words

{

count[j]++; //Increase the count if it is found

noofuniq--; //Decrease the count of unique words

}

}

}

}



fout<<endl<<"Statistics:"<<endl; //Print the staistics to output file

fout<<"Total no of Words: "<<totalnosofwords<<endl;

fout<<"No of Unique Words: "<<noofuniq<<endl;

fout<<"No of Lines: "<<nosoflines<<endl;

fout<<"No of Buts: "<<wbut<<endl;

fout<<"No of Ands: "<<wands<<endl;

fout<<"No of Semicolons: "<<wsemi<<endl;

for (i=0;i<totalnosofwords ;i++ )

{

if(count[i]>0)

{

fout<<word[i]<<" : "<<count[i]<<endl;

}

}

fout.close(); //Close the output file

return 0;

}
Mar 17, 2009 at 10:35am
You have lots to do and I think you've gone down the wrong path.

The vector of words is declared in the loop that reads the file, and when all that ends, the vector is erased. It should be declared at the top.

And at the top, you still have arrays that aren't write. word is being being replaced by words. You've used count without initialising it.

I think you're stuggling with the language at bit.
Topic archived. No new replies allowed.