C: How can I make it so scanf() input has one of two formats?

It far better to use char buf[big_enough * 2]; fgets(buf, sizeof buf, stdin) to read the line and then parse it, perhaps with sscanf(buf, " { [ %lf ... and sscanf(buf, " { %lf ....


Yet if code is obliged to stay with scanf():

OP’s first scanf(" { [ %lf ... consumes the '{' expected in the 2nd scanf( " { %lf ...

Instead:

if(scanf(" { [ %lf ; %lf  ] , [ %lf ; %lf  ] , [ %lf ; %lf  ] }%c", 
        &A[0], &A[1], &B[0], &B[1], &C[0], &C[1], &s) != 7 && s != '\n') {
    s=" ";

    //    no  {
    //        v
    if(scanf(" %lf , %lf , %lf }%c", &a, &b, &c, &s) != 4 && s != '\n') {
        printf("error\n");
        return 1;
    }

}

Preferred fgets() way:

// Form a reasonable, yet generous buffer
#define I (50 /* rough estimate of characters use to read a double, adjust as needed */)
//                          { [ 1 ; 1 ] , [ 3 ; 1 ] , [ 2 ; 2 ] }\n\0
#define LINE_SIZE_EXPECTED (4 + I+3+I  +7  +I+3+I  +7  +I+3+I+6)
char buf[LINE_SIZE_EXPECTED * 2]; // Lets us use 2x for extra spaces, leading zeros, etc.

if (fgets(buf, sizeof buf, stdin)) {
  // Consider using "%n" to detect a complete scan and check for no trailing junk
  int n = 0;
  sscanf(buf, " { [ %lf ; %lf  ] , [ %lf ; %lf  ] , [ %lf ; %lf  ] } %n",
      &A[0], &A[1], &B[0], &B[1], &C[0], &C[1], &n);
  if (n && buf[n] == '\0') {
    // successful scan
  } else {
    n = 0;
    sscanf(" { %lf , %lf , %lf } %n", &a, &b, &c, &n);
    if (n && buf[n] == '\0') {
      // successful scan
    } else
      // both scans failed
    }
  }
}

Leave a Comment