storing data in vectors using getline c++

Viewed 350

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();
}

2 Answers

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)

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.

Related