0

I am currently working on pset2 - ceasar. I have written my code but for some reason I have getting an expected expression error, which itself not very explanatory as to what is wrong with my code and what the issue the compiler is facing while trying to compile my main function.

Would appreciate if someone here can provide guidance on what these errors imply.

Thank you. Bilal

The following is my code

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

//Reference to ceasar function
int ceasar (char Plaintext[], int key);

//Reference to validate_key function
bool validate_key(char key[]);

//Main function
int main(int argc, string argv[])
{
    if(argc !=2)
    {
      printf("Usage ./ceasar key\n");
      return 1;
    }
    else if (validate_key (argv[1]) == false)
    {
      printf("Usage ./ceasar key\n");
      return 1;
    }
    else
    {
      string Plaintext = get_string("Plaintext:");
      int k = (int) argv[1];


      printf("ciphertext: %i", ceasar(Plaintext, k));

      return 0;
    }
}

//ceasar function
int ceasar (char Plaintext[], int key)
    {
    int length = strlen(Plaintext);
    for(int i = 0; i < length; i++)
    {
        if(isalpha(Plaintext[i]) == 0)
        {
            printf("%i", Plaintext[i]);
        }
        if(isupper (Plaintext[i]))
        {
        printf("%i", 65 + ((Plaintext[i] + key) % 65));
        }
        else if (islower (Plaintext[i]))
        {
        printf("%i", 97 + ((Plaintext[i] + key) % 97));
        }
    }
    printf("\n");
    }

//validate_key function
bool validate_key(char key[])
{
    int digit = 0;

    int n = strlen(key);
    for (int i = 0; i < n; i++)
    {
        if(isdigit(key[i]) != 0)
        {
            digit++;
        }
    }
    if(digit == n)
    {
    return true;
    }
    else
    {
    return false;
    }
}

The error I am getting is the following:

~/ceasar/ $ help50 make ceasar
clang -ggdb3 -O0 -std=c11 -Wall -Werror -Wextra -Wno-sign-compare -Wno-unused-parameter -Wno-unused-variable -Wshadow    ceasar.c  -lcrypt -lcs50 -lm -o ceasar
ceasar.c:59:5: error: control reaches end of non-void function [-Werror,-Wreturn-type]
    }
    ^
1 error generated.
<builtin>: recipe for target 'ceasar' failed
make: *** [ceasar] Error 1

Asking for help...

ceasar.c:59:5: error: control reaches end of non-void function [-Werror,-Wreturn-type]

Ensure that your function will always return a value. If your function is not meant to return a value, try changing its return type to void.
~/ceasar/ $ 

I am not sure how the ceasar function is not returning a value because I have already put in all of the possible conditions here (i.e is the character a non-alphabetical character, then simply print that alphabet. If the character is an upper case alphabet, then convert is using the key. If the character is a lower case alphabet, then convert is using the key. Wouldn't all character's fall under this category - and hence, the ceaser function would return a value?

1

The ceasar function (defined here: int ceasar (char Plaintext[], int key) does not have any return statement. Since this is a function "with side effects", ie it is printing the ouptut, then it could be defined
void ceasar (char Plaintext[], int key). And don't forget the prototype definition.

3
  • Hi Dino, Thank you for your response. Mar 23 '20 at 23:13
  • Hi Dino, Thank you for your response. Currently the return type of ceasar is an int. However, I am considering changing it to a char. The reason for this is that since the Ceasar function is now returning characters, the return value would also be a char and therefore, I shouldn't have any issues invoking the ceasar function in the main function. is that a correct alternative approach? Also, regarding your comment on prototype definition, is it not enough that I have put the following right below the #include lines? int ceasar (char Plaintext[], int key); Mar 23 '20 at 23:36
  • The function as posted does not return anything. There is no return statement. It doesn't matter what you change it to as long as it returns something of that type (unless you change it to return type void as mentioned in the help response). I only mentioned the prototype because any change made to the function has to be also made to the prototype. Mar 24 '20 at 1:13

You must log in to answer this question.

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