Read word then add to list

So I'm trying to complete this part of a program where I have to read a text file from Stdin and add it to the 'word list' wl. I get how to read from a text file but I don't know how to go about adding 'words' to a list, if that makes sense. So here's what I got:

1
2
3
4
5
6
7
8
9
10
11
12
13
 
   string getWord(){
        string word;
        while (cin >> word){
            getline(cin, word);
        }
        return word;
    }
	
    void fillWordList(string source[], int &sourceLength){
        ifstream in.file;
        sourceLength = 50;
        source[sourceLength]; ///this is the part I'm having trouble on 


Source is an array that determines how many words are read from the text and length is the amount printed on screen.

Any ideas on what I should begin with?

Here's the program I'm writing the implementation 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
41
42
43
44
45
46
47
    #include <iostream>
    #include <string>
    #include <vector>
    #include "ngrams.h"
    void help(char * cmd) {
      cout << "Usage: " << cmd << " [OPTIONS] < INPUTFILE" << endl;
      cout << "Options:" << endl;
      cout << "  --seed RANDOMSEED" << endl;
      cout << "  --ngram NGRAMCOUNT" << endl;
      cout << "  --out OUTPUTWORDCOUNT" << endl;
    }
    string source[250000];
    vector<string> ngram;
    int main(int argc, char* argv[]) {
      int n, outputN, sl;
      n = 3;
      outputN = 100;
      for (int i = 0; i < argc; i++) {
        if (string(argv[i]) == "--seed") {
          srand(atoi(argv[i+1]));
        } else if (string(argv[i]) == "--ngram") {
          n = 1 + atoi(argv[i+1]);
        } else if (string(argv[i]) == "--out") {
          outputN = atoi(argv[i+1]);
        } else if (string(argv[i]) == "--help") {
          help(argv[0]);
    return 0; }
      }
      fillWordList(source,sl);
      cout << sl << " words found." << endl;
      cout << "First word: " << source[0] << endl;
      cout << "Last word:  " << source[sl-1] << endl;
      for (int i = 0; i < n; i++) {
        ngram.push_back(source[i]);
      }
      cout << "Initial ngram: ";
      put(ngram);
      cout << endl;
      for (int i = 0; i < outputN; i++) {
        if (i % 10 == 0) {
      cout << endl;
    }
    //put(ngram);
    //cout << endl;
    cout << ngram[0] << " ";
    findAndShift(ngram, source, sl);
    } }

I'm supposed to use this as a reference but it dosen't help me too much.
split ur source string based on delimiter space, ull get all the words, insert them one by one into list.
Could you provide an example please? Sorry, I'm not completely sure on what to do.
Last edited on
Topic archived. No new replies allowed.