I have a program that needs two integers to be entered to set up a 2d array and if you enter a letter then the program does not work.
Is there any way to check if it is a number that has been entered or to limit what is pressed on the key board to only numbers?
Once the number has been entered it is scanned into a variable to set up the array.
Here is the example of code:
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
int x;
int y;
int main(void){
int array[x][y];
int i;
int j;
srand(time(NULL));
printf("Please Enter Size X Of Array:");
scanf("%d",&x);
printf("\n");
printf("please Enter Size Y Of Array:");
scanf("%d",&y);
printf("\n Randomised Numbers Table \n\n");
for (i = 0; i < x; i++){
for (j = 0; j < y; j++){
array[i][j] =(rand()%10000);
printf("%d \t", array[i][j]);
The best way to deal with this is to loop until you have valid input. With bash, you can use the regular expression operator to make sure the string is valid before proceeding.