0

I've finally finished my Caesar code as can be seen below. However, when I run the check50, it says that it fails, but the output is correct. What am I doing wrong here? Why is it failing if the output is correct?

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

int main(int argc, string argv[])
{
    int k = 0;
    string key = argv[1];
    if (argc == 2)
    {
        for (k = 0; k < strlen(key); k++)
        {
            if (!isdigit(key[k]))
            {
                printf("\nUsage: %s key\n", argv[0]);
                return 1;
            }
        }
    }
    else
    {
        printf("\nUsage: %s key\n", argv[0]);
        return 1;
    }

string plaintext = get_string("\nplaintext: ");

printf("ciphertext: ");
for (int i = 0; i < strlen(plaintext); i++)
{

    char c = plaintext[i];
    int keycode = atoi(key);
    if (isupper(c))
    {
        printf("%c", (((c + keycode) - 'A') % 26) + 'A');
    }
    if (islower(c))
    {
        printf("%c", (((c + keycode) - 'a') % 26) + 'a');
    }
    else if (isspace(c) || isdigit(c) || ispunct(c))
    {
        printf("%c", c);
    }


}

}

The check50:

:) caesar.c exists.

:) caesar.c compiles.

:( encrypts "a" as "b" using 1 as key expected "ciphertext: b...", not "ciphertext: b"

:( encrypts "barfoo" as "yxocll" using 23 as key expected "ciphertext: yx...", not "ciphertext: yx..."

:( encrypts "BARFOO" as "EDUIRR" using 3 as key expected "ciphertext: ED...", not "ciphertext: ED..."

:( encrypts "BaRFoo" as "FeVJss" using 4 as key expected "ciphertext: Fe...", not "ciphertext: Fe..."

:( encrypts "barfoo" as "onesbb" using 65 as key expected "ciphertext: on...", not "ciphertext: on..."

:( encrypts "world, say hello!" as "iadxp, emk tqxxa!" using 12 as key expected "ciphertext: ia...", not "ciphertext: ia..."

:) handles lack of argv[1]

koby
  • 11
  • 3
  • You're not printing a newline at the end. – Barmar Mar 01 '21 at 17:58
  • 2
    From the problem set: **After outputting ciphertext, you should print a newline. Your program should then exit by returning 0 from main.** – Barmar Mar 01 '21 at 17:58
  • @MichaelDorgan Actually, `main()` is an exception – Barmar Mar 01 '21 at 18:01
  • Yeah- moved it over to godbolt and realized my mistake... – Michael Dorgan Mar 01 '21 at 18:02
  • Still needs a return 0 at the end as he can skip the internal returns... – Michael Dorgan Mar 01 '21 at 18:04
  • @MichaelDorgan See https://stackoverflow.com/questions/18402853/must-the-int-main-function-return-a-value-in-all-compilers – Barmar Mar 01 '21 at 19:05
  • I also want to point out that something like `expected "ciphertext: Fe...", not "ciphertext: Fe..."` doesn't mean you got it right. It's only showing the first 2 characters of the expected output and your output. There might be something wrong later on. – M-Chen-3 Mar 02 '21 at 23:33

0 Answers0