0

I keep getting 2 undeclared identifier errors on these respective lines

line 48: printf("ciphertext:%c", ((p[n] - 65 + k) % 26) + 65); line 54: printf("ciphertext:%c", ((p[n] - 65 + k) % 26) + 65);

every time I run the code, where the variable k is the issue.

I have referenced many similar solutions online where when I run their code there seem to be no issue but when I run mine no matter how I try to debug it; changing the data type of k; editing the line 45 to line 56 portion of my code; it does not seem to solve the problem.

Your help and advice is much appreciated!

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

//getting command line input from user
int main(int argc, string argv[])
{
    if (argc != 2)
    {
        printf("missing command-line argument\n");
        return 1;
    }
    else
    {
        for (int i = 0 ; i < strlen(argv[1]) ; i++)
        {
            if (isdigit(argv[1][i]))
            {
                continue;
            }
            else
            {
                printf("Usage: ./caesar key\n");
                return 1;
            }
        }
        int key = atoi(argv[1]);
        printf("%i", key);
        int k = key % 26;
    }

    //prompt user for input
    string plaintext = get_string("plaintext: ");
    string p = plaintext;

    //looping through each character whether it is uppercase, lowercase or others
    for (int n = 0 ; n <strlen(p) ; n++)
    {
        //if character is a letter
        if (isalpha(p[n]))
        {
            //if character is an upper case letter
            if(isupper(p[n]))
            {
                printf("ciphertext:%c", ((p[n] - 65 + k) % 26) + 65);
                
            }
            //if character is a lower case letter
            else
            {
                printf("ciphertext:%c", ((p[n] - 65 + k) % 26) + 65);
            }
        }
        //if character is not a letter
        else
        {
            printf("ciphertext:%c", p[n]);
        }
    }
printf("\n");
}
1

The problem is that k is out of scope at that point, meaning that it no longer exists.

A variable is only "in scope" or in existence within the closest curly braces that surround it. Let me give you a visual.

// other code precedes this.
{
    int i = 4;     // i is created and initialized.

    {
        int k = 0;  // k is created and initialized.
        k++;        // k is incremented. 
        i++;        // i is still in scope here.
    }  // k is enclosed by this closing curly brace and the opening curly brace above. 
    // at this point, k no longer exists. 
    
    k++;   // this attempt to use k causes an undeclared var error because k is out of scope.

    i++;   //this works fine because i is still in scope.

}  // now, both i and k are out of scope. 
// more code follows.  
// Any attempt to use i or k here would cause an undeclared var error

In short, if you want to use k later, it needs to be declared outside of the curly braces except for the ones that open and close main.

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

You must log in to answer this question.

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