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