-3

This is the tester function:

intA = intB = 0;

printf("TEST-2: ");
intA = getIntPositive(NULL);
if (intA == TEST_INT)
{
    printf("<PASSED>\n");
}
else
{
    printf("<!!! FAILED !!!>\n");
    fail++;
}

This is the function I have written to get positive integer and return it:

int getIntPositive(int *num) {
do
{       
    scanf("%d", num);

    if (*num <= 0)
    {
        printf("ERROR: Enter a positive value:");
    }
} while (*num <= 0);
return *num;
}
Jabberwocky
  • 48,281
  • 17
  • 65
  • 115
  • 1
    You *must* check the function return value of `scanf()` too (before you check the value that was entered) and remove any bad input that is blocking the read. Such as the non-numeric entry, say, `abc` which will remain in the input until you take steps to remove it. – Weather Vane Mar 19 '21 at 08:54
  • Please [edit] and show a [mcve] as well as some samples if input and expected vs actual output – Jabberwocky Mar 19 '21 at 08:56
  • 1
    Are you aware that calling `getIntPositive` with a `NULL` argument won't end well? – Jabberwocky Mar 19 '21 at 08:58
  • 1
    Why pass a pointer to the target *and* return the value too? – Weather Vane Mar 19 '21 at 09:01

1 Answers1

0

The test you show is incomplete (it does not fully test the function), and what you show of it is incomplete (you do not show the input provided to the function). You also do not show the specification of the function (a description of how it is supposed to behave).

What we can tell from the test code you show is:

  • The function is passed a pointer parameter.
  • In a certain test case with input unknown to us and a null pointer argument, the function is expected to return a value TEST_INT.

From the source code you show for getIntPositive, we can tell it likely fails this test case because it uses the parameter it is given to access memory even though it is given a null pointer. For scanf("%d", num);, scanf must be passed a pointer to an int, not a null pointer. And, in if (*num <= 0), the behavior of using *num when num is a null pointer is not defined by the C standard.

One way to fix this is simply to ignore the parameter and use another int to get a value from the input stream and return it:

int getIntPositive(int *num)
{
    do
    {       
        int x;
        scanf("%d", &x);

        if (x <= 0)
        {
            printf("ERROR: Enter a positive value:");
        }
    } while (x <= 0);
    return x;
}

This code might pass that particular test. However, several questions remain unanswered:

  1. What is the code supposed to do when the parameter is a null pointer? What is it supposed to do when the parameter is not a null pointer?
  2. Is the function supposed to loop until it gets a positive value?
  3. What should the function do if there is an error (including end-of-file) in the input stream or a matching failure in scanf (there is input, but it does not match what a decimal numeral should look like).

On question 1, some functions that accept a pointer and return a value provide the value both as the function return value and in the pointed-to-place, and, if the pointer is null, they skip that part. If so, the code above could be modified at the end:

    if (num)
        *num = x;
    return x;
}

However, another possibility is the scanned value is supposed to be returned in *num, if num is non-null, and the return value is supposed to be a status code. Perhaps it should be 0 if a number was scanned successfully or TEST_INT if there was a matching failure or something else if there was an input stream error.

Similarly, on questions 2 and 3, we do not know. Writing a correct function requires having a specification of what the function is supposed to do. We do not even know what the input is in this test case. Is it an empty input stream, at end-of-file? Is it input with some negative numerals followed by a positive numeral? Is it input that does not match a decimal numeral at all? There is critical information missing from what you have shown about the test case.

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312