0

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!

  • 3
    Aside: `if (scanf("%d",&CellValue) == 0)` should be `if (scanf("%d",&CellValue) != 1)` as you'll fail to detect when `EOF` was returned. But you should please post the [Minimal Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example) that shows the problem as anything could be responsible. – Weather Vane Nov 27 '19 at 19:59
  • Out of curiosity, what does `print_invalid_input` do? – edtheprogrammerguy Nov 27 '19 at 20:05
  • @edtheprogrammerguy I have totally rearranged my code to make it much more clear and minimal. Could you kindly take a look? –  Nov 27 '19 at 20:29
  • Please note that the [Minimal Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example) should be a complete program that can be copy-pasted in a single operation and then compiled (without warnings) and run! – Weather Vane Nov 27 '19 at 20:33
  • @WeatherVane Now it's fine, I forgot to add #include and combine the sections thanks for noting that –  Nov 27 '19 at 20:36
  • @John I can't reproduce your problem... have you changed the code? – andreee Nov 27 '19 at 20:38
  • Thank you, MSVC produces 1 warning, *warning C4013: 'FillMatrix' undefined; assuming extern returning int*. And 1 error, *error C2371: 'FillMatrix': redefinition; different basic types*. This is because you have not provided a function prototype for `FillMatrix` and the default asssumptions made by the compiler proved to be incorrect. – Weather Vane Nov 27 '19 at 20:40
  • nag, you need to declare `FillMatrix` or reorder the functions :P – andreee Nov 27 '19 at 20:41
  • I changed the order of the functions now you should get no error –  Nov 27 '19 at 20:43
  • 1
    When I re-order the functions, I can't reproduce. Possbily your compiler was not strict enough to refuse to compile, and the array argument passed was incorrectly used, corrupting something. In the default case it might have been passing a pointer truncated to a 32-bit `int` instead of a 64-bit pointer – Weather Vane Nov 27 '19 at 20:44
  • I have created a fresh new program and "Second" got printed on the screen for me. I am using MinGw compiler (Tested on both codeblocks and clion using c99 standard) –  Nov 27 '19 at 20:47
  • My friend have 64 bit version of MinGw installation and it worked fine for him, how could I solve this bug? I want to make sure that my code runs perfect on any computer –  Nov 27 '19 at 20:51

1 Answers1

2

If I am understanding your error correctly it seems that you get an error when you enter 1, which becomes the value for MatrixSize, and then when you enter 4.5. Your code will scan 4 for the value (and only value) in the first matrix since MatrixSize is 1. The function will then return true and FillMatrix will be called with the second matrix since there was no error detected. .5 is still present in stdin and that is where the error is produced (the first scanf in the call to FillMatrix for secondMatrix).

The problem your mentioning will only occur on the last input for your matrix. One way to avoid this problem would be to read in input as a string and check if that can be parsed as an integer.

mradey
  • 202
  • 1
  • 12
  • 1
    Good spot. I can reproduce that. 1 is the number of elements to enter, not the first data. The lesson for OP, is to provide prompts, as even a program's own author can be confused. – Weather Vane Nov 27 '19 at 20:55
  • why the function will return true, when I provide 4.5 as an input scanf will return 0 which means "Error" phrase should get printed and false should be returned. @WeatherVane –  Nov 27 '19 at 21:04
  • I think that has been explained well. The first function did not return error, the second one did. if you want the first function to return an error, enter `2 4.5` instead of `1 4.5` – Weather Vane Nov 27 '19 at 21:07
  • @WeatherVane Sorry, I rechecked that with clean code and it didn't enter my code it just went to else! but why is that? the user provided 4.5 which is not integer that means that scanf should return 0 –  Nov 27 '19 at 21:24
  • The `4` is an integer, and it fails on the next read, as explained here. – Weather Vane Nov 27 '19 at 21:26