0

So Ive been told to go step by step to fix errors, but when I try to fix each bug, the same message appears: Use of undeclared identifier 'key' please help me with this so i can move on with bugs.

#include <stdio.h>
#include <cs50.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>

int upper(int input);
int lower(int input);


int main(int argc, string argv[])
{
string k = argv[1];

if (argc != 2)
key = atoi(argv[1]);
{
    printf("The key must be an int. Please try again.");
    return 1;
} 

} 
else
{

{
    printf("Good, your key is %i\n, now enter a plaintext!:"
    string plain = GetString();

    for (int  = 0, n = strlen(plain); i < n; i++)
    {
        if (isalpha(plain[i]))
        {
            if (isupper(plain[i]))
            {
                printf("%c", upper(plain[i]));
            }
            if (islower(plain[i]));
            {
                printf("%c", lower(plain[i]));
            }
          }
          else
          {
            printf("%c" , plain[i]);
          }
       }
}
}

int upper(int input)
{
int i = input - 65 + key;l
int k = i + 65;
return k;
}

int lower(int input)
{
int i = input - 97 + key;
i = i % 26;
int k = i + 97;
return k;
}  
0

Yes, the error is very straightforward. You haven't declared key.

Do you want it to be an integer variable? Then you should have a statement like int key; or you could declare it and assign it at the same time, int key = argv[1];. You know how, since you declared plain as a string later on in the code.

Every variable must be declared (or created) before it is used. This is one of the most fundamental tasks in C. You should really go back and watch the class lectures again until you understand it.

Next, you need to check your pairings of curly braces. Right now, main ends just before the first else because you have too many braces and they're not paired correctly.

Also, you need to use some restraint with the curly braces. Too many sets of curly braces could affect your code because they define the scope of any variables created within them, even when they are properly paired. Those variables do not exist outside of any curly brace pair that they are between.

You would benefit from running your code through style50 to clean up your formatting. While clean formatting usually doesn't affect how a program runs, it certainly will make it easier to read, modify and debug later - particularly your abundant use of whitespace and {curly braces}.

If this answers your question, please accept this answer to remove the question from the unanswered question pool. Let's keep up on forum housekeeping. ;-)

1
  • Well how do I fix my curly braces then? – Ani Jul 9 '15 at 14:20

You must log in to answer this question.

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