【发布时间】:2020-06-03 15:27:45
【问题描述】:
我正在尝试用 C 创建一个密码编码器,到目前为止我有以下代码。 if 语句无法正常工作,并在输入任何内容时弹出分段错误,无论是数字还是字母。应该打印出如何使用密文的else语句只有在有空格时才打印出来。
#include <stdio.h>
#include <cs50.h>
#include <ctype.h>
#include <string.h>
#include <math.h>
int main(int argc, string argv[]) //im certain the problem is on this line but I dont know where exactly nor do I know how to fix it
{
if (isdigit(argv[1]))
{
string s = get_string("Cipher Text: ");
}
else
{
printf("Usage: ./caesar key\n");
}
}
我确定错误必须处理命令行参数中的字符串,但我不确定要采取哪些步骤来修复它。
【问题讨论】:
-
请启用完整的编译器警告,该警告应标记
isdigit(argv[1])以传递错误的类型。 CS50 的自制类型string对您没有任何帮助,它导致的问题比解决的问题多。另外请始终在访问argv之前检查argc。