0

here's my code

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

int main(int argc, string argv[])


{
    // prints Usage : ./caesar key
    if (argc == 1)
    {
        printf("Usage : ./caesar key\n");
    }

    // prints Usage : ./caesar key
    else if (argc > 2)
    {
        printf("Usage : ./caesar key\n");
    }

    // following proceeds if argc = 2
    else if (argc == 2)
    {
        for (int a = 0; a < strlen(argv[1]); a++)
        {
            char pt_1 = argv[1][a]; // checks each charecter of argv[1] is digit or not

            // if argv[1] is a number then following happens
            if (isdigit(pt_1))
            {
                // converts string argv[1] to an int;
                int i = atoi(argv[1]);

                // gets user input
                string plaintext = get_string("plaintext:");

                // basically writes ciphertext & performs the rest of the stuff afterwards
                printf("ciphertext: ");
                for (int b = 0; b < strlen(plaintext); b++)
                {
                    // following proceeds if each charecter of plaintext is an alphabet
                    if (isalpha(plaintext[b]))
                    {
                        // following proceeds if the alphabet is Uppercase
                        if (isupper(plaintext[b]))
                        {
                            int p = plaintext[b] % 65;
                            int c = (p + i) % 26;
                            int z = c + 65;
                            printf("%c", z);
                        }

                        // following proceeds if the alphabet is Lowercase
                        else if (islower(plaintext[b]))
                        {
                            int p = plaintext[b] % 97;
                            int c = (p + i) % 26;
                            int z = c + 97;
                            printf("%c", z);
                        }

                    }

                    // prints anyother charecter which is not an alphabet as it is
                    else
                    {
                        printf("%c", plaintext[b]);
                    }

                }

            }

            // prints Usage : ./caesar key even though argc = 2 but argv[1] is not a number
            else if (isalpha(pt_1))
            {
                printf("Usage : ./caesar key\n");
                return 1;
            }
        }
        printf("\n");

    }
}

error 0- So after giving commant line argument "./caesar 65" I get a prompt saying "plaintext:" after typing input as "burfoo" i get "cyphertext: onesubbplaintext:". It is clearly printing "plaintext:" again. why is this happening? Also when I type "burfoo" again I get the output as cyphertext: onesubb.

error 1- what is "handles lack of key"? then it says "expected exit code 1, not 0". what doe this mean? error 2- what is "handles non-numeric key"? then it says "timed out while waiting for program to exit". what is this?

error 3- what is "handles too many arguments"? Again it says "expected exit code 1, not 0". what doe this mean?

1 Answer 1

1

There are a lot of questions and problems here. Normally, it's one question to handle one or a few related problems.

The messages like "handles lack of key" are describing the specific test being run. In this case, "Does the program handle the lack of a key, ie, a missing parameter when executing the program?" Another checks for nonalphas in the key.

For many of the errors that you are getting when running check50, the underlying problem is the same. "expected exit code 1, not 0" means exactly that. check50 was expecting a return code of 1 from your program, indicating that the program terminated with an error. Specifically, there should be a return 1; statement after printing any usage message, terminating the program.

The timeout error is because your program is waiting for user input when there should be none. check50 is waiting for it to end, while the program is waiting for input. Eventually it times out. This is related to the second "plaintext" prompt.

The code has everything built into a single for loop. That means it's trying to check each char in the key and trying to get the plain text and encode it on every pass through the for loop. The code is trying to do too much at the same time. It's like trying to bake a cake by mixing in the flour, butter, baking soda, icing, sprinkles, decorations and everything else into a pan and baking it all at once. It'll be a mess.

There are likely more issues, but this will get you going. Instead of throwing it at check50, you should try running the program with good and bad input and make sure that it does what it is supposed to do. Work on one issue at a time.

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

1
  • Thank you! So I guess it's about adding return value? but still my code is taking ./caesar 2x correctly while it should not so now I'm working on that(I pretty much understand why that is happening). But one final question. Why should I add a return value? I mean I wasn't doing this till week 1(first time learning CS)? Dec 3, 2020 at 1:02

You must log in to answer this question.

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