-1

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

  • Why would you write 97 instead of `'a'` – M.M May 22 '17 at 09:23
  • You might want to read up on the [`isalpha`](https://linux.die.net/man/3/isalpha), `isupper`, `islower`, [`toupper`](https://linux.die.net/man/3/toupper), and `tolower` functions in the standard library (`ctype.h`). As a rule, you don't want to look at raw character encodings, since they may change based on system and locale. – John Bode May 23 '17 at 18:48

2 Answers2

3

Could someone let me know where I am going wrong?

The problem is that you declare variable c inside your if statements, so the scope of the variable is just inside if. Therefore, the statement :

return c;

cannot be executed properly, as there is no c defined in the function scope. Change your codeLetters function to :

char codeLetters(char a, int b)
{
    char c;
    if (((a < 91) && (a + b) > 90) || ((a > 97) && (a + b) > 122))
    {
        c = (a + b) - 26;
    }
    else
    {
        c = a + b;
    }

    return c;
}

See this question for information on variables' scope.

Community
  • 1
  • 1
Marievi
  • 4,951
  • 1
  • 16
  • 33
0

scope of c is limited within the blocks where it is declared, it is not visible where it is returned

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; // c is not visible here
}
Pras
  • 4,047
  • 10
  • 20