Home > Mobile >  incompatible pointer to integer conversion in caesar cipher
incompatible pointer to integer conversion in caesar cipher

Time:08-28

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

CodePudding user response:

The function rotate takes a single character as an argument and returns the corresponding rotated character.

However, in the line

rotate(plaintext,n);

you are attempting to pass an entire string (i.e. several characters) instead of a single character. You are not allowed to do this.

If you want all characters in the string to be rotated, then you must call the function rotate once for every character, by using a loop:

printf( "ciphertext: " );

int len = strlen( plaintext );

for ( int i = 0; i < len; i   )
{
    char c;

    c = rotate( plaintext[i], n );

    printf( "%c", c );
}

printf( "\n" );

CodePudding user response:

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;
}
  • Related