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!