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?