0

I've been trying to display 2 rows with 6 columns, on different rows. I've tried different methods, none of which seems to be working. This is what I have for now:

void Travel::LoadDest()
{

    ifstream file("worlddeals.txt");
    vector<vector<string>>data;

    if (file) {
        string line;
        while (getline(file, line)) {
            data.push_back(vector<string>());

            stringstream split(line);
            string value;

            while (split >> value)
                data.back().push_back(value);
        }
    }
    for (int i = 0; i < data.size(); i++) {
        for (int j = 0; j < data[i].size(); j++)
            cout << data[i][j] << endl;

        cout << '\n';
    }
}

In my .txt I have:

Corfu [Tab] 5* Gelina Village Waterpark [Tab] 21 May 2017 [Tab] Luton [Tab] 10 nights [Tab] All Inclusive

Holguin [Tab] 5* Paradisus Rio De Oro [Tab] 22 May 2017 [Tab] Manchester [Tab] 10 nights [Tab] All Inclusive

What I want the output to be is:

Corfu

Accommodation: 5* Gelina Village Waterpark

Departure Date: 21 May 2017

Departure Airport: Luton

Duration: 10 nights

Board Basis: All Inclusive


Holguin

Accommodation: 5* Paradisus Rio De OroStar

Departure Date: 22 May 2017

Departure Airport: Manchester

Duration: 10 nights

Board Basis: All Inclusive

  • How are your fields delimited in the text file? Is the computer supposed to just know that "Waterpark" is part of the hotel name, and not part of the date? Maybe you could look up CSV format, just as an example, or maybe write them in xml? – Kenny Ostrom Apr 08 '17 at 18:10
  • I just have tabs between each value. So maybe I need to add to the code for the computer to find these tabs, and to move the value after them on a new row. Thanks, I'll have a look at it. – John Moore Apr 08 '17 at 18:27
  • http://stackoverflow.com/questions/11719538/how-to-use-stringstream-to-separate-comma-separated-strings except use \r instead of a comma? – Kenny Ostrom Apr 08 '17 at 20:52
  • I've been trying to do it like that for some time now, but it keeps underling getline, saying that it doesn't match the argument list. – John Moore Apr 08 '17 at 21:11
  • Also I meant \t for tab. I'll try it after dinner and maybe post the code. – Kenny Ostrom Apr 08 '17 at 23:18

1 Answers1

0

You said it's not working out for you, but I just tried that code I linked using your input data, and it printed out pretty much what you want. I didn't print the field names, but here is the line parsed according to your specifications:

#include <iostream>
#include <sstream>

void print_tab_delimited_fields(const std::string &input)
{
    std::istringstream ss(input);
    std::string token;
    while (std::getline(ss, token, '\t')) {
        std::cout << token << '\n';
    }
}

int main()
{
    std::string line1 = "Corfu\t5 * Gelina Village Waterpark\t21 May 2017\tLuton\t10 nights\tAll Inclusive";
    print_tab_delimited_fields(line1);

    std::string line2 = "Holguin\t5 * Paradisus Rio De Oro\t22 May 2017\tManchester\t10 nights\tAll Inclusive";
    print_tab_delimited_fields(line2);
}

output

Corfu
5 * Gelina Village Waterpark
21 May 2017
Luton
10 nights
All Inclusive
Holguin
5 * Paradisus Rio De Oro
22 May 2017
Manchester
10 nights
All Inclusive

It would be a trivial change to push the results to a vector, or just print them along with the field labels, since the data file is already assumed to have those exact fields in that exact order.

Kenny Ostrom
  • 5,639
  • 2
  • 21
  • 30
  • Thank you so much for your help, mate! Maybe it's not working for me, because I am trying to get the data from a text file, rather than having it in the code. – John Moore Apr 09 '17 at 00:13
  • No, the getline should get those exact lines from the text file. If it's not, then your problem may be a bad data file. It might be easier to verify that the data looks right if you used something more visible than a tab. A format like csv or xml would be common, here. – Kenny Ostrom Apr 09 '17 at 01:31