'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();
}
Solution 1:[1]
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.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Remy Lebeau |
