I am trying to read each word from a file and put it in a pointer then send it to a vector.
Here is what I have so far. I am having a hard time sending the data to a function where I will add it to a vector.

Here is what I have so far. Right now the output is showing the last word of the file spelt out with one letter per line. It should be showing all the words from the text file with one word per line. (I will be counting the words later right now I am just trying to get this part done.) Thanks for your help.

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



void WordFunction(string *pstr);

int main()
{
    ifstream file;
    string word;
    file.open("word.txt");
    while (!file.eof())
    {

        file >> word;
        WordFunction(&word);
    }
    file.close();

      for (int i=0; i < word.size(); i++)
    {
        cout << word[i] << endl;
    }


   system ("pause");
    return 0;
}

void WordFunction(string *pstr)
{
    vector<string *> words;
    words.push_back(pstr);


}

Edited 4 Years Ago by deceptikon: Changed to a discussion thread

currently you are doing nothing with the vector you have in your function. since it is a local variable of the function it gets created and destrroyed everytime you call the function. If you want to get all of the words from a text file into a vector you can do that in main by simply doing:

string word;
vector<string> fileWords;
ifstream fin("word.txt");
while (file >> word)
{
    fileWords.push_back(word);
}

You could do it like this, passing not a copy of the vector, but it's adress, and work directly on the vector, from your function. Here's a quick example.

void WordFunctions(string *pstr, vector<string*> &words){
    words.push_back(pstr);
}

int main(){
    vector<string*> a;
    for(int i=0;i<10;i++){
        string* f=new string(1, (char)80+i);
        WordFunctions(f, a);
    }
    for (int i=0;i<10;i++){
        cout<<(*a[i])<<" ";
        delete a[i];
    }
}

Thanks for the replies. My teacher want me to use vector<string *>.

This is where I am at. It is passing the values to the function but it wipes out the vector everytime. Hmmm it looks like I need to find a way to create a new string everytime it goes through the loop.

Thank you.

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


void WordFunctions(string *pstr, vector<string*> &words)
{
    words.push_back(pstr);
}
int main(){
    ifstream file;
    vector<string*> a;
    string word;
    file.open("word.txt");
    while (!file.eof())
    {
        file >> word;
        WordFunctions(&word, a);
    }
    file.close();

     for (int i=0;i<10;i++){
        cout<<(*a[i])<<" ";
        delete a[i];
    }

     system ("pause");
}

Edited 4 Years Ago by Jbvo

This is what I needed

void WordFunctions(string *pstr, vector<string*> &words)
{
    words.push_back(new string(*pstr));
}

Be carefull that the:

for (int i=0;i<10;i++){
        cout<<(*a[i])<<" ";
        delete a[i];
    }

especially the

delete a[i];

part will delete the objects strings created in the memory, from the vector words. So, if you do plan to use that itmes further in your program, I suggest deallocating them only at the end of your program.

Edited 4 Years Ago by Lucaci Andrew

Avoid the memory leak

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

void WordFunction(string *pstr);
void CleanUp();

vector<string *> words;

int main()
{
    ifstream file;
    string word;
    file.open("word.txt");
    while (!file.eof())
    {

        file >> word;
        if (!file.eof())
        {
            WordFunction(&word);
        }
    }
    file.close();
    for (int i=0; i < words.size(); i++)
    {
        cout << *(words[i]) << endl;
    }
    CleanUp();
}

void WordFunction(string *pstr)
{
    string* str = new string(*pstr);
    words.push_back(str);

}

void CleanUp()
{
    string* pstr;
    for (int i=0; i < words.size(); i++)
    {
        pstr = words[i];
        delete pstr;
    }
    words.clear();
}
This article has been dead for over six months. Start a new discussion instead.