-1

I am trying to insert several string class arrays (taken from an input file) within a string class array.

The program I am writing consists of a Dictionary class in which the default constructor takes in a filename (eg."words.txt") as a parameter, and thereby stores each individually read word into a single element in a String class array. The text file would look something like:

example
text
file
here
etc...

The code I have written to test it out so (which is not working at all) is below:

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

class Dictionary(const char *file) {
public:
  string wordList;
  int numWords;
};

Dictionary::Dictionary(const char *file) {

  ifstream inFile;
  int SIZE = 50000;
  char pass[SIZE+1];
  numWords = 0;
  inFile.open(file);
  if(!inFile)
    cout << "File can not be opened" << endl;
  else
    while(inFile.getline(pass, SIZE)){
      wordList[numWords] = pass[SIZE+1]; 
      numWords++;
    }
  }
  inFile.close();
  cout << wordList[5] << endl;
  cout << numWords << endl;
}

int main(){
  Dictionary *foo = new Dictionary("words.txt");
};

The code compiles but prints:

//should print out the 5th word in the file but nothing
0

I am wondering what exactly am I missing here? I've been worked up about this overnight and I feel the solution is so simple but I'm missing the point.

My main problem seems to be the error message that prints out throughout my other trials concerning an "invalid conversion from 'char*' to 'char'. Further, other functions in the program not shown require the letters of each word in the elements of the String class array to be manipulated (preferable as a string class array instead of C-string). I'm at a lost here. Please help?

I found this helpful link but it's for Java. What would be an equivalent for C++? http://www.mathcs.emory.edu/~cheung/Courses/170/Syllabus/09/String-array.html

teamram19
  • 1
  • 2
  • 1
    `Dictionary *foo = new Dictionary("words.txt");` You need to put away the Java books: Here is C++: `Dictionary foo("words.txt");` Second, collating words from a file into an array is basically a 3 line program (maybe less). You don't need an entire class. – PaulMcKenzie Apr 10 '15 at 19:28
  • I wrote it here as a class since I'll be implementing the code for composition to another class. In the other class where I'll be using the Dictionary object the call in this main to the Dictionary object would be the in the same syntax. I have to use a pointer, class requirement. – teamram19 Apr 10 '15 at 19:33
  • What is that weird mixture between a function and a class declaration? Does that even compile? Which compiler? Also, you can well reuse code without putting it into a class. OOP is a tool, not a goal! – Ulrich Eckhardt Apr 10 '15 at 19:38
  • I'm sorry I'm afraid I don't know what you mean. I'm quite new at this, you mean the parameter (const char*)? It is compiling. – teamram19 Apr 10 '15 at 19:45
  • @teamram19 - What do you need to implement as a pointer? Your code has a memory leak. If you mean that line in `main`, there is no need for a pointer to be used in `main` -- that's why I mentioned that this isn't Java.. Second: http://stackoverflow.com/questions/8365013/reading-line-from-text-file-and-putting-the-strings-into-a-vector It's a two line procedure. – PaulMcKenzie Apr 10 '15 at 20:00

1 Answers1

0

A couple places to start:

  1. You're trying to use wordList, which is of type string, like it's an array of strings. wordList[5] will give you the sixth character in wordList... assuming you've assigned it correctly.
  2. SIZE appears to be undefined.
Bob
  • 1,779
  • 3
  • 22
  • 35
  • Oops forgot to write that down! – teamram19 Apr 10 '15 at 19:39
  • Thank you! I didn't realize about the difference. I changed string wordList; to string wordList[]; but now getting Segmentation fault. – teamram19 Apr 10 '15 at 19:40
  • Declaring size zero arrays isn't really a thing in C, you need to statically declare it to be the size you need it to be, or use pointers, or use a structure such as vector. – Bob Apr 10 '15 at 19:44