Home > Net >  enforce sscanf input value to be unqiue in c
enforce sscanf input value to be unqiue in c

Time:08-11

Code link: https://godbolt.org/z/dxn38f53h
Trying to replicate/understand this post: https://stackoverflow.com/a/47587807/15603477.
The issue is that teams can be duplicated.
Trying to find a way that check sscanf input value is unique compare to all of previous input (ignore default value 0).

Expected result: when stdin input (team number) value is duplicated make the function input reset to "begin":

printf("\nEnter your team: ");

while x>=1 block that part is so far what I tried.

void input(int a[NROW][NCOL], int teams[NROW])
{
    int x, y;
    for (x = 0; x < NROW; x  ) {
        for (;;) {
            int rtn;
            printf("\nEnter your team: ");
            rtn = scanf("%d", &teams[x]);      
            while (x >= 1) {
                printf("x=\t%d\n", x);
                for (int j = 1; j <= x; j  ) {
                    if (teams[j] == teams[j - 1]) {
                        fprintf(stderr,"duplicate team number.\n");
                        // x = x   1;
                        printf("x=%d\n", x);
                        break;
                    } else {
                        break;
                    }
                }
                break;
            }
            if (rtn == 1)
                break;
            else if (rtn == EOF) {
                fprintf(stderr, "user canceled input.\n");
                exit(EXIT_FAILURE);
            }
            fprintf(stderr,"error: invalid input.\n");
            empty_stdin(); 
        }

        for (y = 0; y < NCOL; y  ) {
            for (;;) {
                int rtn;
                printf("Enter team[%d] score [%d]: ",teams[x],y 1);
                rtn = scanf("%d", &a[x][y]);

                if (rtn == 1)
                    break; 
                else if (rtn == EOF) {
                    fprintf(stderr, "user canceled input.\n");
                    exit(EXIT_FAILURE);
                }
                fprintf(stderr, "error: invalid input.\n");
                empty_stdin();
            }
        }
    }
}

CodePudding user response:

The loop trying to locate duplicates is incorrect: you compare teams[j] and teams[j - 1] instead of comparing teams[j] and teams[x] and the range for j should include 0.

Furthermore, you perform the scan even of scanf() failed to read a team number.

You should first test for valid input, then check for a duplicate, then accept the input if no duplicate was found.

Here is a modified version:

void input(int a[NROW][NCOL], int teams[NROW])
{
    int x, y;
    for (x = 0; x < NROW; x  ) {
        for (;;) {
            int rtn, j;
            printf("\nEnter your team: ");
            rtn = scanf("%d", &teams[x]);    
            if (rtn != 1) {
                if (rtn == EOF) {
                    fprintf(stderr, "user canceled input.\n");
                    exit(EXIT_FAILURE);
                }
                fprintf(stderr,"error: invalid input.\n");
                empty_stdin(); 
                continue;
            }
            for (j = 0; j < x; j  ) {
                if (teams[j] == teams[x])
                    break;
            }
            if (j < x) {
                fprintf(stderr, "duplicate team number.\n");
                continue;
            }
            break;
        }

        for (y = 0; y < NCOL; y  ) {
            for (;;) {
                int rtn;
                printf("Enter team[%d] score [%d]: ", teams[x], y 1);
                rtn = scanf("%d", &a[x][y]);
                if (rtn == 1)
                    break; 
                if (rtn == EOF) {
                    fprintf(stderr, "user canceled input.\n");
                    exit(EXIT_FAILURE);
                }
                fprintf(stderr, "error: invalid input.\n");
                empty_stdin();
            }
        }
    }
}
  •  Tags:  
  • c
  • Related