> I know the easiest way is just create a char and buffer it in,
> but is there a more "industry professional" way to do it?
The "industry professional" way to do something is the simplest, the most transparent way in which it can be done - in this case (if the string does not contain embedded white space), create a char and read the comma into it (and if validation is required, verify that it indeed was a comma).
> I use getline to read the whole thing, how can I split the string to remove the comma?
We can specify comma as the delimiter and call getline repeatedly to read in each part without the comma.
For instance: std::getline( stm, str, ',' ) ;
If I use the getline(stm, str, ',') in a while loop, then str would contain after each iteration:
Test x1
Test x2
Test x3
Text x4
Test xn
However I want "Test" to be stored in a different variable than "x1" and this getline would put Test & x1 in the same variable.
If I wanted "Test" and "x1" to be stored in two seperate variables instead of one, is the best way to split str and then put the split string into the 2 variables?
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
usingnamespace std;
string blankPunctuation( string line, string punctuation )
{
for ( char &c : line )
{
if ( punctuation.find( c ) != string::npos ) c = ' ';
}
return line; // original passed by value, so not changed
}
int main()
{
const string punctuation = ",;";
string line, text;
int number;
// ifstream in( "infile.txt" );
// getline( in, line );
getline( cin, line );
line = blankPunctuation( line, punctuation ); // change punctuation to white space (note: incl. at end)
stringstream ss( line );
while ( ss >> text >> number ) cout << text << " " << number << endl;
}