Q:

scanf reading formatted input

I am trying to reading an input of the sort:
[Some text] (x,y)

where i need to store x,y in integers.
I tried this following code:
#include<iostream>
#include <cstdio>
using namespace std;

int main(){
    char temp[20];
    int x1, x2;
    scanf("%[^(]%d,%d)", temp, &x1, &x2);
    printf("%s %d %d", temp,x1,x2);
    return 0;
}

But the integers stored in x1 and x2 are always 0. This is the output i get:
this is a trial (8,6)
this is a trial  0 0

What is the error?

A:

%[^(]%d,%d)

this tells scanf() to:

Read all characters which aren't a left (opening) parenthesis;
Then read a decimal integer,
then read a comma,
then read another decimal integer,
then consume the trailing closing parenthesis.

What's missing is that after reading the leading text, you don't actually read the opening paren. So, either change your format string to include that:
%[^(](%d,%d)

Or, even better, consider parsing the string manually. scanf() format strings are obscure and it's easy to make one slight mistake and then the entire thing goes boom (as just happened to you). How about this instead?
char buf[LINE_MAX];
fgets(buf, sizeof(buf), stdin);
const char *lparen = strchr(buf, '(');
const char *comma = strchr(lparen + 1, ',');
// const char *rparen = strchr(comma + 1, ')'); // is this even needed?

char str[lparen - buf + 1];
memcpy(str, buf, lparen - buf);
str[lparen - buf] = 0;
int n1 = strtol(lparen + 1, NULL, 10);
int n2 = strtol(comma + 1, NULL, 10);

Some demo for goodness sake...

