Counting words in a string array c++

I am trying to write a code that takes words from a text file, reads them, stores them into a string array, and counts the words of the string array. I have been able to count the words from the file and also store them into a string array, but I can't seem to do both. Any idea what I am dong wrong?

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
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main(){
  int getWordCount = 0;
  int finalCount = 0;
  string filename;
  string words;
  string wordsArray[2000];
  ifstream infile;
  
  cout << "Enter filename: ";
  getline(cin,filename);
  
  infile.open(filename.c_str());
  
  if(!infile)
    cerr << "Sorry, filename unavailable." << endl;
  else{

    //this was what I originally wrote to get the word count from file
    //while(infile >> words){
    //  getWordCount++;
    // }

    while(getline(nfile, wordsArray[getWordCount])){
      cout << "Words in array: " << wordsArray[getWordCount] << endl;
      getWordCount++;
    }
    
    //this is what I wrote to count after storing words into the array from the file
    //I used finalCount because I don't think I can reuse getWordCount
    //for(int i=0; wordsArray[i] != '\0'; i++){
       if(wordsArray[i] == ' ')
         finalCount++;
     }

    infile.close();
   
    cout << "The array contains " << finalCount << " words."<< endl;
   }

return 0;
}


I had to hand type this out because my copy/paste is messed up. It compiles file so if there's a typo, please ignore it lol
Last edited on
What can we say about default constructed strings?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <string>
using namespace std;

int main(){
  string words[1];
  
  cout << "empty " << words[0].empty() << '\n';
  cout << "value '" << words[0].empty() << "'\n";
  cout << "length " << words[0].length() << '\n';
  // ERROR no operator: cout << "not 0" << (words[0] != '\0') << '\n';
  // ERROR no operator: cout << "space" << (words[0] == ' ') << '\n';
  cout << "space " << (words[0] == " ") << '\n';
  cout << "empty " << (words[0] == "") << '\n';  
}
What does your input file look like?

Note: The getline() function gets an entire line, not necessarily a single word.

What is the purpose of finalCount? You already have the total number of words in the array, getWordCount. Realize that that if() statement is not inside any loop construct so it will only happen once.

Remember if a stream enters an error state that no further processing of the stream is possible until the error state is cleared. So, for example, if you try to read the entire file more than once you need to take care about clearing the stream error state and repositioning the stream pointer before trying to read the subsequent times.

You should also consider using a std::vector instead of the array, then you will know how many "words" are read by looking at the size of the vector.

Hello, there's no input for the filename but I think that's what you're waiting for:

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
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <conio.h>
using namespace std;

int main()
{
    string testline;
    string word[16];

    ifstream Test ( "filename.txt" );

    if (!Test)
    {
        cout << "There was a problem opening the file. Press any key to close.\n";
        getch();
        return 0;
    }

    int i=0;
    //store words in array while outputting, skipping blank lines
    while( Test.good() )
    {
        getline ( Test, testline, ' ');
        if (testline!="")
        {
            word[i]=testline;
            cout << word[i] << endl;
            i++;
        }
    }

    //output whole array with spaces between each word
    cout<<"\nArray contents:\n";
    for (int k=0;k<i;k++)
        cout<<word[k]<<"\n";
    return 0;
}
Last edited on
1
2
3
4
    //this was what I originally wrote to get the word count from file
    //while(infile >> words){
    //  getWordCount++;
    // } 

That should work.

1
2
3
4
5
6
    //this is what I wrote to count after storing words into the array from the file
    //I used finalCount because I don't think I can reuse getWordCount
    //for(int i=0; wordsArray[i] != '\0'; i++){
       if(wordsArray[i] == ' ')
         finalCount++;
     }

That should not.

You can't compare a string to a character. Also, why would you add one to the count if the string is a space? Also, once you read the file, you have the final word count. If you insist on checking the array, then count up until you reach an empty string.
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
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main(){
  int getWordCount = 0;
  int finalCount = 0;
  string filename;
  string words;
  string wordsArray[2000];
  ifstream infile;
  
  cout << "Enter filename: ";
  getline(cin,filename);
  
  infile.open(filename.c_str());
  
  if(!infile)
    cerr << "Sorry, filename unavailable." << endl;
  else{

      // this was what I originally wrote to get the word count from file
      while(infile >> wordsArray[getWordCount]){
	  getWordCount++;
      }

      cout << "getWordCount is " << getWordCount << '\n';

      // Count the words the hard way
      for(int i=0; !wordsArray[i].empty(); i++){
	  ++finalCount;
      }

    infile.close();
   
    cout << "The array contains " << finalCount << " words."<< endl;
   }

return 0;
}


$ ./foo
Enter filename: foo.cxx
getWordCount is 101
The array contains 101 words.

After I posted this, I realized what I was trying to do and what I was actually doing was wayyyyy off. Thanks for your input! the counting part helped.
Topic archived. No new replies allowed.