C program for assigning elements of a matrix without letters

344 Views Asked by At

I'm having trouble outputting an invalid statement if the user inputs a letter instead of a number into a 2D array.

I tried using the isalpha function to check if the input is a number or a letter, but it gives me a segmentation fault. Not sure what's wrong any tips?

the following code is just the part that assigns the elements of the matrix.

#include <stdio.h> 
#include <stdlib.h>
#include <ctype.h>

#define MAX 10

void display(int matrix[][MAX], int size);

int main() {
    int n, degree;
    int matrix[MAX][MAX];

    printf("Enter the size of the matrix: ");  // assigning size of the matrix
    scanf("%d", &n);

    if (n <= 1 || n >= 11) {  // can't be bigger than a 10x10 matrix
        printf("Invalid input.");
        return 0;
    }

    for (int i = 0; i < n; ++i) {  // assigning the elements of matrix
        printf("Enter the row %d of the matrix: ", i);
        for (int j = 0; j < n; ++j) {
           scanf("%d", &matrix[i][j]);

           if (!isalpha(matrix[i][j])) { // portion I'm having trouble with
               continue;
           } else {
               printf("Invalid input.");
               return 0;
           }
        }
    }
    ...
5

There are 5 best solutions below

0
On

You must check the return value of scanf(): It will tell you if the input was correctly converted according to the format string. scanf() returns the number of successful conversions, which should be 1 in your case. If the user types a letter, scanf() will return 0 and the target value will be left uninitialized. Detecting this situation and either aborting or restarting input is the callers responsibility.

Here is a modified version of your code that illustrates both possibilities:

#include <stdio.h> 

#define MAX 10

void display(int matrix[][MAX], int size);

int main(void) {
    int n, degree;
    int matrix[MAX][MAX];

    printf("Enter the size of the matrix: ");  // assigning size of the matrix
    if (scanf("%d", &n) != 1 || n < 2 || n > 10) {
        // can't be bigger than a 10x10 matrix nor smaller than 2x2
        // aborting on invalid input
        printf("Invalid input.");
        return 1;
    }

    for (int i = 0; i < n; i++) {  // assigning the elements of matrix
        printf("Enter the row %d of the matrix: ", i);
        for (int j = 0; j < n; j++) {
            if (scanf("%d", &matrix[i][j]) != 1) {
                // restarting on invalid input
                int c;
                while ((c = getchar()) != '\n') {
                    if (c == EOF) {
                        printf("unexpected end of file\n");
                        return 1;
                     }
                }
                printf("invalid input, try again.\n");
                j--;
            }
        }
    }
    ...
0
On

Try this:

if (isalpha (matrix[i][j])) { 
    printf ("Invalid input.");
    return 0;
}
2
On

As the value of n will be number, we can solve it using string instead of int.

char num[10];
int n;
scanf("%s", num);
if(num[0] < '0' || num[0] > '9' || strlen(num) > 2){
    printf("invalid\n");
}
if(strlen(num) == 1) n = num[0] - '0';
if(strlen(num) == 2 && num[0] != 1 && num[1] != 0) printf("invalid\n");
else n = 10;

Also we can use strtol() function to convert the input string to number and then check for validity.You can check the following code for it. I have skipped the string input part. Also you have to add #include<stdlib.h> at the start for the strtol() function to work.

char *check;
long val = strtol (num, &check, 10);    
if ((next == num) || (*check != '\0')) {
        printf ("invalid\n");
}
if(val > 10 || val < 0) printf("invalid\n");
n = (int)val; //typecasting as strtol return long
1
On

So if anyone in the future wants to know what I did. here is the code I used to fix the if statement. I am not expecting to put any elements greater than 10000 so if a letter or punctuation is inputted the number generated will be larger than this number. Hence the if (matrix[i][j] > 10000). May not be the fanciest way to do this, but it works and it's simple.

for (int i = 0; i < n; ++i) {  // assigning the elements of matrix

    printf("Enter the row %d of the matrix: ", i);

    for (int j = 0; j < n; ++j) {


    scanf("%d", &matrix[i][j]);

    if (matrix[i][j] > 10000) { // portion "fixed" 

       printf("Invlaid input");

       return 0;

       }

    }

}

I used a print statement to check the outputs of several letter and character inputs. The lowest out put is around and above 30000. So 10000 I think is a safe condition.

0
On

The isdigit() library function of stdlib in c can be used to check if the condition can be checked.