为什么此循环不运行“ if语句”?

时间:2020-06-17 06:41:53

标签: c for-loop cs50

我不知道为什么这个C代码不起作用。一切工作到循环中的if语句为止。问题基本上是使用Caesar加密来打印密文。

This is how the program runs。我已上传问题的屏幕截图。我正在GitHub上使用CS50 IDE。

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

int main(int argc, char* argv[])
{
    if( argc == 2 )
    {
        printf("Success");
       printf("\n");
    }
    else
    {
        printf("Usage: ./caesar key");
        return 1;
    }

    char pt[100];
    char ct[100];
    int key = (int)(argv);
    /*(if(key < 0)
    {
        printf("Integer input only");
        return 1;
    }*/

    printf("plaintext:  ");
    get_string("%s",pt);
    for (int i=0, n=strlen(pt);i<n;i++)
    {
        printf ("#");
        if (islower(pt[i]))
        {
            ct[i] = (((( pt[i] + key )) - 97) % 26) + 97;
        }
        else if (isupper(pt[i]))
        {
            ct[i] = (((( pt[i] + key )) - 65) % 26) + 65;
        }
        else
        {
            ct[i] = pt[i];
        }
    }

    for (int x=0, n = strlen(ct);x<n;x++)
        printf("ciphertext: %c \n",ct[x]);
    return 0;
}

1 个答案:

答案 0 :(得分:3)

问题:

  • 调用get_string()后变量pt []为空

文档:

使用代码:

{{1}}