Accept, read and store txt file into strin array. then count

So I posted earlier and realized later I was wayyyy off. Now, I think I'm pretty close to doing what I was trying to do originally. I'm trying to open txt file, read the words into a string array, then count the number of words in the array. Please check and see if I am missing something. Later on I have to count letters, alphabetize and such, but this seems to be the part I'm most lost on. Please be patient with me - this is my first attempt at coding in over 3 years :(

An example of the file input would be:
Hello, my name is Bob. I love to eat cookies.

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


//Purpose: check number of command line arguments, display error, get words from file, save to memory, close file, analyze metrics and display
int main(int argc, char *argv[]){
  if(argc < 2){
    cout << "Correct usage: " << endl;
  }else{
    //calls getWords to read file
    getWords(maxSize, wordsArray, argv[1]);

    //get words from file here -- ????

    //close file
    fileIn.close();

    //display metrics (doing this later)
  return 0;
}

//Purpose: input file for reading, otherwise display error message. Read words from file, store into array of strings. Return the number of words in the string array.
int getWords(int maxSize, string wordsArray[], string filename){
  int totalWords = 0;
  int counter = 0;
  int i;

  //open input file for reading
  if(!fileIn)
    cerr << "Sorry, could not open " << filename << " for reading << endl;
  else{
    //get each words one at a time and read into wordsArray
    while(fileIn, wordsArray[counter]){
      counter++;
    }
    
    //to find the count of words in the string array
    for(int i = 0, !wordsArray[i].empty(); i++){
      ++totalWords;
    }
  }
  return totalWords;
}

//Purpose: accept the array of strings and integer size, like last word alphabetically and return it (ex: I love you --> last word is "you")
string findMaxWord(int maxSize, string wordsArray[]){

  int N = sizeof(wordsArray)/sizeof(wordsArray[0]) - 1;
  sort(begin(wordsArray),end(wordsArray));
  for(int i = N; i >= N; i--){
    cout << wordsArray[i] << endl;
  }
    lastWord = wordsArray[i]; //I dont even think you can do this? Im trying to store the last word of the string array into the string and return it

  return lastWord;
} 
Last edited on
There are a few errors.

At line 14, you have called the getWords() function. There will be an error there because the function has not been declared before it has been called. There are also undeclared variables: maxSize and wordsArray

At line 33, the second string is missing a quotation mark.

After you define maxSize and wordsArray, the word count process should be complete.
Last edited on
If this isn't a homework then you better use a vector instead of the old-fashioned arrays.
http://www.cplusplus.com/reference/vector/vector/

To read words separated by whitespace you can use the >> operator.
1
2
3
4
5
6
7
8
9
10
11
12
ifstream input("your file name");
if (!input)
{
  cout << "Error opening file";
  return 1;
}
string word;
while (input >> word)
{
  // use word
  // be aware that word might end with ',' o '.'
}
I'm trying to open txt file, read the words into a string array, then count the number of words in the array.
Are you required to use a string array? You don't need it to count the words and find the largest.
Yes. Unfortunately it does have to be a string array.
If you have to use this old C crab then you need to count the words in the file first, allocate your array and read the words into it.
http://www.cplusplus.com/doc/tutorial/dynamic/
Please post the assignment.
I'm trying to open txt file, read the words into a string array, then count the number of words in the array.

The procedures implement your steps as follows, pretty much as you've already done.
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
// declare the stuff we use
#include <string>
#include <vector>
#include <fstream>

// we need some prototypes
int getWords(std::istream& input, std::vector<std::string>& wordsArray);
unsigned countWords(const std::vector<std::string>& wordArray);;

int main(int argc, char *argv[]){
  if (argc < 2)
    return 1;

  // open txt file
  std::ifstream fileIn(argv[1]);

  //calls getWords to read file
  std::vector<std::string> wordsArray;
  getWords(fileIn, wordsArray);

  // get words from file here -- ????
  unsigned countWords(wordArray);

  // display metrics (doing this later)

  // the file object closes the file on destruction
}


Then you need to implement the procedures:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
int getWords(std::istream& input, std::vector<std::string>& wordsArray) {
  // read a words from stream
  std::string word;
  while (input >> word) {
    wordsArray.push_back(word);
  }
}

unsigned countWords(const std::vector<std::string>& wordArray) {
  unsigned count = 0;
  // wordArray has the words, you need to work out how many there are

  return count;
}
Last edited on
Topic archived. No new replies allowed.