Skip to main content
Commonmark migration
Source Link

#Check the result of I/O functions

Check the result of I/O functions

This code may fail to initialize index:

int index;
scanf("%d", &index); // NOLINT

while (index < 1 || index > SIZE || current->values[index - 1] != EMPTY) {
    printf("Invalid position! Try again : ");
    scanf("%d", &index); // NOLINT
}

We need to use the return value from scanf() to determine whether index has been assigned. Also, if the input fails, there's no point re-trying if the stream is in error or at EOF:

size_t index;
int nread;
while ((nread = scanf("%zd", &index)) != 1)
       || !index || index > SIZE
       || current->values[index - 1] != EMPTY)
{
    if (nread == EOF) {
        /* serious error - indicate failure */
        return NULL;
    }
    printf("Invalid position! Try again : ");
}

Obviously, you'll need to adapt main() to handle a null return from take_input().

#Check the result of I/O functions

This code may fail to initialize index:

int index;
scanf("%d", &index); // NOLINT

while (index < 1 || index > SIZE || current->values[index - 1] != EMPTY) {
    printf("Invalid position! Try again : ");
    scanf("%d", &index); // NOLINT
}

We need to use the return value from scanf() to determine whether index has been assigned. Also, if the input fails, there's no point re-trying if the stream is in error or at EOF:

size_t index;
int nread;
while ((nread = scanf("%zd", &index)) != 1)
       || !index || index > SIZE
       || current->values[index - 1] != EMPTY)
{
    if (nread == EOF) {
        /* serious error - indicate failure */
        return NULL;
    }
    printf("Invalid position! Try again : ");
}

Obviously, you'll need to adapt main() to handle a null return from take_input().

Check the result of I/O functions

This code may fail to initialize index:

int index;
scanf("%d", &index); // NOLINT

while (index < 1 || index > SIZE || current->values[index - 1] != EMPTY) {
    printf("Invalid position! Try again : ");
    scanf("%d", &index); // NOLINT
}

We need to use the return value from scanf() to determine whether index has been assigned. Also, if the input fails, there's no point re-trying if the stream is in error or at EOF:

size_t index;
int nread;
while ((nread = scanf("%zd", &index)) != 1)
       || !index || index > SIZE
       || current->values[index - 1] != EMPTY)
{
    if (nread == EOF) {
        /* serious error - indicate failure */
        return NULL;
    }
    printf("Invalid position! Try again : ");
}

Obviously, you'll need to adapt main() to handle a null return from take_input().

Source Link
Toby Speight
  • 81.7k
  • 14
  • 101
  • 308

#Check the result of I/O functions

This code may fail to initialize index:

int index;
scanf("%d", &index); // NOLINT

while (index < 1 || index > SIZE || current->values[index - 1] != EMPTY) {
    printf("Invalid position! Try again : ");
    scanf("%d", &index); // NOLINT
}

We need to use the return value from scanf() to determine whether index has been assigned. Also, if the input fails, there's no point re-trying if the stream is in error or at EOF:

size_t index;
int nread;
while ((nread = scanf("%zd", &index)) != 1)
       || !index || index > SIZE
       || current->values[index - 1] != EMPTY)
{
    if (nread == EOF) {
        /* serious error - indicate failure */
        return NULL;
    }
    printf("Invalid position! Try again : ");
}

Obviously, you'll need to adapt main() to handle a null return from take_input().