0

I wrote my solution to pset2 caesar and it compiles and behaves as expected but doesn't pass the check50 test. Error is as follows:

:) caesar.c exists.
:) caesar.c compiles.
:) encrypts "a" as "b" using 1 as key
:) encrypts "barfoo" as "yxocll" using 23 as key
:( encrypts "BARFOO" as "EDUIRR" using 3 as key
expected "ciphertext: EDU...", not "ciphertext: EB..."
:( encrypts "BaRFoo" as "FeVJss" using 4 as key
expected "ciphertext: FeV...", not "ciphertext: FB..."
:) encrypts "barfoo" as "onesbb" using 65 as key
:) encrypts "world, say hello!" as "iadxp, emk tqxxa!" using 12 as key
:) handles lack of argv[1]
See https://cs50.me/checks/5633e9927597f6d38744478e81dc9131f7d6f141 for more detail.

I tried debug50, it seems to launch (the side window opens) but then crashes (the side window closes and my prompt goes to my own error message.

~/workspace/pset2/caesar/ $ debug50 ./caesar
Wrong number of arguments

help50 also doesn't work, it gives the following output:

~/workspace/pset2/caesar/ $ help50 ./caesar
Wrong number of arguments

Sorry, not sure how to help (yet) with this one!   

Here is my code, I have tried moving the final else and embedding islower as a new else inside an if but both of those create errors that prevent the program from compiling. Please let me know if you are able to help with debug, help or my code.

I did run update50 before starting this problem set, I did it at the ~/workspace/ directory level but let me know if that isn't right.

The weird indenting in the islower if loop was recommended by style50 which tells me that's probably where the problem is but I can't figure out how to fix it.

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

int main(int argc, string argv[])


{
//Ensure that user enters 2 command line arguments (argv[0] - program name and argv[1] - the key number)
if (argc <= 1 || argc >= 3)
{
    printf("Wrong number of arguments\n");
    return 1;
}

//Get plaintext string that user wants converted to ciphertext
string plaintext = get_string("plaintext:  ");
printf("ciphertext:  ");

//Convert key as string to integer
int key = atoi(argv[1]);

//Ensure capital letters stay capital and lowercase stay lower
for (int i = 0, n = strlen(plaintext); i < n; i++)
{
    if (isalpha(plaintext[i]))

        if (isupper(plaintext[i]))
        {
            printf("%c", ((plaintext[i] - 'A' + key) % 26) + 'A');
        }

            if (islower(plaintext[i]))
            {
                printf("%c", ((plaintext[i] - 'a' + key) % 26) + 'a');
            }

    // Ensure non alpha characters stay as they are.
    else
    {
        printf("%c", plaintext[i]);
    }
}
printf("\n");
return 0;
}
0

If you called ./caesar 3, the version with debug50 is debug50 ./caesar 3.

Weird thing is - the code you show looks correct (although not that beautiful, instead of three if with isalpha, islower, isupper you could have only two of them with an else each). Are you sure that's the version you passed to check50?

Also, if (argc <= 1 || argc >= 3) could have been if (argc != 2).

About the weird indentation - are you mixing tab and space characters for indentation? Tab has no specific width per definition, some editors interpret it as 1 to 4, others as up to 8 spaces. Maybe style50 makes another assumption than you do.

Where you run update50 should not matter, as it works on the system and not on the current directory.

3
  • Thank you. I was looking at the problems others have had with this pset and came across a post where someone commented on the handling of upper and lower case. I think that might be a problem for me too. I ran the program using a combination of upper and lower case and the ciphertext it spits out is often longer than the plaintext. Thanks for the tip on the argc line and the else statements. I'll play with that as I think that's probably what is causing the style issue.
    – CM23
    Mar 2 '18 at 21:23
  • I think I get what you're getting at with help and debug. I'll try putting a number in argv and see if that helps. I've previously been calling just caesar.
    – CM23
    Mar 2 '18 at 21:59
  • I fixed my if else statements and that seems to have fixed all of the problems. I also got debug working but so far not help50. debug helped me see why I was getting more letters that I should be. Because my if/else statements weren't structured properly, it was running some loops twice when it should have skipped to the next one. It now passes check50 and style50.
    – CM23
    Mar 2 '18 at 22:01

You must log in to answer this question.

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