How can I fix any letter or overall character input in integer input

Advertisements

So basically I have a simple code where im working with square matrix, where either the matrix is generated randomly, or the user can input the numbers in matrix, but when i enter the size of it for example in this do-while:

 do {
                     printf("Define the size of the matrix: ");
                     scanf("%d", &n);
                         } while (n <= 1);  

the cycle will just loop till I enter any number bigger than 1, but when I enter any letter, or character besides number, it gets stuck in infinite loop.

I have the same problem in my other function where the user inputs the number in matrix:

void input_matrix(double* matrix, int n) {
    for (int i = 0; i < n; i++) 
    {
        for (int j = 0; j < n; j++) 
        {
            printf("Define the value of %d. row a %d. columm:\t", j+1 ,i+1);
            scanf("%lf", &matrix[i*n+j]);
        }
    }
}

I don’t know how to fix it, is there any easy way to fix my problem?

(sorry for my bad english, not a native speaker).

I tried looking on the internet for the answer, found it, but didn’t know how to implement it in my code.

>Solution :

1.The reason why your program gets stuck in an infinite loop when a non-numeric character is entered is that scanf() fails to read an integer value from the input stream when it encounters a non-numeric character. The non-numeric character is left in the input stream, waiting to be read by the next input function.

In your case, the loop condition n <= 1 will always evaluate to true if scanf() fails to read an integer value, which causes the loop to run indefinitely.

To fix this issue, you can add error handling to your code to handle the case where scanf() fails to read an integer value. One way to do this is to check the return value of scanf() to see if it successfully read an integer. If scanf() returns a value other than 1 (the number of input items successfully matched and assigned), then you can clear the input stream using getchar() and prompt the user to enter a valid input again.

Here is code:

#include <stdio.h>

int main() {
    int n;
    char c;

    do {
        printf("Define the size of the matrix: ");
        if (scanf("%d", &n) != 1) {
            // Clear input stream
            while ((c = getchar()) != '\n' && c != EOF) {}
            printf("Invalid input. Please enter a valid integer.\n");
            continue;
        }
    } while (n <= 1);

    printf("Matrix size: %d\n", n);

    return 0;
}
  1. To handle non-numeric input in your input_matrix() function, you can use the same approach as in the previous solution. You can check the return value of scanf() to see if it successfully read a double value. If scanf() returns a value other than 1, then you can clear the input stream using getchar() and prompt the user to enter a valid input again.

Here is code:

void input_matrix(double* matrix, int n) {
    char c;

    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            printf("Define the value of %d. row a %d. columm:\t", j+1 ,i+1);

            while (scanf("%lf", &matrix[i*n+j]) != 1) {
                // Clear input stream
                while ((c = getchar()) != '\n' && c != EOF) {}
                printf("Invalid input. Please enter a valid number.\n");
                printf("Define the value of %d. row a %d. columm:\t", j+1 ,i+1);
            }
        }
    }
}

Leave a ReplyCancel reply