Read from txt to vector

Hell there, What is up?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void Equipa::alocaDirector (istream &isV){
	string _name;
	string _age;
	string _country;

	while (!isV.eof()){
		getline(isV, _name);
		getline(isV, _age);
		getline(isV, _country);


		Director *_d1 = new Director (_name, atol(_age.c_str()),_country );
		membros.push_back(_d1);
	}
}


I want this function to "copy" the first 3 txt lines to one position in the vector. Example

Directors.txt:
1
2
3
4
5
6
John
45
England
Mark
53
Belgium


With a print function this would be something like:
Name: John
Age: 45
Country: England
and the same for the second guy.

But something really weird is happening. I have this alloc function and then I have in the main() something to print it:
1
2
3
4
5
6
7
8
9
10
11
12
13
Equipa me1;

				ifstream myFile;
				string output;
				myFile.open("/home/luiscosta/git/AEDA/AEDAP1/DB/MemEqui/Diretores.txt");
				//if (myFile.fail())
				while (!myFile.eof()) {
					getline(myFile, output);
					me1.alocaDirector(myFile);

				}
				myFile.close();
				cout << me1.imprime();


my imprime (print) function:

1
2
3
4
5
6
7
8
9
10
11
12
string Equipa::imprime() {

	stringstream info;

	//info << nome << "," << pais;

		for(vector<MembroEquipa *>::const_iterator it = membros.begin(); it != membros.end(); it++){
			info << (*it)->imprime() << endl;
		}

	return info.str();
}


which calls a new print function:

1
2
3
4
5
6
7
8
string MembroEquipa::imprime() const {
	stringstream ss;
	//ss << nome << ", " << idade << ", " << pais;
	ss << "Name: " << nome << endl;
	ss << "Age: " << idade << endl;
	ss << "Country: " << pais << endl;
	return ss.str();
}


And then I run it, this happens:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Name: 45

Age: 0
Country: Angel Lopez


Name: 50

Age: 0
Country: Armindo Pinto


Name: 43

Age: 0
Country:


Somehow my print function is skipping one line and I can't print anything.

This is really messing with me for a few hours, any help would be huge :)

Best regards
Is line 8 in main() intentional?

-Albatross
It is. Is anything wrong with that?

Regards
What happens when you remove it?

-Albatross
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <string>
#include <vector>
#include <iostream>
#include <sstream>

struct director
{
    director( std::string name = "", int age = 0, std::string country = "" )
        : name(name), age(age), country(country) {}

    std::string name ;
    int age = 0 ;
    std::string country ;
};

std::istream& operator>> ( std::istream& stm, director& dir )
{
    std::string name ;
    int age ;
    std::string country ;
    // read name, read age, throw away the new line, read country
    if( std::getline( stm, name ) && stm >> age && stm.ignore(1000,'\n') && std::getline(stm,country) )
        dir = { name, age, country } ;
    else dir = {} ;

    return stm ;
}

std::ostream& operator<< ( std::ostream& stm, const director& dir )
{ return stm << "name: " << dir.name << "\nage: " << dir.age << "\ncountry: " << dir.country ; }

int main()
{
    std::istringstream file(
                                "John\n"
                                "45\n"
                                "England\n"
                                "Mark\n"
                                "53\n"
                                "Belgium\n"
                           );

    std::vector<director> directors ;

    director d ;
    while( file >> d ) directors.push_back(d) ;

    for( const director& d : directors ) std::cout << d << "\n--------\n" ;
}

http://coliru.stacked-crooked.com/a/7578a9d92123f5fd
It stays the same Albatross.

I don't know why it is only ignoring the first txt line.

The txt I have looks like this:

1
2
3
4
5
6
7
8
9
Antonio Mendes
45
Portugal
Angel Lopez
50
Espanha
Armindo Pinto
43
Portugal


and it doesnt print the first line as intended but starts with the 45.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Name: 45

Age: 0
Country: Angel Lopez


Name: 50

Age: 0
Country: Armindo Pinto


Name: 43

Age: 0
Country: 


I really don't get it.
Any help guys? Deadline is tomorrow and I really need this.
Topic archived. No new replies allowed.