So I am writing a cipher program as part of the Harvard CS50 course and I have this code to check if a character + the cipher key will put it out of ASCII alphabetical range and to loop it back around if it does (as well as preserving case). The array types being cycled through are 'char' (in plainText) and 'int' (in cipherArray)
if (((plainText[i] < 91) && ((plainText[i] + cipherArray[j]) > 90)) || ((plainText[i] > 97) && ((plainText[i] + cipherArray[j]) > 122)))
{
printf("%c", ((plainText[i] + cipherArray[j]) - 26));
}
else
{
printf("%c", (plainText[i] + cipherArray[j]));
}
However the code above is a little unwieldy and I am trying to tidy it up by replacing it with a function which I have written like this:
char codeLetters(char a, int b)
{
if (((a < 91) && (a + b) > 90) || ((a > 97) && (a + b) > 122))
{
char c = (a + b) - 26;
}
else
{
char c = a + b;
}
return c;
}
The idea being I could then call that function with:
char code = codeLetters(plainText[i], cipherArray[j]);
printf("%c", code);
However when I try and compile it I get the following errors:
vgen1.c:114:14: error: unused variable 'c' [-Werror,-Wunused-variable] char c = (a + b) - 26; ^ vgen1.c:118:14: error: unused variable 'c' [-Werror,-Wunused-variable] char c = a + b; ^ vgen1.c:121:12: error: use of undeclared identifier 'c' return c;
Could someone let me know where I am going wrong? For the record I have another function in this code which runs just fine