Search code examples
istringstream

sscanf to istringstream confusion c++


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.


Solution

  • 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;
    }