1

Hello and thanks for reading.

I have created a few functions and at the moment I have no idea if my code works, so I just want to test to see if my code works but I am getting a weird error which I've spent ages trying to fix.

error: unused variable 'key' [-Werror,-Wunused-variable]

I have used the key variable in the functions below, but I am getting this error. I feel as if it has something to do with the key variable not being global? Any help is appreciated.

#include <cs50.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h> // for atoi(), string to int
#include <ctype.h>

int upperConv(int input);
int lowerConv(int input);
int key;

int main(int argc, string argv[])
{
if (argc != 2)
{
    printf("Please enter an integer for the key after ./caesar");
    return 1; 
}

int key = atoi(argv[1]);
string input = GetString();

for(int i = 0, n = strlen(input); i < n; i++)
{
    if (isupper(input[i]))
    {            
        printf("%c", upperConv(input[i]));
    }
    else if (islower(input[i]))
    {            
        printf("%c", lowerConv(input[i]));
    }          
    }    
}

int upperConv(int input)
{
int x = input - 65 + key;
int z = x % 26;
z = z + 65;
return z;
}

int lowerConv(int input)
{
int x = input - 97 + key;
int z = x % 26;
z = z + 97;
return z;
}
1

Your instincts are more or less correct. You have actually declared key twice, once as a global and a second time as a local var inside main(). Specifically, the line int key = atoi(argv[1]); is the second declaration and it takes precedence over the global var, so any usage of key inside of main will use the local version.

The actual error is that once you declare key inside of main, you never use it inside of main(), so you are getting the error. Well, actually, you're getting a warning, but there's a compiler flag that says to treat every warning as an error, so, well, you get the idea.

How to fix it? There are a couple of approaches. First, and easiest, if you want the global key to be used inside of main(), just remove the leading int from the statement in main. It changes the line from a combined declaration and initialization assignment to a simple assignment statement that uses the global var.

Another approach takes into consideration that it is generally not a good practice to use global variables. Instead of declaring key as a global, declare it as a local in main(), as you have done, and as a passed parameter in your two functions. You would change the function prototypes to something like this:

int upperConv(int input, int key);

There's no longer a global key var, and you pass the key by copy to the functions as needed - a much better practice. Of course, you'd need to make the appropriate change (add key) to your function calls and prototypes, etc.

If this answers your question, please click the check mark to accept this and remove the question from the unanswered pool. Let's keep up on forum maintenance. ;-)

| improve this answer | |

You must log in to answer this question.

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