CS50 凯撒多重错误

CS50 Caesar multiple errors

我正在研究 CS50 的凯撒,我不知道这段代码有什么问题。我不断收到 5 个错误和 1 个警告(向下滚动)。到目前为止,我已尽最大努力修复它,但老实说,我不知道该怎么做。

该程序本质上是一个凯撒密码编码器。当我启动程序时,它应该为我提供一个密钥(这是一个数字)和我试图加密的消息,然后它应该给我输出密码。

#include <stdio.h>
#include <cs50.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
bool check_valid_key(string s);

int main(int argc, string argv[])
{
    if (argc != 2 || !check_valid_key(argv[1]))
    {
        printf("Usage: ./caesar key\n");
        return 1;
    }
    int key = atoi(argv[1]);

    string plaintext = get_string("plaintext: ");

    printf("ciphertext: ");
    for (int i = 0, len = strlen(plaintext); i < len; i++)
    {
        char c = plaintext[i];
        if (isalpha(c))
        {
            char m = 'A';
            if (islower(c))
                m = 'a';
            printf("%c", (c - m + key) % 26 + m);
        }
        else
            printf("%c", c);
    }
    printf("\n");
 }

 bool check_valid_key(string s);
 {
     for (int i = 0, len = strlen(s); i < len; i++)
        if (!isdigit(s[i]))
            return false;
    return true;
 }

使用更好的文本编辑器在编译之前捕获语法错误会很有用。这是您的程序存在的语法错误。

len应该像这样先赋值

for (int i = 0, len = strlen(plaintext); i < len; i++) {

但这很有效,因为每次迭代都会调用 strlen(),请改为执行此操作。

int len = strlen(plaintext); // it's in efficient to calculate length each iteration.
for (int i = 0; i < len; i++) {

if 语句的条件应该用方括号括起来。
if (islower(c)) // if (cond) <-- brackets are importants

你的函数定义有一个分号,这也是一个语法错误

bool check_valid_key(string s) // ; <-- remove this semicolon
{
}