Search code examples
ccs50caesar-cipher

incompatible pointer to integer conversion in caesar cipher


I'm trying to create a "Caesar cipher". I was hoping my code was finally complete however I ran into some errors while trying to run the code. I'm a complete beginner with C so I suspect I'm just not correctly calling a function BUT there will probably be more issues with my code. Here it is:

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

bool only_digits(string s);
char rotate(char c, int n);

int main(int argc, string argv[])
{
    if ((argc == 2) && only_digits(argv[1]) == true)
    {
        //convert string from argv[1] into int
        int n = (atoi(argv[1]));
        string plaintext = get_string("plaintext: ");
        rotate(plaintext,n);
        printf("ciphertext: %s \n", plaintext)
    }
    else
    {
        printf("usage: ./caesar key\n");
    }
}

bool only_digits(string s)
{
    size_t n = strlen(s);
    for (size_t i = 0; i < n; i++)
    {
        if (!isdigit(s[i]))
        {
            return false;
        }
    }

    return true;
}

char rotate(char c, int n)
{
    if (c >= 65 && c <= 90)
    //establish capital letter
    {
        while (n>0)
        {
            (c++ && n--);
            if (c > 90)
            {
                (c ==65)
            }
        }
        return c;
    }
    if (c >= 97 && c <= 122)
    //establish lowercase
    {
        while (n>0)
        {
            (c++ && n--);
            if (c > 122)
            {
                (c == 97)
            }
        }
        return c;
    }
    if (c >= 48 && c <= 57)
    //establish number
    {
        while (n>0)
        {
            (c++ && n--);
            if (c > 57)
            {
                (c == 48)
            }
        }
        return c;
    }
    else
    {
        if (n=0)
        {
            continue
        }
    }
}

I was getting multiple errors, some of which I already resolved. This is the latest error:

caesar.c:17:16: error: incompatible pointer to integer conversion passing 'string' (aka 'char *') to parameter of type 'char'; dereference with * [-Werror,-Wint-conversion]
        rotate(plaintext,n);
               ^~~~~~~~~
               *
caesar.c:8:18: note: passing argument to parameter 'c' here
char rotate(char c, int n);
                 ^
fatal error: too many errors emitted, stopping now [-ferror-limit=]
2 errors generated.
make: *** [<builtin>: caesar] Error 1

Solution

  • The error is pretty self-explanatory. The rotate function expects a char but you are providing a string which is just a typedef for char *.

    Given the way it's used, I suspect you want to rotate each character in the input string using a loop.

    A few other notes:

    char is a numeric type. Rather than using 97 in your code, for instance, you can use the char literal 'a'.

    It's common convention to put any conditions that would cause termination of your program upfront and return with a non-zero exit code.

    int main(int argc, string argv[])
    {
        if (argc != 2 && !only_digits(argv[1]))
        {
            printf("usage: ./caesar key\n");
            return 1;
        }
    
        // convert string from argv[1] into int
        int n = (atoi(argv[1]));
        string plaintext = get_string("plaintext: ");
        rotate(plaintext,n);
        printf("ciphertext: %s \n", plaintext);
    
        return 0;
    }