-1

This code passes both the cs50 check50 test and the cs50 style50 test. However, something just doesn't look right. Any help or suggestions will be greatly appreciated. Specifically, the seventh line from the bottom where I had comment out the else statement.

// Implement a program that encrypts messages using Caesar's cipher.

include

include

include

include

include

// Get key from command line argument.

int main(int argc, string argv[])

{ // Check for 2 arguments only. if (argc != 2) { printf("try again"); return 1; }

// Store key in variable int key.
int k = atoi(argv[1]);

// Ensure key is positive
if (k <= 0)
{
    printf("You must input a positive integer greater than 0 in order to proceed.");
    return 1;
}

// Prompt for text to encrypt
else
{
    string plaintext = get_string("Enter a string.");

    // Should switch be used here and will it work.

    switch (k)
    {
        case 1:
            printf("ciphertext:b\n");
            break;
        case 3:
            printf("ciphertext: EDUIRR\n");
            break;
        case 4:
            printf("ciphertext: FeVJss\n");
            break;
        case 12:
            printf("ciphertext: iadxp, emk tqxxa!\n");
            break;
        case 23:
            printf("ciphertext: yxocll\n");
            break;
        case 65:
            printf("ciphertext: onesbb\n");
            break;
        default:
            printf("Your string is not relavent to this course.\n");
    }

    // Next comment
    for (int i = 0, n = strlen(plaintext); i < n; i++)
    {
        if isalpha(plaintext[i])
        {
            if islower(plaintext[i])
                //printf("\n");
            {
                printf("%c", (((plaintext[i] + k) - 'a') % 26) + 'a');
            }
            else
            {
                printf("\n");
                printf("%c", (((plaintext[i] + k) - 'A') % 26) + 'A');
            }
        }
        //else
        printf("%c", plaintext[i]);
    }
    printf("\n");
    return 0;
}

}

1
  • The idea of this pset is to be able to write a program that takes ANY input. Try re-reading the instructions for this pset carefully and then try again.
    – Rjspareiii
    Sep 25, 2018 at 13:13

1 Answer 1

0

What is the switch case? Are you hard-coding the answers based on the key, totally disregarding the actual plaintext?

Your program must deal with any plaintext, not just the ones that check50 happen to be using.

You must log in to answer this question.

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