Getting a 1 for word count from a string

Trying to count the words in a string and I seem to get a zero or a one. I have seen fixes in these forums but for some reason I can't seem to apply them correctly to my code.

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


using namespace std;


int main() {


	cout << "I will count the names in test2.txt. By first putting them in an array, then printing amont. \n";
	 ifstream instream;

	char words[100]; // name of the array

	instream.open("test2.txt");
	 if (instream.fail()) {
		 cout << "Faild to open file! \n";
		 exit(1);
	 }

	 int cnt = 0;  // this will be a counter into the array

	while (instream >> words[cnt]) {
		//  while loop for what it is pulling from the file

		cnt++;
	}
int numberofwords = 0;

for (int i = 0; i < cnt; i++) {
    if ((!isspace(words[i])) && (isspace(words[i+1])) || (words[i+1] == '\0')){
    numberofwords++;
    }
}

    cout << numberofwords << '\n';  // <<< this seems to print 1 everytime.
	cout << cnt << '\n';  // prints out how many times it read from the file


	return(0);
}
show me the .txt you are getting the data from.
Actually, I changed your code so that is actually counts how many strings there are in the text instead of chars. It works no matter how many spaces there are or how many line there are.

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


using namespace std;


int main() {


	cout << "I will count the names in test2.txt. \nBy first putting them in an array, then printing amont. \n\n";
	 ifstream instream;

	string words[100]; // name of the array

	instream.open("test2.txt");
	 if (instream.fail()) {
		 cout << "Faild to open file! \n";
		 exit(1);
	 }

	 int cnt = 0;  // this will be a counter into the array

	while (instream >> words[cnt]) {
		//  while loop for what it is pulling from the file

		cnt++;
	}
int numberofwords = 0;

for (int i = 0; i < cnt; i++) {
    if(words[i] != " ") //actually count the words in there.
         numberofwords++;
}

    cout << "Number of Words : " << numberofwords << '\n';  // <<< this seems to print 1 everytime.
	cout << "Number Times read from file : " <<  cnt << '\n';  // prints out how many times it read from the file


	return(0);
}


test2.txt

1
2
my             hey             name is 
bob


Output

1
2
3
4
5
6
7
I will count the names in test2.txt. By first putting them in an array, then pri
nting amont.
5
5

Process returned 0 (0x0)   execution time : 0.136 s
Press any key to continue.
Last edited on
If you need to know only the number of words in a file then you can write a simple program

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <fstream>
#include <string>
#include <iterator>

int main()
{
	std::ifstream in( "test2.txt" );
	size_t count = 0;

	if ( in )
	{
		count = std::distance( std::istream_iterator<std::string>( in ),
			               std::istream_iterator<std::string>() );
	}

	std::cout << "There are " << count << " words in the file" << std::endl;
}


As for your program then starting from this loop

1
2
3
4
5
	while (instream >> words[cnt]) {
		//  while loop for what it is pulling from the file

		cnt++;
	}


your code is invalid because operator >> reads characters that are separated by white spaces. For example if a text is started from word "Word" then the first read will finish with an error because after letter 'W' there is no a white space.
Last edited on
I had tried the
 
if (words[i] != " ")


Before but kept getting an "ISO C++ forbids comparison between pointer and integer" error. After looking at what you did a little closer I relied I had char words[100] not string words[100] I must have been so set on "knowing" where my mistake was that I was over looking it.

Thank you so very much.

Is it the != " " that makes it count words out and not the whole string?
Basically what that line says is, if there is not a word there, dont put it in the array.
Topic archived. No new replies allowed.