Finding how many words are in a line

I'm just looking for a few tips here on how to keep my homework assignment from killing me. Not asking for answers here just someway to go in the right direction. I have to read a txt. file in line by line and go through and find how many words are in each line and add them to the total number of words in the file. I have to write a function as well to find the number of words in each line and this is where I'm completely lost. Any step in the right direction would help me out greatly. Thank you very much.

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

using namespace std;




// This function will read in a current line and find out how many words are in it

void wordsInCurrentLine (string line)

{
	string delimeters = " .,?:;!()";
	
	
	
	return;
}








int main ()
{
	ifstream inFile;   // Declares the file that holds all the data
	int countline;         // This is used to keep count
	string currentLine;   // This is the string being read from the file
	int wordCount;
	
	// Open the file
	
	inFile.open("story.txt");
	
	// Check to make sure the file opened correctly
	
	if (!inFile)
	{
		cout << "File did not open correctly" << endl;
		return 1;
	}
	
	// Set the count equal to zero
	
	countline = 0;
	
	// Get the first line of the file
	
	getline(inFile, currentLine);
	
	// Check the number of lines in the file
	
	while (inFile)
	{
		wordsInCurrentLine (currentLine, wordCount);
		
		countline ++;
		
		getline(inFile, currentLine);
	
	}
	

	
	// Display the number of lines in the file
	
	cout << "There are " << countline << " lines in the file" << endl;
	
	return 0;
	
}
Bumping because I could really use some help on this.
Look at this http://www.cplusplus.com/reference/string/string/

Especially this: http://www.cplusplus.com/reference/string/string/find_first_of/

The example there is very close to what you need to do
I talked to my teacher a little more the other day and I think I'm on the right track but I've obviously done something very wrong.

I've got a .txt file with at most 25 words in it but I keep getting a number of words returned in the hundreds of millions. If anyone can please help me decipher what I've done wrong it will be a great help. My character count is off as well.

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

using namespace std; 

// This function finds the number of words and characters in a given line

void wordsInFile (string line, int& wordCount, int& charCount)
{
	
	string delimiters = " .,?:;!()";         // Sets the delimiters of a word
	char currentChar;                        // Keeps track of the current character
	bool inWord = false;                             // Keeps track of whether you're in a word or not
	
	
	
	// Tack a delimiter on at then end of the line to make word ID easier
	
	line = line + '.';
	
	// Initialize the wordCount to zero
	
	wordCount = 0;
	
	// For loop to count the characters in the line
	
	for (charCount == 0; charCount < line.length(); charCount++)
	{
		// Set the current character
		currentChar = line.at (charCount);
		
		// If current character is not a delimiter
		
		if (delimiters.find (currentChar) == string::npos)
		{
			// If we're inside a word
			if (inWord)
				
			// Increment the character count
			
				charCount++;
			
			// If it's the beginning of the word then set inWord to true and increment
			// the character and word counts
			
			else
			{
				inWord = true;
				charCount++;
				wordCount++;
			}
		}
		
		// else the current character is a delimiter
		
		else
		{
			if (inWord)
			{
				// Set inWord to false and increment the character counter
				
				inWord = false;
				charCount++;
			}
			
			else
				charCount++;
		}
	}
	
	return;


}



int main ()
{
	ifstream inFile;        // Declares the file that holds all the data
	int countline;          // This is used to keep count
	string currentLine;     // This is the string being read from the file
	int totalWordCount;     // This is the total word count of the file
	int totalCharCount;     // This is the total character count of the file
	int wordCount;          // A word count of each line
	int charCount;          // A character count of each line
	
	
	// Open the file
	
	inFile.open("story.txt");
	
	// Check to make sure the file opened correctly
	
	if (!inFile)
	{
		cout << "File did not open correctly" << endl;
		return 1;
	}
	
	// Set the count equal to zero
	
	countline = 0;
	
	// Get the first line of the file
	
	getline(inFile, currentLine);
	
	// Check the number of lines in the file
	
	while (inFile)
	{
		
		// Call the function to find words and characters in the file
		
		wordsInFile (currentLine, wordCount, charCount);
		
		// Add the wordCount from the function to the total word count
		
		totalWordCount += wordCount;
		
		// Add the charCount from the function to the total character count
		
		totalCharCount += charCount;
		
		// Increment the line counter
		
		countline ++;
		
		// Get the next line
		
		getline(inFile, currentLine);
	
	}
	

	
	// Display the number of lines in the file
	
	cout << "There are " << countline << " lines in the file" << endl;
	
	// Display the number of words in the file
	
	cout << "There are " << totalWordCount << " words in the file" << endl;
	
	// Display the number of characters in the file
	
	cout << "There are " << totalCharCount << " characters in the file" << endl;
	
	return 0;
	
}
	
Last edited on
Any ideas as to why the last code I posted is wrong?
One obvious things is that you are using charCount as both the loop variable (the char index in the string) and the count of chars (in words??)

As a result, it looks like you are prob incrementing your loop index twice as fast as planned?

Andy
closed account (DGvMDjzh)
I made my self a small function to count the number of words in a string a while ago.

1
2
3
4
5
6
7
8
9
int Words(char* s) {
 int w = 0;
 int l = Lenght(s);
 for(int i = 0; i < l; i++) {
   if ((s[i-1] == ' ' || s[i-1] == NULL) && (s[i] != ' ')) w++;
   if (s[i] == NULL) break;
  }
 return w;
}


The algorithm may look a bit confusing, but it definitely works.

Turns out that I had two things wrong. I didn't initialize totalWordCount in the main function and like andwestken said, I was double counting character in the for loop in the first function. Appreciate the help everyone once again.

This place is awesome btw. Everyone seems to be really nice and helpful to us newbs.
Topic archived. No new replies allowed.