dimanche 23 juillet 2017

C program for assigning elements of a matrix without letters

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;

       }

    }

}

the code continues from here.

Aucun commentaire:

Enregistrer un commentaire