0

I am currently working on pset2 cs50 2020. The Ceasar's cipher problem. I have managed the code for the first few steps and I think I know how to do the rest however I'm having an issue declaring strings. The code does not have an issue working with strings in regards to the command line arguments, however when I atempt to declare one so I can prompt the user for plaintext: it does not cooperate. The word string does not change color like intdoes and the compiler gives me the following error message when using help50 :

ceasar.c:39:32: error: incompatible integer to pointer conversion passing 'int' to parameter of type 'const char *' [-Werror,-Wint-conversion]
string plain = get_string(printf("plaintext:"));

By "incompatible conversion", clang means that you are assigning a value to a variable of a different type on line 39 of ceasar.c. Try ensuring that your value is of type pointer.

I do not know how to make sense of this and would appreciate it if someone could enlighten me. I'm new to c and dyslexic, so I understand it may be a stupid question to someone more experienced.
This is my code, (it works perfectly until I add the line string pl = get_string(printf("plaintext:") and the one after it:

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

int correct_input(string a);


int main(int argc,char* argv[])
{
    if (argc < 2 || argc > 2)
    {
        printf("Usage: ./caesar key\n");
    }

    if (argc == 2)//Checks for correct amount of CL arguments
    {
        int check = correct_input(argv[1]);

        if (check > 0)
        {
            int key = atoi(argv[1]);//converts the second command line argument into and int

            printf("Success!\n");

            if(key > 26)//loops larger key numbers around to the beginning of the alphabet
            {
                int sub = key / 26;
                key = key - sub*26;
            }


        }

    }
    string plain = get_string(printf("plaintext:"));
    printf("plainttext:%s\n, plain");
}  

int correct_input(string a)//checks whether the input is soley made up of decimal didgits
{
    int la = strlen(a);
    int check = 0;

        for(int i = 0; i < la; i++)
        {
           if(isdigit(a[i]) == 0)
           {
            check = 0;
            printf("Usage: ./caesar key\n");
           }
           if(isdigit(a[i]) != 0)
           {
             check++;
           }
        }
    return check;

} 



1
  • There's a problem with the quotation marks in this line: printf("plainttext:%s\n, plain");
    – Tritum
    Apr 22 '20 at 17:06
1

According to the man page, printf returns an int

Return value

Upon successful return, these functions return the number of characters printed (excluding the null byte used to end output to strings).

This printf("plaintext:") returns 10 (number of characters printed).

get_string expects a string (defined as char *) as it's argument.

You should have success it you sent "plaintext: " as the argument instead of printf("plaintext:")

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .