0

So this is my main method:

#include <iostream>
#include "TextFileToArray.h"
#include <vector>
using namespace std;

int main()
{
    cout << "Hello world!" << endl;
    TextFileToArray myobject("C:/bunz.txt");
    vector<string> v[10];

    myobject.vectorfiller(*v);

    for(int i =0; i<10; i++){

    cout << v;

    }
}

It calls upon an object known as myobject and it calls upon a method/function. Here is the method/function:

int TextFileToArray::vectorfiller(vector<string>& givenpointer) {
    vector<string> *innervec = &givenpointer;
    const char * constantcharversion = path.c_str();
    ifstream filler(constantcharversion);

    string bunz;
    string lineoutliner = "Line ";
    string equalssign = " = ";
    int numbercounter = 1;
    while (!filler.eof()) {

        std::getline(filler, bunz, ';');
        if (bunz.empty()) {
            lineoutliner = "";
            numbercounter = 0;
            equalssign = "";
        }
        cout << lineoutliner  << numbercounter << equalssign << bunz <<endl;
        cout << "" << endl;
        innervec->push_back(bunz);

        numbercounter++;
    }
    filler.close();

    return 0;
}

So far it displays the text from the textfile, but for some reason it pushes memory addresses into the vector, so when main() displays the vector, it shows memory locations:

it won't work

Gustavo Muenz
  • 9,278
  • 7
  • 40
  • 42
turnt
  • 3,235
  • 5
  • 23
  • 39
  • Well aside from the fact that as Luchian says you have an array of vectors instead of just one vector, the for loop seems to be printing the memory address of the v array 10 times. Did you mean `cout << v[i]` ? – yellow_nimbus Jan 03 '13 at 19:08
  • [Read this.](http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong) It will eventually cause you painful time. – chris Jan 03 '13 at 19:12

3 Answers3

4

vector<string> v[10]; creates an array of 10 vectors, which is probably not what you want.

Create a single vector, pass that as parameter, and output its contents:

vector<string> v;
myobject.vectorfiller(v);
for(int i =0; i < v.size(); i++){
    cout << v[i];
}
Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
1

Agree, size should not be 10, should be size(), and count<

36Kr
  • 121
  • 1
  • 5
1

The problem is that you're printing the array of vectors, not the elements in the first vector. Instead, you want this in your main:

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

PS: As Luchian said, you are creating 10 vectors, not one vector with 10 slots. To get just one vector do this:

vector<string> v;

You also don't need to mention 10; vectors grow when you push elements on them. If you happen to know how much space you want to be reserved ahead of time, you can use the reserve member function like so:

vector<string> v;
v.reserve(some_number);

reserve doesn't change the size of v; it only makes the vector ready to accept that many elements, so that it doesn't have to reallocate memory, and copy things around as much. It is purely an optimization; if you were to simply comment out reserve calls in your program, it will behave exactly the same. The only thing that might change is performance, and memory usage.

allyourcode
  • 21,871
  • 18
  • 78
  • 106