Search code examples
carguments

Create a Function that displays N or P depending on integer entered


Yo! so I'm starting to learn how to code on my own and for this exercise I have to create a function that displays ’N’ or ’P’ depending on the integer’s sign entered as a parameter. If n is negative, display ’N’. If n is positive or null, display ’P’. I've managed to do that and come up with a main that works. ALLOWED FUNCTION IS WRITE (no atoi etc)

void    ft_is_negative(int n)
{
    if (n >= 0)
    {
        write(1, "P", 1);
    }
    else
    {
        write(1, "N", 1);
    }
}

   int  main()
{
    int i;

    i = 200;
    ft_is_negative(i);
}

However I've been trying to get used to using arguments in my main but I've been struggling to get a working main when I use them. Here, with this one I simply get the carriage ("\n"), and I just can't figure out why (it's probably obvious but I can't see it). Thanks for the help in advance, if you don't directly want to give me the answer that's fine just point me in the right direction please.

void    ft_is_negative(int n)
{
    if (n >= 0)
    {
        write(1, "P", 1);
    }
    else
    {
        write(1, "N", 1);
    }
}

int main(int argc, char *argv[])
{
    if (argc != 2)
    {
        while (argv[1])
        {
            ft_is_negative(*argv[1]);
        }
    }
    write(1, "\n", 1);
    return 0;
}

Solution

    • if (argc != 2) is wrong. If you want to check if exactly 1 argument is given, it should be if (argc == 2).
    • while (argv[1]) is wrong. It may be an infinite loop because argv[1] is not updated in the loop.

    To check if the passed value is negative, you should check:

    • If the first character is -
    • There are character other than 0 after that
    • There are no invalid characters

    It can be done like this:

    int main(int argc, char *argv[])
    {
        if (argc == 2)
        {
            char* p = argv[1];
            int is_minus = 0, contain_nonzero = 0, contain_invalid = 0, contain_valid = 0;
            if (*p == '-')
            {
                is_minus = 1;
                p++;
            }
            else if (*p == '+')
            {
                p++;
            }
            for (; *p != '\0'; p++)
            {
                if ('0' <= *p && *p <= '9')
                {
                    contain_valid = 1;
                    if (*p != '0') contain_nonzero = 1;
                }
                else
                {
                    contain_invalid = 1;
                }
            }
            if (contain_invalid || !contain_valid)
            {
                /* contain invalid character or no character (except for sign) */
                write(1, "invalid", 7);
            }
            else if (is_minus && contain_nonzero)
            {
                /* contain minus sign and nonzero digit */
                ft_is_negative(-1);
            }
            else
            {
                /* contain digit, positive or zero (including minus zero) */
                ft_is_negative(0);
            }
        }
        write(1, "\n", 1);
        return 0;
    }