0

When compiling this code getting mutltiple errors any idee why is the couse of these errors thanks.

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


//This boolean will store true ore false
bool control_caesar_key(string s);

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

    //To use this atoi function added stdlib.h to the library
    int key = atoi(argv[1]);

    string text = get_string("plaintext: ");

        printf("ciphertext: ");

    //This formula will encrypt the messages by rotating each letter position and
    //to come plaintext from ASCII example if k is 27 = A. A should become 27
    //position away from B. This will provide wrap around from Z to A and so on.

    for (int i = 0, len = strlen(text); i < len; i++)
    {
        char ci = (text[i]);
        if (isalpha(ci))
        {
            char j = 'A';
        if (islower(ci))
            char j = 'a';
        printf("%c", (ci - j + key) %26 + j);
    }

    else
        printf("%c", ci);
    }
    printf("\n");
}

bool control_caesar_key(string s)
 {
    if (int i = 0, len = strlen(s); i < len; i++)

        if (!isdigit(s[i]))
        return false;
    return true;
}

Errors

   

~/pset2/caesar/ $ make caesar
clang -ggdb3 -O0 -std=c11 -Wall -Werror -Wextra -Wno-sign-compare -Wno-unused-parameter -Wno-unused-variable -Wshadow    caesar.c  -lcrypt -lcs50 -lm -o caesar

caesar.c:50:9: error: expected expression
    if (int i = 0, len = strlen(s); i < len; i++)
        ^
caesar.c:50:37: error: use of undeclared identifier 'i'
    if (int i = 0, len = strlen(s); i < len; i++)
                                    ^
caesar.c:50:41: error: use of undeclared identifier 'len'
    if (int i = 0, len = strlen(s); i < len; i++)
                                        ^
caesar.c:50:46: error: use of undeclared identifier 'i'
    if (int i = 0, len = strlen(s); i < len; i++)
                                             ^
4 errors generated.
make: *** [<builtin>: caesar] Error 1
~/pset2/caesar/ $ 
1

You have identified the variables correctly, but used your brackets and keywords wrongly. Inside the for loop, you open a if statement bracket but don't close. In your control_caesar_key function, you wanted to use a for loop and wrote down the condition properly, but you replace the keyword for with the keyword if. Along with that, you will be required to enclose the statements within the for loop in brackets.

Hope this helps. If it does then please check the tickmark.

2
  • Hi Vsjan. I removed the empty space from [ for (int i = 0, len = strlen(text); i < len; i++)] error where reduced to 4 start from line 50 what the error shows. But now know I that error start from line 50 "I updated the code above". I've changed for to if but this generated even more error. will put another update later on thanks – Matrix May 9 at 18:00
  • Hi Again I changed [ if to for ] in line 50 and it compile without errors, i run the code [ ~/pset2/caesar/ $ make caesar clang -ggdb3 -O0 -std=c11 -Wall -Werror -Wextra -Wno-sign-compare -Wno-unused-parameter -Wno-unused-variable -Wshadow caesar.c -lcrypt -lcs50 -lm -o caesar ~/pset2/caesar/ $ ./caesar 13 plaintext: HELLO ciphertext: URYYB ~/pset2/caesar/ $ ] it compile and run as it should Thank you again for your excellent explanation. – Matrix May 9 at 18:16

You must log in to answer this question.

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