Hi everyone!
We were assigned a project not too long ago that asks us to ask a user to input numbers into a matrix. The user also chooses its dimensions.
My questions is how would I incorporate a validation for the dimensions. Obviously, the only valid dimensions are numerical so I want an error to display if the user types a letter or a special character such as apostrophe or backslash.
My thoughts:
I was thinking that I could have it check the ASCII numerical value and limit those that range from 48 - 57 (numbers 0 - 9) but I cannot seem to get this to work. I would want the number that the ASCII code refers to be used as the dimensions not (48 - 57). Maybe there is an easier way to do this?
Code:
int main(void)
{
//Declare necessary variables
int rowSize = 0;
int columnSize = 0;
do
{ //Beginning of column while loop
printf("Please enter the number of columns: ");
scanf("%d", &columnSize);
//Verify column size
if (columnSize > 10)
printf("ERROR: The maximum number of columns is 10.\n");
else if(columnSize < 0)
printf("ERROR: Invalid number of columns. Must be greater than 0.\n");
}
while (columnSize > 10 || columnSize < 0);
... Same for the row of the matrix.
Thanks for any and all help!