Matthew Mohaupt
Matthew Mohaupt

Reputation: 1

is there any way to pass multiple arguments in a class just by using a single string variable in C++

I am trying to take in a lot of data from multiple files and I use getline() to take the entire line to put it into a temporary string value and turn the data from the line into an object(everything is already sorted).

void charts(vector<Song> &data, int menuop){
  ifstream stream;
  if(menuop == 1){
    stream.open("charts_top50_1960_1980.csv");
    assert(stream.fail() == false);
    data.clear();
  }
  else if(menuop == 2){
    stream.open("charts_top50_1981_2000.csv");
    assert(stream.fail() == false);
    data.clear();
  }
  else if(menuop == 3){
    stream.open("charts_top50_2001_2020.csv");
    assert(stream.fail() == false);
    data.clear();
  }
  string tempstring;
  while(getline(stream, tempstring)){
    for(int i = 0; i < tempstring.length(); i++){
      if(tempstring[i] == '/'){
        tempstring[i] == ',';
      }
    }
    //cout << tempstring << endl;
    Song tempsong(const &tempstring);
    data.push_back(tempsong);
  }
  stream.close();
}

Upvotes: 0

Views: 123

Answers (1)

Remy Lebeau
Remy Lebeau

Reputation: 598414

Simply change:

Song tempsong(const &tempstring);

to:

Song tempsong(tempstring);

And then make sure Song has a constructor that takes a const string& parameter. Then you can have that constructor split the string on ',' characters and use the substrings as needed. There are plenty of other questions on StackOverflow that explain how to do that, for instance:

Parse (split) a string in C++ using string delimiter (standard C++)

Splitting a C++ std::string using tokens, e.g. ";"

How do I iterate over the words of a string?

Just to point out a few.

Upvotes: 1

Related Questions