0

I'm trying to call my encryption function and am getting an undeclared identifier error. Not sure where i'm going wrong.

Code:

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

string encrypt(int k, string plaintext);


int main(int argc, string argv[])
{
    // Ask for Key, checking inputs
    if (argc != 2)
    {
        printf ("Usage: ./caesar key\n");
        return 1;
    }
    for (int i = 0, n = strlen(argv[1]); i < n; i++)
    {
        if (!isdigit(argv[1][i]))
        {
            printf ("Usage: ./caesar key\n");
            return 1;
        }
        int k = atoi(argv[1]);
        string plaintext = get_string("plaintext: ");
        printf ("key = %i\n", k);
        printf ("plaintext - %s\n", plaintext);
    }
    // Return cyphertext
    string cypertext = encrypt(k, plaintext);
}

Error:

caesar.c:31:32: error: use of undeclared identifier 'k'
    string cypertext = encrypt(k, plaintext);

1 Answer 1

0

It's a scope issue. k is declared inside the curly braces of the FOR loop. That means that when the code exits the for loop, k will cease to exist.

K should be declared outside of any loops or if code blocks if it is to be used outside of them. You can always assign new values to k inside the for loop if you need to.

Try declaring k just before the for loop.

You must log in to answer this question.

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