Note:I have edited my code to provide clear and minimal steps to reproduce the error. I wrote the following code using C99 standrad:
#include <stdio.h>
#include <stdbool.h>
bool FillMatrix(int MatrixSize, int Matrix[][10])
{
int CellValue=0;
for (int i=0;i<MatrixSize;i++)
{
for (int j = 0; j < MatrixSize; j++)
{
if (scanf("%d",&CellValue) == 0)//Check if User provided un-integer value
{
printf("Error\n");
return false;//Her's the problem
}
else
{
Matrix[i][j] = CellValue;
}
}
}
return true;
}
int main()
{
int MatrixSize=0;
scanf("%d",&MatrixSize);
int FirstMatrix[10][10],SecondMatrix[10][10];
printf("First:\n");
if (!FillMatrix(MatrixSize,FirstMatrix)) return 0;
printf("Second:\n");
if (!FillMatrix(MatrixSize,SecondMatrix)) return 0;
printf("Hello");
return 0;
}
When I provide 1
as the first input and then 4.5
I can see Error
on the screen but also can see Second:
What is wrong with my code?
Before printing Second
I have asked to verify that FillMatrix(...)
didn't return false but still it gets printed anyway!