Member Avatar for Jbvo

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


}

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];
    }
}
Member Avatar for Jbvo

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");
}
Member Avatar for 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.

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();
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.