4

Here is an example of the type of input I would be working with: (coming from standard input)

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

As the title suggests I want to take each line, split it a the comma, and store each one of those strings into an array (or vector of strings). THEN I want to take each item in the array and use it as parameters for a function. I know how to read multiple lines and how to split a string but when I put those things together it's not really working out 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])
    }
}

so after I get my array i want to use an addItem function that I made which just creates an athlete structure (takes 8 parameters). like so:

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

Am I on the right track ? or is this completely off base?? thanks.

note: I have tested the addItem functions and it works when you just type in the parameters yourself.

2 Answers 2

3

Your thinking is on the right track; but you are making a few mistakes in the code:
- stringstream ss(line); - you are initializing the stringstream on an empty string; you need to first input the line and then insert it into the stringstream.
- array.push_back(line); - you are directly pushing the string line to the resultant vector; you need to first break it up into its constituent words.


The following code implements a working solution to your problem:

#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 );
    }
}
Sign up to request clarification or add additional context in comments.

Comments

2

Here is a solution that properly loops through the input, splitting each line and calling addItem with the extracted tokens:

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]);
}

4 Comments

Well shouldnt there be a new vector for every single line? So if I declare the vector outside of the while loop wouldn't that just create one giant vector rather than creating a new one each time the loop executes and passing that in?
It wasn't clear where the contents of your line variable were coming from... I'll update my answer
I changed my answer.
well it is storing in the array correctly but when i pass it into addItem the program crashes, still working on it though!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.