0

I want to convert the following C code to C++ utilizing istringstream:

void test(char *s) {
   int i;
   sscanf(s, "%*s%d", &i);
}

What I have so far is:

void test(char *s) {
   int i;
   istringstream iss(s);
   iss >> s >> i;
}

It comes up with errors. I'm too foreign to istringstream, and can't figure out what to do to fix it. I'm hoping for some insight into my mistake. s is supposed to be a string and integer with no spaces (e.g. Good123) and I want to remove the integer and place it into i.

1 Answers1

0

For starters, you should use a different variable to store the resulting parsed string.

void test(char *s) {
   int i;
   string s1;
   istringstream iss(s);
   iss >> s1 >> i;
}
fons
  • 4,905
  • 4
  • 29
  • 49
  • Tried that, but then it stores the contents of s into s1, but no int is being stored in i. – user1820560 Feb 14 '13 at 05:15
  • 1
    Problem is that, contrary to sscanf, istringstream expect a whitespace separator. It is only well suited to very simple parsing (even simpler than what sscanf offers as it seems). I would use a regular expression. – fons Feb 14 '13 at 05:18