CS50凯撒程序(pset2)运行,但在check50中输出错误(“预期的退出代码1,不是0”)

时间:2020-05-24 22:11:58

标签: c encryption cs50 caesar-cipher

我完成了CS50的pset2(凯撒),但是当我通过check50运行它时,我遇到了三个错误,每个错误都以某种方式涉及命令行参数。当我尝试自己进行测试时,它工作正常,这使我认为问题出在“幕后”,除非我修改代码,否则无法解决。我尝试删除不必要的行,但错误仍然存​​在,因此我想知道是否有人可以看一下我的代码并告诉我出什么问题了。

这是代码:

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

int check_key(int argc, string argument);
string plain_cipher(string plaintext, int key);
int main(int argc, string argv[])
{

    // checks if there are only 2 cl-arguments + if there are any alpha characters (uses function: check_key)
    if (argc != 2 || argc == 1)
    {
        printf("Usage: ./caesar key\n");
    }
    else
    {
        string input = argv[1];
        int key = check_key(argc, input);
        if (key == 0)
        {
            printf("Usage: ./caesar key\n");
        }
        else
        {
            string plaintext = get_string("plaintext: ");
            string ciphertext = plain_cipher(plaintext, key);
        }
    }
}

int check_key(int argc, string argument)
{
    int i;
    int j = 0;
    char letter = argument[0];

    for (i = 0; i < strlen(argument); i++)
    {
        letter = argument[i];
        if isalpha(letter)
        {
            j++;
        }
    }
    if (j >= 1)
    {
        return 0;
    }
    else
    {
        int key = atoi(argument);
        return key;
    }
}

string plain_cipher(string plaintext, int key)
{
    string ciphertext = "ciphertext: ";
    printf("%s", ciphertext);
    int i = 0;
    for (i = 0; i < strlen(plaintext); i++)
    {
        char letter = plaintext[i];
        if isalpha(letter)
        {
            if isupper(letter)
            {
                char enc_letter_upper = (letter - 65 + key) % 26;
                enc_letter_upper += 65;
                printf("%c", enc_letter_upper);
            }
            else
            {
                char enc_letter_lower = (letter - 97 + key) % 26;
                enc_letter_lower += 97;
                printf("%c", enc_letter_lower);
            }
        }
        else
        {
            printf("%c", letter);
        }
    }
    printf("\n");
    return ciphertext;
}

这是check50输出:

:) 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
:) encrypts "BaRFoo" as "FeVJss" using 4 as key
:) encrypts "barfoo" as "onesbb" using 65 as key
:) encrypts "world, say hello!" as "iadxp, emk tqxxa!" using 12 as key
:( handles lack of key
    expected exit code 1, not 0
:( handles non-numeric key
    expected exit code 1, not 0
:( handles too many arguments
    expected exit code 1, not 0

所有这三个失败显示的错误是“预期的退出代码1,而不是0”,但是我不太确定这实际上是什么意思,因此我希望有人可以详细说明/修改我的代码。在此先感谢:)

0 个答案:

没有答案