0

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:

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?

EDIT: Here's the program I'm writing the implementation for:

#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.

  • 1
    The relation between your two functions is not clear. And I'm not quite sure about what you want to do with source and sourceLength. – Sceptical Jule Jun 03 '13 at 20:22
  • @Sceptical Jule I'm a bit baffled on what to do with them as well. Here's a description from the header of what I'm supposed to do: `string getWord(); // return a space-separated word from standard input` `void fillWordList(string source[], int &sourceLength); // read a text file from standard in and add all // space-separated words to the word list wl` – user2421178 Jun 03 '13 at 21:39

1 Answers1

1

Declaring raw array requires the size of the array to be a compile-time constant. Use std::vector or at least std::array instead. And pass source by reference if you want to fill it.

Sceptical Jule
  • 889
  • 1
  • 8
  • 26