c-PSET2 Caesar:指针和整数错误之间的有序比较



我目前正在参加哈佛大学的CS50课程(CompSci简介(。我甚至还没有学会指针,我对这个错误消息感到困惑。它面临错误的行是这样的:对于上下文,完整的消息如下:

caesar.c:48:36:错误:指针和整数("字符串"(又名"char*"(和"char'"(之间的有序比较[-Weror]if(明文[j]+key>z(

因此,由于尚未接受指针方面的教育,我无法理解错误消息。当我用Help50编译时,它是无用的。如果需要的话,这是我的代码,我希望评论能有所帮助!

#include <cs50.h>
#include <stdio.h>
#include <ctype.h>
#include <string.h>
int main(int argc, string argv[])
{
//Creates string key
string key = argv[1];
//Checks for appropriate argument count
if (argc != 2)
{
printf("Usage: ./caesar key");
return 1;
}
//Checks if all argument characters are digits
for (int i = 0; i < strlen(key); i++)
{
if (isdigit(key[i]) == 0)
{
printf("Usage: ./caesar key");
return 1;
}
}
//Gets plaintext from user
string ciphertext = NULL;
char z;
string plaintext = get_string("plaintext:  ");
//Converts plaintext to ciphertext
for (int j = 0; j < strlen(plaintext); j++)
{
//Asks if character is alphabetical
if (isalpha(plaintext[j]))
{
//Asks if character is uppercase and assigning ASCII code accordingly
if (isupper(plaintext[j]))
{
z = 90;
}
else
{
z = 122;
}
//Performs conversion operation
if (plaintext[j] + key > z)
{
ciphertext[j] = plaintext[j] + key - 25;
}
else
{
ciphertext[j] = plaintext[j] + key;
}
}
//Keeps text the same since it is not a letter, and therefore shouldn't be shifted
else
{
ciphertext[j] = plaintext[j];
}
}
printf("ciphertext: %sn", ciphertext);
}

谢谢!

//Creates string key
string key = argv[1];

CCD_ 1是";字符串";,而不是整数值。

if (plaintext[j] + key > z)

该代码试图将char添加到string,然后将其与char进行比较。

您需要回到基础知识并复习课程,直到您理解了字符和字符串的数组。

string ciphertext = NULL;

在这里,您初始化了一个string,然后似乎期望它在稍后将字符写入该string时以某种方式增长。

拿着纸和铅笔坐下来,画出你对记忆存储的预期。。。

最新更新