Take several lines of input, split by comma, save each line in an array of lines

Here is an example of the type of input I would work with: (comes from standard input)

Archery,M,TEAM,Archery,Lord Cricket Ground,1. GOLD,team ITA,Italy
Archery,M,TEAM,Archery,Lord Cricket Ground,2. SILVER,team USA,United States
Archery,M,TEAM,Archery,Lord Cricket Ground,3. BRONZE,team KOR,South Korea
Cycling,M,IND,Road,Regent Park,1. GOLD,Aleksander Winokurow,Kazakhstan
Cycling,M,IND,Road,Regent Park,2. SILVER,Rigoberto Uran,Colombia
Cycling,M,IND,Road,Regent Park,3. BRONZE,Alexander Kristoff,Norway
Fencing,F,IND,Foil,ExCeL,1. GOLD,Elisa Di Francisca,Italy
InsertionEnd

As the name implies, I want to take each line, break it with a comma and save each of these lines into an array (or a vector of lines). Then I want to take each element in the array and use it as parameters for the function. I know how to read multiple lines and how to split a line, but when I put these things together, it does not work for me.

my line of thinking:

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

using namespace std;
int main()
{
    string line;
    stringstream ss(line);

while (line != "InsertionEnd") {

    vector<string> array;
    getline(ss, line, ',');
    array.push_back(line);
    addItem(array[0],array[1],array[2],array[3],array[4],array[5],array[6],array[7])
    }
}

therefore, after I get my array, I want to use the addItem function that I created, which only creates the athlete structure (takes 8 parameters). So:

myTable.addItem("Archery","M","TEAM","Archery","Lord Cricket Ground","1. GOLD","team ITA","Italy");

Am I on the right track? or is it completely off the ground? thank.

note: addItem , .

+4
2

; :
 - stringstream ss(line); - ; , .  - array.push_back(line); - line ; .


:

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

using namespace std;

int main()
{
    string line;
    vector<vector<string>> array;
    while ( true )
    {
        getline ( cin, line );
        if ( line == "InsertionEnd" )
        {
            break;
        }

        stringstream ss ( line );
        string word;
        vector<string> vector_line;
        while ( getline ( ss, word, ',' ) )
        {
            vector_line.push_back ( word );
        }
        array.push_back ( vector_line );
    }
}
+3

, , addItem :

string line;
while (getline(cin, line)) { // for each line...
    if (line == "InsertionEnd") break;

    // split line into a vector
    vector<string> array;
    stringstream ss(line);
    string token;
    while (getline(ss, token, ',')) { // for each token...
        array.push_back(token);
    }

    // use the vector
    addItem(array[0],array[1],array[2],array[3],array[4],array[5],array[6],array[7]);
}
+2

Source: https://habr.com/ru/post/1657223/


All Articles