0

This is my code:

    #include <stdio.h>
    #include <cs50.h>
    #include <stdlib.h>
    #include <string.h>
    #include <ctype.h>
    
    int main (int argc, string argv[])
    {
        // getting a proper command line argument
    
        if (argc != 2)
        {
            printf("GET A VAID NUMBER, IDIOT!!!!\n");
            return 1;
        }
    
        // Checking if the command line arguments are correct
         int j= atoi(argv[1]);
    
        if( j<0)
        {
            printf(" Please enter a valid input");
        }
        else
        /*
        The real deal begins here
        */
        // Asking for an input
    
        string a = get_string("Plaintext");
        printf ("Ciphertext: ");
       //  n= strlen(a);
    
        for (int i=0; n= strlen(a); i<n; i++)
        // checking if the input is uppercase or lowercase
        {
            if isupper(a[i])
            {
                printf("%c", (((a[i] + j) - 65) % 26) + 65);
    
            }
            else if islower (a[i])
            {
                printf("%c", (((a[i] + j) - 97) % 26) + 97);
            }
            else
            {
                printf("%c", a[i]);
            }
            printf("\n");
            return 0;
        }
    
    }

And what's the problem with : string a = get_string("Plaintext"); There's shouldn't be any errors regarding this, but the terminal shows:

     make cesar
    clang -ggdb3 -O0 -std=c11 -Wall -Werror -Wextra -Wno-sign-compare -Wno-unused-parameter -Wno-unused-variable -Wshadow    cesar.c  -lcrypt -lcs50 -lm -o cesar
    cesar.c:30:5: error: expected expression
        string a = get_string("Plaintext");
        ^
    cesar.c:34:29: error: use of undeclared identifier 'a'
        for (int i=0; n= strlen(a); i<n; i++)
                                ^
    cesar.c:34:19: error: use of undeclared identifier 'n'
        for (int i=0; n= strlen(a); i<n; i++)
                      ^
    cesar.c:34:35: error: use of undeclared identifier 'n'
        for (int i=0; n= strlen(a); i<n; i++)
                                      ^
    cesar.c:34:36: error: expected ')'
        for (int i=0; n= strlen(a); i<n; i++)
                                       ^
    cesar.c:34:9: note: to match this '('
        for (int i=0; n= strlen(a); i<n; i++)
            ^
    cesar.c:34:38: error: misleading indentation; statement is not part of the previous 'for' [-Werror,-Wmisleading-indentation]
        for (int i=0; n= strlen(a); i<n; i++)
                                         ^
    cesar.c:24:5: note: previous statement is here
        else
        ^
    cesar.c:34:38: error: use of undeclared identifier 'i'
        for (int i=0; n= strlen(a); i<n; i++)
                                         ^
    7 errors generated.
    make: *** [<builtin>: cesar] Error 1
    ~/ $ 

And I think I've already declared i and n, so why are there still errors?

1

Your problem is with this bit of code here:

if( j<0)
{
    printf(" Please enter a valid input");
}
else
/*
The real deal begins here
*/
// Asking for an input
    
string a = get_string("Plaintext");
printf ("Ciphertext: ");

You seem to have written this as if everything after the else will be performed as long as the if statement is false. But, this is not the case. The compiler sees this:

if(j < 0)
{
    printf("Please enter a valid input");
}
else
{
string a = get_string("Plaintext");
}
printf("Ciphertext: ");

When an if, else if, or else statement is not followed by {}, then next statement (and only the single, immediately-following statement) is considered linked with it. So, a is throwing undeclared identifier exceptions because it is only declared within the scope of the else statement. Because you want the rest of your code to be performed if your if statement does not, you do not need else here.

Your loops are throwing an undeclared identifier exception for n and i because you have too many statements in your loop declaration; this is also why you are getting errors for ( and ; in that same line. There should only be three, but you have 4: for(int i = 0; n = strlen(a); i < n; i++) should be for(int i = 0, n = strlen(a); i < n; i++) because declaring variables are all part of one statement.

Hope all of this helps and let me know if you have any questions.

1
  • Thanks, Mr. Pratt, you have helped me with almost all my doubts and now I have my code fixed (after removing another bug)! – Young Physists Feb 18 at 14:01

You must log in to answer this question.

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