Getting an average word count

closed account (GL1Rko23)
For this program I have to take an input file, and get the average word count.

So far I have the word count correctly and my function removes all special characters so all I have is the words. What I need to do next is find some way of counting up all the characters after the special characters have been removed so I can do the calculation. Any help would be appreciated.

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
/*
 * ch6pro11.cpp
 *
 *  Created on: Mar 10, 2011
 *      Author: Matt
 */
#include<fstream>
#include<iostream>
#include<cstdlib>
#include <sstream>
#include <string>


using namespace std;
void Function(ifstream& fin);
int main ()

{


	std::ifstream inFile;
	inFile.open( "input.dat", std::ios::in );

	if( inFile.is_open() )
	{
	std::string word;
	unsigned long wordCount = 0;

	while( !inFile.eof() )
	{
	inFile >> word;
	if( word.length() > 0 )
	{
	wordCount++;
	}
	}

	std::cout << "The file has " << wordCount << " words in it." << std::endl;
	}



	ifstream fin;


	fin.open("input.dat");

	Function(fin);

fin.close();

return 0;
}


void Function(ifstream& fin){
	string str;

while (!fin.eof()){

	 fin >> str;
	  size_t found;

	  found=str.find_first_of(".,");
	  while (found!=string::npos)
	  {

	    str[found]=' ';
	    found=str.find_first_of(".,");
	  }

	  cout << str <<  endl;

	  






 }}




Last edited on
I think you're doing things in a way that is making it more difficult for you to examine the problem. How about when you read the data into the file you load them into a vector http://www.cplusplus.com/reference/stl/vector/ that way you only have to read them in once instead of this open close open thing that you have here. Before you store them you check to see if it's a special character that way you aren't doubling your work by having to remove them afterward. Then you can add the result of 'word.length()' in every iteration where the input wasn't a symbol to get the number of characters.

EDIT: As it stands you do not need the vector but I thought you'd want to work more with the data later on.
Last edited on
closed account (GL1Rko23)
I was told to use vectors by someone else, but for the sake of this class we are not supposed to use them yet. Any other route I can go? I think I am making it harder then it should be too.
Like I added to the end you don't need a vector, I just thought that you were going to work with the data a little more after you accomplish your current goal. What I outlined above should work just fine.
closed account (GL1Rko23)
Ah okay, sorry to sound stupid, but could you explain a little more what you mean?
You're reading your data in here:
1
2
3
4
5
6
7
8
	while( !inFile.eof() )
	{
	inFile >> word;
	if( word.length() > 0 )
	{
	wordCount++;
	}
                }


But the problem is that's ALL you're doing. Nothing is stopping you from adding more operations to this loop. Like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
	while( !inFile.eof() )
	{
	   inFile >> word;
	   if( word.length() > 0 )
	  {
	     wordCount++;
	   
                   for(int i = 0; i <= word.length(); ++i)
                      {
                         if(isalpha(word[i])
                           {Number_Of_Chars++;} /*Assuming you declared int Number_Of_Chars */
                       }
                   }
                  }
closed account (GL1Rko23)
Ah, okay! Thank you very much for that. I see what you mean now
closed account (GL1Rko23)
When I plug that in I just get a bunch of errors though, and a warning with comparing a signed and unsigned variable.

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
#include<fstream>
#include<iostream>
#include<cstdlib>
#include <sstream>
#include <string>


using namespace std;
void Function(ifstream& fin);
int Number_Of_Chars, i;
int main ()

{


	std::ifstream inFile;
	inFile.open( "input.dat", std::ios::in );

	if( inFile.is_open() )
	{
	std::string word;
	unsigned long wordCount = 0;

	while( !inFile.eof() )
	{
	inFile >> word;
	if( word.length() > 0 )
	{
	wordCount++;


	 for(int i = 0; i <= word.length(); ++i)
	                      {
	                         if(isalpha(word[i])
	                           {Number_Of_Chars++;} /*Assuming you declared int Number_Of_Chars */
	                       }
	                   }


	}


	}


	std::cout << "The file has " << wordCount << " words in it." << Number_Of_Chars << std::endl;
	}



	ifstream fin;


	fin.open("input.dat");

	Function(fin);

fin.close();

return 0;
}


void Function(ifstream& fin){
	string str;

while (!fin.eof()){

	 fin >> str;
	  size_t found;

	  found=str.find_first_of(".,");
	  while (found!=string::npos)
	  {

	    str[found]=' ';
	    found=str.find_first_of(".,");
	  }

	  cout << str <<  endl;


 }}




Topic archived. No new replies allowed.