0

I've been a bit stuck in trying to solve this problem involving changing the key from one letter to the next. I haven't gotten to wrap around the key yet. I think I know what the problem is but need help in coming up with ideas to solve it.

Here's my code below (without all the include files):

int main (int argc, string argv[]) { //check command prompt to be 2 if (argc != 2) { printf("Error\n"); return 1; }

string key = argv[1];
int keylen = strlen(key);

//check to make sure key is an alphabetical letter 
for (int i = 0; i < keylen; i ++)
{
    if (!isalpha(key[i]))
    {
        printf("Error\n");
        return 1;
    }
}

int keyarray [keylen];
for (int x = 0; x < keylen; x ++)
{
    keyarray[x] = ((toupper(key[x])) - 'A');
}

//get secret message from user
string Msg = GetString();
int MsgLen = strlen(Msg);

for (int j = 0; j < MsgLen; j ++)
{
    if (isalpha(Msg[j]))
    {
        if (MsgLen > keylen)
        {
            printf("%c", Msg[j] + keyarray[x]);
        }
    }

    else
    {
        printf("%c", Msg[j]);
    }

    printf("\n");
}

}

When I run the code, I receive a the following error message:

use of undeclared identifier 'x' printf("%c", Msg[j] + keyarray[x]);

I'm pretty sure this is because x is declared as a local variable when I'm using it to assign numbers to the key. On an overall level, I'm having trouble understanding how to apply keyarray[] into the for loop with the secret message. My thinking is that if I can declare x as a global variable so that key keyarray[x1], keyarray[x2], keyarray [x3] etc. will all be different numbers that I can use to apply to the secret message. Is this the right way to think about this problem? If so, how do I declare x as global variable so that keyarray will change? I tried declaring int x outside of the loop, but I also get an error from that. Any help would be appreciated!

1

Your thoughts about why you're getting the error are on track. As declared in your code, the variable x only exists in the for loop where it is declared. Outside of the for loop, it is out of scope, so it no longer exists.

You need to declare a local variable that you can use as an index for the key. You could redeclare x, but that would be a bad practice, so choose something else. Once you have an index variable, think in terms of incrementing it only when a letter from the plain text string is encoded. When you do increment it, you need to look at whether you are at the end of the key string, and if so, reset it.

RE: local vs. global variable. I don't know that you have a firm grasp of what a global variable is yet. A global variable is one that is declared outside of main or any other function. It will essentially be in scope EVERYWHERE. This is different from, say, a local variable declared at the start of a main program that will be in scope everywhere within main.

Why is this significant? It is a bad practice to create a global variable without a really good reason. It can lead to conflicts with variables in functions, and will commonly be a problem in a team programming environment when one person isn't intimately familiar with what another programmer is doing. In short, globals are an opportunity for error. HOWEVER, I am not saying that they should never be used. Globals should only be created when their use is appropriate and justified (something that will be demonstrated in a later pset.) You will hear people say that they should never be used, but that's just not true. You'll learn the rules of programming, like this, and then with experience, learn when to break the rules. ;-)

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

1
  • Hi Cliff, thank you for your help. I had the incorrect understanding of local vs. global var. I thought everything under main was a global variable, but realized they were local after rewatching the CS50 short. As far as declaring another local variable to for index, you mentioned to increment the index only when a letter from the plain text is encoded. This mean the incrementing will take place within the loop for the message rather than the the loop in the key, but I will also have to use the index variable within the key loop to make sure the array is holding different variables, correct? – jwang1191 Jan 17 '16 at 16:35

You must log in to answer this question.

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