0

I wrote this code and ran check50 and got an error in lack of argv[1], using keys of 23, 12 and 65. Can someone point to my mistake that i have made in the code.

int main (int argc, string argv[])
{
    // condition to limit user types the command and value of key only.
    if (argc == 2)
    {
        // converting the string of k to an actual int.
        int k = atoi(argv[1]);

        string s = GetString();

        //loop to go through each alphabet of the string
        for (int i = 0, n = strlen(s); i < n; i++)
        {
            // condition to check the ascii values bw A to Z or a to z.
            if ((s[i] >= 'A' && s[i] <= 'Z') || (s[i] >= 'a' && s[i] <= 'z' ))
            {

                printf("%c",s[i]+ (char)k % 26 );
            }
            else
            {
                printf("%c", s[i]);
            }    
        }
        printf("\n");
        return 0;
    }
    else
    {
        return 1;
    }
}
0

Per the specification:

If your program is executed without any command-line arguments or with more than one command-line argument, your program should yell at the user and return a value of 1 (which tends to signify an error) immediately as via the statement below:

The error you get:

:( handles lack of argv[1]
   \ expected output, not an exit code of 1

is because in case of wrong number of inputs, you just exit the program returning 1, while additionally you should print a message that explains to the user what went wrong. Your code though has more serious problems than that. The error has nothing to do with the number provided as key. Those errors will need additional fixing. To fix this just add a printf statement before returning 1. Something like:

else
{
    printf("Usage: ./caesar key\n");
    return 1;
}
7
  • Can you be a bit of more to the point as to where the serious problem is? The 1, 3,4 keys seems to b working fine.....I used the formula given in the specification....just cant seem to figure out why its not working with 23, 12 and 65 keys Aug 8 '15 at 16:32
  • The problem is with the part of code bellow // condition to check the ascii values bw A to Z or a to z.. You are not using the modulo correct to wrap around after z.
    – ChrisG
    Aug 8 '15 at 16:37
  • hey I fixed my problem... its passing the check50 test...but when i tried to decyrpt the link to the video in the end i noticed that its not converting the u to h properly.... but when i cut paste the same code of the if condition it decrypted the u to h....cant seem to understand why it is not converting in the caesar.c file Aug 9 '15 at 11:49
  • Change the code in your Q to reflect what you have.
    – ChrisG
    Aug 9 '15 at 13:48
  • 1
    dont know why i restarted the pc ran my caesar.c and it worked fine....deleted the question that i posted as another post due to academic honesty Aug 9 '15 at 16:19

You must log in to answer this question.

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