0

I have a file that is outputting information into a class. Specifically one of the strings I am trying to output will go into a vector. The problem is that I am trying to take a string (in this case of interests) which would be formatted:

interest_string = "food, exercise, stuff"

So basically I want to turn the string above into a string of arrays or somehow copy the above string to a vector in each individual string separated by the comma delimeter.

void Client::readClients() {
    string line;


    while (getline( this->clients, line ))
    {

        string interest_num_string, interest_string;

        istringstream clients( line );
        getline( clients, this->sex, ' ' );
        getline( clients, this->name, ',' );
        getline( clients, this->phone, ' ' );
        getline( clients, interest_num_string, ' ' );
        getline( clients, interest_string, '.' );

        this->interests = atoi(interest_num_string.c_str());

        cout << this->sex << "\n" << this->name << "\n" << this->phone << "\n" << interest_num_string << "\n" << interest_string;
    }

    this->clients.close();
}
3
  • Not sure what exactly is the problem here. You seem to be filling up your structure/class fields pretty fine with istringstream Commented Jun 11, 2012 at 1:35
  • on the interest_string I grab the line that ends with a . but the items before it are seperated by comas like "item, item, item." I was going to try and seperate these by the commas also. Im guessing I can run the same function for these and copy to the vector? Commented Jun 11, 2012 at 1:45
  • I think that's what he's asking since it seems to be the only piece missing. Commented Jun 11, 2012 at 1:52

3 Answers 3

2

Hint: an alternate signature for getline is

istream& getline ( istream& is, string& str, char delim );

strtok in C is also a viable option that isn't too brutal on low-level string manipulation.

Sign up to request clarification or add additional context in comments.

Comments

0

You could use a vector or other suitable container. You will need to create a "person" class that will contain all of the data you read in and place into the container.

void Client::readClients(std::vector<MyClass*>& myPeople)
{
    //  ... other parts of your code

    // Create a person
    pointerToPerson = new Person();

    // read them in
    getline(clients, pointerToPerson->field, ' ');

    // After you load a person just add them to the vector
    myPeople.push_back(pointerToPerson);

    // more of your code ...
}

Comments

0

Simple c++ piece of code :

  string s = "abc,def,ghi";
  stringstream ss(s);
  string a,b,c;
  ss >> a ; ss.ignore() ; ss >> b ; ss.ignore() ; ss >> c;    
  cout << a << " " << b << " " << c << endl;

Output :

abc def ghi

Comments

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.