0

I am just wondering why the check result is like this:

:) caesar.c exists.

:( caesar.c compiles.

    expected exit code 0, not 1

:| encrypts "a" as "b" using 1 as key

    can't check until a frown turns upside down

:| encrypts "barfoo" as "yxocll" using 23 as key

    can't check until a frown turns upside down

.(below are several lines for the similar result)

.

.

I exited it with 0 at the end not 1 ( only used in when argv is != 2), why does it fail to compile?? I checked with different strings and it works but somehow just don't understand why it does not pass the check. Thanks.

I have added my code in the following, please check:

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

int index;
int shifted_index;
int ascii;

int main(int argc, string argv[])
{
    if (argc != 2)
    {
        printf("Usage: ./caesar k\n");
        return 1;
    }
    int k = atoi (argv[1]);
    string s = get_string("plaintext: ");
    printf("ciphertext: ");
    for (int i = 0, n = strlen(s); i < n; i ++)
    {
        if (isalpha(s[i]))
        {
            if (isupper(s[i]))
            {
                index = ((int) s[i]) % 65; //find out the index of the charachter
                shifted_index = (index + k) % 26; // the index after using the key
                ascii = shifted_index + 65; // find out the ascii number of the new index
                printf("%c", (char) (ascii)); //print out the ciphertext one by one
            }
            else if (islower(s[i]))
            {
                index = ((int) s[i]) % 97;
                shifted_index = (index + k) % 26;
                ascii = shifted_index + 97;
                printf("%c", (char) (ascii));
            }
        }
        else
        {
            printf("%c", s[i]);
            }    
    }    
    printf("\n");    
    return 0; //exit the function    
} 
2
  • Surely it is not the fault of your program but of check50, the specifications are very strict, if any failure is not going to compile even if it does in your environment. In these cases it would be necessary to see the complete code to help you – MARS Nov 8 '18 at 20:59
  • have added my codes here, thanks for your help! :) – Ching Nov 9 '18 at 23:17
2

To be clear, check50 is saying the program is not compiling and the later tests can't be executed until it does compile. The exit code 1 has nothing to do with your program's return statement. The exit code of 1 simply means it didn't compile.

check50 is rarely the problem, but it is still a possibility. We'd need to see your entire code to figure out what's happening. You did successfully compile it in the IDE, correct?

[EDIT: Addition]

Now that I've seen your code, it's your fault, sort of. ;-)

There was an issue with check50 that when index was used as a var name, it caused a conflict. That was more or less fixed long ago, but there's one way that it can rear it's ugly head and you found it. You created a GLOBAL variable called index. If it had been a local variable, this would not have happened.

Simply put, your code has a library conflict with something that check50 uses, the global variable name index. Change the variable name index to something else and you should be just fine. ;-)

Now, about GLOBAL VARIABLES..... You have created multiple global variables for your code. A global variable is one that is created outside of main or any other function. By being global, it is universally available throughout every part of the program. They're also highly dangerous and risky because they introduce the possibility of conflicts with other parts of a program where the same name could be used as a local variable, as you've demonstrated with your code and check50.

Some say that globals should never be created. That is simply not true. The rule is that a global variable should ONLY be created when there is an extremely good reason for doing so. In the case of the code above, there's no valid reason. The entire program is contained inside main, so all vars should be local to main.

About the semi-key-word index. This word is used in so many contexts in so many libraries that it's a good practice NOT to use it as a variable name. (my_index or something similar is fine.) Index and similar names that are so commonly used in programming and database coding should generally be avoided because of conflicts with libraries and other usages in the wild. Why invite hidden problems? Would you have figured this one out if someone didn't tell you about the check50 issue? What about when you're using a library and don't get a clue? (although, if you're compiling locally, there's likely to be a clue in the compile log.)

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

1
  • I have added my entire code. It says ":( caesar.c compiles.expected exit code 0, not 1", so I guess I did not compile it successfully? Thanks for letting me know will click on the check mark if I get my answer. :) – Ching Nov 9 '18 at 23:20

You must log in to answer this question.

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