-1

I am doing a C++ project for the university. I have to make a chronology of historical events. I have a class to save the events, called "FechaHistorica", with a set<string> and int as private members. The set contains strings, each string is one historical event. The int is the year when all that events happened. The other class is called "Cronologia", with a map<int,FechaHistorica> to store every "FechaHistorica".

The problem is with the operator << of FechaHistorica I guess. The executbale recives two .txt files with movies, and the outpout should be the union of both documents. Here is the code for the operator << os FechaHistorica:

const char SEP = '#';
ostream& operator<< (ostream& os, const FechaHistorica& e) {
    os << e.getYear();

    for(const_iterator p = e.eventos.begin(); p != e.eventos.end(); ++p)
        os << SEP << *p;

    return os;
}

The output is:

victor@victor-GL552VW:~/Documentos/Universidad/1º Cuatrimestre/Estructura de dat
os/Practicas/Practicas-ED/Práctica 4 - STL e Iteradores$ ./bin/prueba movies.txt
 films.txt
Cronología leida del archivo: 
0
1900#Sherlock Holmes Baffled#The Enchanted Drawing
1901#Scrooge, or, Marley's Ghost#Star Theatre
1902#A Trip to the Moon
#The Great Train Robbery Fireman
1904#The Impossible Voyage
1905#Adventures of Sherlock Holmes; or, Held for Ransom
#Humorous Phases of Funny Faces#The Story of the Kelly Gang
1907#Ben Hur#L'Enfant prodigue
#Fantasmagorie#The Assassination of the Duke of Guise#The Taming of the Shrew#The Thieving Hand
#The Country Doctoreat#Les Misérables#Princess Nicotine; or, The Smoke Fairy
1910#In Old California'
1911#Defence of Sevastopol#L'Inferno
1912#Independenţa României#The Musketeers of Pig Alley
1913#Barney Oldfield's Race for a Life#Fantômas#Raja Harishchandra#The Bangville Police
1914#Judith of Bethulia#The Perils of Pauline#Tillie's Punctured Romance
1915#A Fool There Was#Birth of a Nation#Les Vampires#The Tramp

As you can see, some years are not printed correct, the output should be:

1900#Sherlock Holmes Baffled#The Enchanted Drawing
1901#Scrooge, or, Marley's Ghost#Star Theatre
1902#A Trip to the Moon
1903#The Great Train Robbery#Life of an American Fireman
1904#The Impossible Voyage
1905#Adventures of Sherlock Holmes; or, Held for Ransom
1906#The Story of the Kelly Gang#Humorous Phases of Funny Faces#Dream of a Rarebit Fiend
1907#Ben Hur#L'Enfant prodigue
1908#Fantasmagorie#The Taming of the Shrew#The Thieving Hand#The Assassination of the Duke of Guise#A Visit to the Seaside
1909#The Country Doctor#A Corner in Wheat#Les Misérables#Princess Nicotine; or, The Smoke Fairy
1910#In Old California'
1911#L'Inferno#Defence of Sevastopol
1912#Independenţa României#The Musketeers of Pig Alley
1913#The Bangville Police#Barney Oldfield's Race for a Life#Fantômas#Raja Harishchandra
1914#The Perils of Pauline#Judith of Bethulia#Tillie's Punctured Romance
1915#Birth of a Nation#Les Vampires#The Tramp#A Fool There Was

Well, if I add endl after every event, the output is correct, I mean, os << SEP << *p << endl; instead of os << SEP << *p;, but I don't want to have endl after each historical event in the output. What can I do?

I hope you can help me, thanks.

The files with data are "movies.txt" and "films.txt" The full code of the proyect is here: https://github.com/vicase/Practicas-ED/tree/master/Pr%C3%A1ctica%204%20-%20STL%20e%20Iteradores

southernKid33
  • 351
  • 1
  • 2
  • 14
  • Have a look at [`std::endl`](http://en.cppreference.com/w/cpp/io/manip/endl). The `std::endl` does actually two things: output of `'\n'` and `flush`. If you don't want the `'\n'` but the `flush` then simply add `<< `[`std::flush`](http://en.cppreference.com/w/cpp/io/manip/flush) at end of output. – Scheff's Cat Nov 26 '17 at 11:44
  • I tried with os << SEP << *p << flush; but it doesn't work. If i make cout << endl; in the operator << before os << SEP << *p << flush; it works, but I have the endl again. I do not understand why it does not work – southernKid33 Nov 26 '17 at 11:58
  • Make small input files. Debug step by step. Break down individual parts of your software and test them isolated. Find out what's going wrong. I had a short look into your source code: `main()` has no `return`. Don't use `while (!feof())` ([Why is “while ( !feof (file) )” always wrong?](https://stackoverflow.com/q/5431941/7478597)). In general, this is far too much code to ask for help on this site. Concerning your output operator: output should work with `std::endl` as well as with `std::flush` as well as without both of them. Hence, there might be broken something else. – Scheff's Cat Nov 26 '17 at 12:52
  • Ok, thank you so much! – southernKid33 Nov 26 '17 at 13:10

1 Answers1

0

Fixed: the problem was that the first txt was in crlf format (Windows) and the other one was in LF(Ubuntu). With both documents in LF works ok. Thanks everybody

southernKid33
  • 351
  • 1
  • 2
  • 14