1

I have a while(inFile >> word){ loop and a text file with 10 lines:

symbol instruction #123, $456 ;a bunch of comments

my question is, how can I ignore anything after the semicolon and skip to next line? Essentially,

if(word.find(";"))
{
//move along
}
SPlatten
  • 5,334
  • 11
  • 57
  • 128
Tyler
  • 13
  • 3

1 Answers1

3

Use a different approach - first read whole line, then strip the comments after ; and then use the rest of the line.

std::string line;
while (std::getline(infile, line)) {
    const auto commentStart = line.find(';');
    if (commentStart != std::string::npos)
    {
        line.erase(commentStart); //strip comments
    }
    ... //use the line somehow
}

If you want to have separate words after line was read, you can use e.g. std::stringstream (but there are many different options):

std::stringstream ss(line);
std::string word;
while(ss >> word) {
    //use single word
}
Yksisarvinen
  • 18,008
  • 2
  • 24
  • 52