Hello nakash,
Now that I have had a chance to test the program. I found that you have a similar problem with the read function.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
|
void Files::read()
{
//Files d; // <--- Local variable that overshadows what is defined in "main".
int i = 0;
string word;
string definition;
string type;
string blank;
//vector<string>words; // <--- Local variable that overshadows the class variable.
ifstream inFile("Text.txt");
if (!inFile)
{
perror("\n\n File error");
return;
}
|
Lines 4 and 10 create local variables that are destroyed when the function looses scope. So when you do
words.push_back(word);
this is to the local variable and not the class variable. The variables "definitions" and "types" both store their values in the class vectors.
I also moved the 4 strings from the class to the function where they are used. there is no need to define them in the class which only holds the last value read.
As
Thomas1965 demonstrated in his line 38 and as I did
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
while (getline(inFile, word))
{
words.push_back(word);
getline(inFile, definition);
definitions.push_back(definition);
getline(inFile, type);
types.push_back(type);
getline(inFile, blank);
i++;
}
|
This will allow you to read any file of any size. As long as the format of the file is the same.
If you want a little more you could use
while (getline(inFile, word) && getline(inFile, definition) && getline(inFile, type) && getline(inFile, blank))
. This way if any one of those fail to read or set the "eof" bit the while loop will fail. That would leave the ".push_back"s and "i++" inside the while loop.
If you choose to use the "perror" in the if statement this is all you need because "perror" will add the ": " before the message that it adds. So
File error: No such file or directory |
would be your output.
Now once the read works properly I did this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
void Files::display()
{
std::cout << "The vector elements are:\n\n";
for (size_t i = 0; i < words.size(); i++)
{
//cout << "running" << words.at(i) << ' ';
std::cout
<< words[i] << '\n'
<< definitions[i] << '\n'
<< types[i] << "\n\n";
}
}
|
Since all 3 vectors should be the same size or length, both functions return the same number, the for loop is fine with one small change. The ".length()" and ".size()" functions return a "size_t" variable which is an "unsigned" something. The "something" part will depend on your compiler header files and how "size_t" is defined. This could be an "int" or a "long" or maybe something else. Either way defining "i" as a "size_t" means the the types will match. Not usually an error, but a warning that can be avoided.
I am not sure what you intended with line 7, but for now I put a comment on it.
The ".at(?)" has its use, but not here. the []s are sufficient since the for loop limits how far the loop will go. The ".at(?) function is just extra work that is not needed. In a very large file you might see the program slow down processing all the ".at(?)" functions.
I made one change in "main":
1 2 3 4 5 6 7 8 9 10 11
|
int main()
{
Files dictonary;
//intro();
dictonary.read();
dictonary.display();
return 0; // <--- Not required, but makes a good break point.
}
|
It is a smalll change and you can shorten it to "dict" if you want, but it is much better than just "d".
Andy