C、如何正确使用exit code?

C, How to use exit code properly?

当我尝试提交作业时,系统告诉我使用退出代码。当我使用 return 0 并重新检查时,系统告诉我使用 return 1 ... (https://i.stack.imgur.com/dnVLV.png)

:) 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: is..."  
:( handle lack of argv[1]
    expected exit code 1, not 0  
:( handles non-numeric key
    timed out while waiting for program to exit
:( handles too many arguments
    expected exit code 1, not 0

我该如何修复它以及我的代码有什么问题?

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

int main(int argc, string argv[])
{
    int ok;
    char r1;
    if (argc == 2)
    {
        for (int i = 0, s = strlen(argv[1]); i < s; i++)
        {
            if (!isdigit(argv[1][i]))
            {
                printf("Sorry\n");
                return 0;
            }
            else
            {
                ok = atoi(argv[1]);
                string c = get_string("Enter:");
                printf("ciphertext: ");
                for (int t = 0, a = strlen(c); t < a; t++)
                {
                    if (c[t] < 91 && c[t] > 64)
                    {
                        r1 = (c[t] - 64 + ok) % 26 + 64;
                        printf("%c", r1);
                    }
                    else if (c[t] < 123 && c[t] > 96)
                    {
                        r1 = (c[t] - 96 + ok) % 26 + 96;
                        printf("%c", r1);
                    }
                    else
                    {
                        printf("%c", c[t]);
                    }
                }
                return 0;
            }
        }
    }
    else
    {
        printf("Sorry\n");
    }
    printf("\n");

    return 0;
}

我努力做好我的作业和所有的绿色...

您可以通过在适当的代码行添加 return <value>, 来使用退出代码。
<value> 与您的接口定义相匹配,如果您的在线判断它似乎是 1.
在您的代码中,您至少没有在此处这样做:

else
    {
        printf("Sorry\n");
    }

应该是

else
    {
        printf("Sorry\n");
        return 1;
    }

对于程序结束的路径不那么明显的情况,另一种选择是更明确的https://en.cppreference.com/w/c/program/exit

(Lundin 的评论中主要提到的就是这个,我把它变成了一个明确的答案。)

但是,要完全满足法官的要求,您需要对输出进行处理。
根据问题中给出的信息,无法解决这些问题。

您的代码中存在多个问题:

  • 您应该 return 出现错误时的非零退出状态。

  • 如果作为命令行参数给出的数字超过 1 个数字,则执行多次迭代(每个数字一次)。您应该将编码循环移出第一个 for 循环。

  • 对大写字母和小写字母使用硬编码的 ASCII 值会降低代码的可移植性和可读性。您应该使用字符常量 'A''Z'

  • r1 = (c[t] - 64 + ok) % 26 + 64; 是不正确的,对于某些输入可能会产生 @ 而不是 Z。您应该使用 r1 = (c[t] - 65 + ok) % 26 + 65; 或更好的 r1 = (c[t] - 'A' + ok) % 26 + 'A';

  • 同样的错误 r1 = (c[t] - 96 + ok) % 26 + 96;

这是修改后的版本:

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

int main(int argc, char *argv[]) {
    if (argc != 2) {
        fprintf(stderr, "missing argument\n");
        return 1;
    }
    char *arg = argv[1];
    char *p;
    int shift = (int)strtol(arg, &p, 10);
    if (!(arg[0] >= '0' && arg[0] <= '9') || p == arg || *p != '[=10=]') {
        fprintf(stderr, "invalid shift argument: %s\n", arg);
        return 1;
    }
    char *s = get_string("Enter string: ");
    printf("ciphertext: ");
    for (int t = 0; s[t] != '[=10=]'; t++) {
        unsigned char c = s[t];
        /* assuming ASCII: upper and lowercase letters are contiguous */
        if (c >= 'A' && c <= 'Z') {
            c = (c - 'A' + shift) % 26 + 'A';
        } else
        if (c >= 'a' && c <= 'z') {
            c = (c - 'a' + shift) % 26 + 'a';
        }
        putchar(c);
    }
    putchar('\n');
    return 0;
}