2

my vector is not recording any data from my text file

the text file contains 106,187 words but for testing purposes i'm using the below set:

The text file format

bleeping
damned.
adj

blemishless
Without blemish; spotless.
adj

blendous
Pertaining to consisting of or containing blende.
adj

abaca
The Manila-hemp plant (Musa textilis); also its fiber. See Manila hemp under Manila.
n

abacinate
To blind by a red-hot metal plate held before the eyes.
v

abacination
The act of abacinating.
n

i need to get the words into the words vector and definitions into the definitions vector and then the types into the types vector. afterwards i need to search for a word and be able to get the meaning and the type. for this i was thinking of using a for loop would that be a good idea or is there any other way.

My Code

#include <iostream>
#include <fstream>
#include <string>
#include <vector>


using namespace std;

class Files {
private:
    string word;
    string definition;
    string type;
    string blank;
    vector<string>words;
    vector<string>definitions;
    vector<string>types;
public:
    void read();
    //void intro();
    void display(vector <string>& words);
};
void Files::display(vector <string> & words) {
   
        std::cout << "The vector elements are : ";

        for (int i = 0; i < words.size(); i++)
            cout <<"running"<< words.at(i) << ' ';
    
    
}
void Files::read()
{
    Files d;
    int i = 0;
    string  e, t;
    ifstream out("Text.txt");
    string array[]{ d.word,d.definition,d.type,d.blank };
    vector<string>words;

    
    do
    {
        (getline(out, d.word, '\n'));
        words.push_back(word); 
        getline(out, d.definition, '\n');
        definitions.push_back(definition);
        getline(out, d.type, '\n');
        types.push_back(type);
        getline(out, d.blank, '\n');

        i++;
        cout << "number of line " << i << ' ' << d.word << endl;
    } while (!out.eof());
   
    display(words);

}


int main()
{
    Files d;
    //intro();

    d.read();
    //d.display();
}

  • Think about where you store the result of your `getline` calls, and what variables you use when you push to the vectors. – Some programmer dude Aug 31 '20 at 11:01
  • 1
    As a way forward to solving the problem, think about the member variables `word`, `definition`, `type` and `blank`. Do they really need to be member variables? Perhaps they should be local variables inside the `read` function? Then also start to think about the variables `e`, `t`, `array` and most importantly the *local* variable `words` (as opposed to the *member* variable `words`). – Some programmer dude Aug 31 '20 at 11:04
  • from the `getline(out, d.word, '\n')` call i'm storing it in the `d.word` and `words.push_back(word);` is pushing the word to the back of the words vector `vectorwords;` so my problem is why is it empty still – Akash De Silva Aug 31 '20 at 11:05
  • `d.word` is a different string from `this->word` (which is the string you push back). It seems you might need to take a couple of steps back, and start over with some of the basics about C++. – Some programmer dude Aug 31 '20 at 11:06
  • thanks @Someprogrammerdude making them a local variable resolved the issue. – Akash De Silva Aug 31 '20 at 11:13
  • do you think it may be easier using a std::map > to store your data instead of 3 seperate vectors? – william_ Aug 31 '20 at 11:27
  • i did try with map but it can only store a key and a value but in my case i have 2 values and 1 key it gives me errors, is there any possibility you could show how it can be done. thanks – Akash De Silva Aug 31 '20 at 13:37

2 Answers2

1

Pseudocode :

vector storage
string line
while getline(line): // this should read only empty lines.
 assert line == ""
 Element elem
 if not getline(elem.word):
  break // if it is the last line, stop
 getline(elem.definition) // since a word is there, definition is also coming
 getline(elem.type)
 storage.append(elem.type)

puio
  • 1,208
  • 6
  • 19
1

You have some extra logic in there that is not really necessary. You should avoid creating an object of type Files inside the read function, it does not make much sense, you have those variables available as members but you're creating an object of the same type and using this object's local variables, which are the same to store the values you want to store in the member variables, it's mind twisting.

You should also use getline return values as a stop condition for your read loops.

You can also make string word, string definition, string type local to the read function.

An improved version of your function may look like this:

Live demo

void Files::read()
{
    //make these local, it should work either way, it's just better design
    string word;
    string definition;
    string type;
    string blank;
    int i = 0;
    ifstream out("text.txt");
   
    //use getline as a condition of the while loop instead of eof
    while(getline(out, word, '\n') && getline(out, definition, '\n') && getline(out, type, '\n')){
        words.push_back(word);      
        definitions.push_back(definition);
        getline(out, blank, '\n');
        types.push_back(type);       
        i++;
        cout << "number of line " << i << " " << word << endl;
    } 
    display(words);
}

Also note that the use of using namespace std is not recommended.

anastaciu
  • 23,467
  • 7
  • 28
  • 53
  • Three getlines in while conditional is genius ! :) edit: this is also safe against partial elements (like not having type, or definition) – puio Sep 02 '20 at 09:37