0

I have written the following piece of code and after staring at it for the longest time and trying out a set of different things, I have accomplished nothing. The problem is that the text that is printed on the screen, although "correct", does not include the const strings I'm printing every time like "date: " or "task: ".

Here is the code:

#include <iostream>
#include <fstream>
#include <string>
#include <vector>

using namespace std;

int main() {

    ifstream myFile("calend.txt");

    vector<string> info;
    string str;

    while (getline(myFile, str, '-')) {

        info.push_back(str);

        if (info.size() > 1) {

            cout << "date: " << info.at(0) << endl;
            cout << "task: " << info.at(1) << endl;

            info.clear();
        }
    }

    myFile.close();
    system("pause");
    return 0;
}

Here is the text file (calend.txt):

15/05-checkpoint IART
18/05-checkpoint COMP
22/05-SDIS
25/05-apresentacao PPIN
27/05-IART
28/05-apresentacao LPOO
28/05-teste PPIN
01/06-LBAW
05/06-COMP
08*14/06-PPIN

And finally here is the output:

date: 15/05
task: checkpoint IART
18/05
date: checkpoint COMP
22/05
task: SDIS
25/05
date: apresentacao PPIN
27/05
task: IART
28/05
date: apresentacao LPOO
28/05
task: teste PPIN
01/06
date: LBAW
05/06
task: COMP
08*14/06

As you can see, the first two lines are printed correctly, but afterwards it skips the const strings. Thanks in advance.

Azeem
  • 11,148
  • 4
  • 27
  • 40
NoHoly
  • 23
  • 4

2 Answers2

1

You are using delimiter '-' in std::getline().

So, from line # 1:

15/05-checkpoint IART
^^^^^

Only, 15/05 is read and stored in str.

And, then, it goes on to read up to another delimiter i.e. line # 1 and 2:

15/05-checkpoint IART
      ^^^^^^^^^^^^^^^

18/05-checkpoint COMP
^^^^^

And, it goes on like that.

That's why you're seeing as the output:

date: 15/05
task: checkpoint IART
18/05

This is your one output on the console.

Solution:

Read up to - and then to newline like this:

#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>

int main()
{
    std::ifstream myFile { "calend.txt" };
    if ( !myFile.is_open() )
    {
        std::cerr << "Could not open file!\n";
        return -1;
    }

    std::vector<std::string> info;
    std::string date;
    std::string task;

    while ( getline(myFile, date, '-') && getline(myFile, task) )
    {
        info.push_back( date );
        info.push_back( task );

        if ( info.size() > 1 )
        {
            std::cout << "date: " << info.at(0) << '\n';
            std::cout << "task: " << info.at(1) << '\n';
            info.clear();
        }
    }

    myFile.close();
    return 0;
}

Output:

date: 15/05
task: checkpoint IART
date: 18/05
task: checkpoint COMP
date: 22/05
task: SDIS
date: 25/05
task: apresentacao PPIN
date: 27/05
task: IART
date: 28/05
task: apresentacao LPOO
date: 28/05
task: teste PPIN
date: 01/06
task: LBAW
date: 05/06
task: COMP
date: 08*14/06
task: PPIN

Relevant reads:

Azeem
  • 11,148
  • 4
  • 27
  • 40
1

The first line of the loop says: give me everything till '-', so you basically make getline() to skip line delimiters and look only for '-' character.

This why it splits correctly at first iteration and prints

date: 15/05

But then the second iteration captures everything until next '-' and gives you

task: checkpoint IART
18/05

And from here on it's all kind of shifted.

To fix this try calling getline() whithout '-' to read the file line-by-line into str, then split the str into two tokens.

jackhab
  • 17,128
  • 37
  • 99
  • 136