JDillon522
JDillon522

Reputation: 19686

C - my function backfires

When I call this function it always returns false and stops the program. But I cant figure out why.

// checks to see if the second argument is a positive interger, 
// and that there is only one arg
    if (ArgCheck (argc, shift) == false)
   {
       printf("You may only input one non-negative argument.\n");
       return 1;
    }
    else
        return true;  

The function ArgCheck is:

int ArgCheck (int a, int b)
{
  if (a > 2)
        {
        return false;
        }
  else if (b < 0)
        {
        return false;  
        }
  else
    return 0;  
}

Even when I enter in the proper arguments in the command line, it still returns false. I know its a noob problem but I appreciate the help.

Thanks.

Upvotes: 0

Views: 75

Answers (1)

Ed Swangren
Ed Swangren

Reputation: 124732

int ArgCheck (int a, int b)
{
  if (a > 2)
        {
        return false;
        }
  else if (b < 0)
        {
        return false;  
        }
  else
    return 0;  
}

This function returns false at all times. 0 is the same as false. Your requirement is:

return true if the second argument is a positive integer and that there is only one arg

So...

bool ArgCheck(int argCount, int value)
{
    return argCount == 1 && value >= 0;
}

Note that I do not allow an argument count of 0 because you say that one argument is required. Your current code allows no args.

Upvotes: 1

Related Questions