Search code examples
arrayscpointersnullchar

I am running a conditional loop to iterate through an Array to validate that the user only entered digits. Error when I make the comparison to NULL


As the title says. I am making a program where it takes in command line argument from the user and it encrypts the message. Before the user enters the message, they are required to make an 'encryption' key which should only contain numbers.

So before anything I have to check that they have only entered numbers.

To my understanding, argv[] can only be an array that stores string values.

So to prevent run time error, I can not do int key = argv[1] or else this would run into an error if it's a string.

Now here is my code for checking if the argument entered by the user is valid.

#include<stdio.h>
#include<cs50.h>
#include<string.h>
#include<stdlib.h>
#include<ctype.h>





bool verification(string array[]);

// ask the user for a key.

int main(int argc, string argv[])
{
    if(argv[2] != NULL)
    {
        printf("Usage: ./caesar key");
        return 1;
    }
    else if(verification(argv) == false)
    {
        printf("Usage: ./caesar key");
        return 1;
    }
    else if(verification(argv) == true)
    {
        printf("works");
    }

}

//verification for all character is a digit
bool verification(string array[])
{
    int i = 0;
    bool status = true;
    while(array[1][i] != NULL || status == true )
    {
        if(array[1][i] <= '9' && array[1][i] >= '0')
        {
            status = true;
            i += 1;
        }
        else
        {
            status = false;
        }
    }
    return status;
}

// ask for the plain text



// rotate the message to the assigned key letter
// use the formula Ci = (Pi + k) % 26





// display the text

I will now explain this part of the code:

int i = 0;
    bool status = true;
    while(array[1][i] != NULL || status == true )

the variable i is the index number which will be used to iterate through the array. by using array[1][i] I am iterating through all the character in the first data of the array. To my understanding, at the end of all strings (which apparently can be treated like arrays) there is a \0 or so called NULL. So I wanted to keep running the loop until either the program encounters a letter (which will set status to false) or they have iterated through the last character and encounted \0.

However this doesn't seem to work which is quite fustrating as if(argv[2] != NULL) this line at the top of the program does actually work.

The error I get is as follows when I try to make the program:

error: comparison between pointer and integer ('char' and 'void*')

I have also tried to swap the NULL with "\0" with the quote mark and without. Still receiving similar/ same errors.

(apologies for bad grammar and / or bad English. because its not my first language)


Solution

  • Instead of array[1][i] != NULL use array[1][i] != '\0', array[1][i] != 0 or while(array[1][i])
    Also check argc for the number of arguments.

    #include<stdio.h>
    #include<stdbool.h>
    
    #define string char*
    
    bool verification(string array[]);
    
    int main(int argc, string argv[])
    {
        if(argc != 2)
        {
            printf("Usage: ./caesar key\n");
            return 1;
        }
        else if(verification(argv) == false)
        {
            printf("Usage: ./caesar key\n");
            return 1;
        }
        else if(verification(argv) == true)
        {
            printf("works\n");
        }
    
    }
    
    //verification for all character is a digit
    bool verification(string array[])
    {
        int i = 0;
        while(array[1][i])
        {
            if(array[1][i] <= '9' && array[1][i] >= '0')
            {
                ++i;
            }
            else
            {
                return false;
            }
        }
        return true;
    }