0
#include <stdio.h>
#include <cs50.h>
#include <math.h>
#include <string.h>
#include <ctype.h>

bool is_valid_key(string s);

int main(int argc, string argv[])
{
    if (argc != 2 || !is_valid_key(argv[1]))
    {
        printf("Usage ./caesar key\n");
        return 1;
    }
    return 0;

bool is_valid_key(string s);

    for(int i = 0; i < strlen(s); i++)
        {
            if(isdigit(s))
            {
                return false;
            }
        }
    return true;
}

error: use of undeclared identifier 's' for(int i = 0; i < strlen(s); i++)

error: use of undeclared identifier 's' if(isdigit(s))

1
  • Where is s being declared? Commented Oct 10, 2021 at 0:52

1 Answer 1

2

You have a structural problem. You're trying to create main(), followed by a function called is_valid_key), but you have some errors in how you did it.

The main() function (yes, it's technically a function, with a special name) should end with a closing curly brace, before the start of any other functions. This code has the closing curly brace at the very end. The consequence is that the compiler interprets the function as part of main. Then, it looks for where s is declared in main. It isn't, so it throws these errors.

Next, the first line of the is_valid_key() function has a semicolon at the end. The compiler interprets that semicolon as the end of the function (in other words, a do-nothing function.)

The structure should look like this:

int main(int argc, string argv[])
{
    //main code here
}

bool is_valid_key(string s)
{
    //function code here
}

BTW, I laid it out in detail because it's not a question of figuring out the logic. This is something that anyone would simply either know or not know. It's just a matter of being exposed to it once. Once it's explained, it's easily remembered and understood. ;-)

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

1
  • Thank you so much!
    – Bob Cap
    Commented Oct 11, 2021 at 19:36

You must log in to answer this question.

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