Parsing data from STDIN

Hey, I'm unsure of how to go about this.

I'm taking data from stdin, this can either be a comment or a command with a value associated, so for instance I could have:

1
2
3
/this is a comment because it begins with / so it should be ignored

input key (where key is the value and input is the command associated with the value)


I'm storing the command as a string and the key as an int and I do something with them, like this:

1
2
3
4
5
6
string command; int key;
while(cin)
  cin >> command;
  cin >> key;
  fn(command,key)
}


What I'm confused about is how to ignore lines that start with a certain character.

Thanks for any help.
Use the string::find() function.
1
2
#include <limits>
#include <string> 
1
2
3
4
5
6
7
8
9
10
11
while (cin)
{
  cin >> command;
  if (command.find( "/" ) == 0)  // skip comments
  {
    cin.ignore( numeric_limits <streamsize> ::max(), '\n' );
    continue;
  }
  cin >> key;
  fn(command,key);
}

Hope this helps.
Topic archived. No new replies allowed.