You could cut your string at the first '#'
(if it's present) at the time you construct your istringstream
. This would let you simplify the rest of your logic by pretending that the '#'
is never there:
while(getline(pgmFile, temp))
{
size_t pos = temp.find('#');
istringstream readIn(pos == string::npos ? temp : temp.substr(0, pos));
lines++;
...
}
Since you read line-by-line, and because the delimiter is discarded, you can safely skip the check for the '\n'
character, too: it's not going to be there.