c - 即使我输入了一个数字,为什么我的代码仍然返回 1 和错误消息?
问题描述
我正在尝试在 pset 2 上完成 Caesar 练习。我的代码编译得很好,但它似乎给了我输出 Usage: ./caesar key 即使我输入了一个 int。任何帮助都非常感谢我出错的地方:)
该程序应该可以工作,用户键入 ./Caesar 后跟一个空格和一个整数。它应该打印成功和给定的整数。如果用户要键入除此之外的任何其他内容,即。2x 或任何字符等,它应该打印 Usage: ./caesar key。
// Libraries
#include <cs50.h>
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
int main(int argc, string argv[])
{
if (argc == 2 && (atoi(argv[1]) > 0))
for(int i = 0, len = strlen(argv[1]); i < len; i++)
{
char n = argv[1][i];
int digit = isdigit(n);
if (n != digit)
{
printf("Usage: ./caesar key\n");
return 1;
}
else
{
printf("Success\n %i", digit);
return 0;
}
}
else
{
printf("Usage: ./caesar key\n");
return 1;
}
}
解决方案
int digit = isdigit(n);
if (n != digit)
应该
int digit = isdigit(n); // 0 (false) if n isn't a digit; non-zero (true) if n is a digit.
if (!digit)
要不就
if (!isdigit(n))
推荐阅读
- java - 通知 FCM android java 不起作用服务处理意图的时间太长:com.google.android.c2dm.intent.RECEIVE 应用程序可能会关闭
- windows - Windows 10 自定义凭据提供程序:无需 Windows 密码即可进行身份验证
- sql - 如何进行自我加入
- reactjs - 如何在我的材质网格中使每张卡片的大小相同
- javascript - 球放入盒子时如何改变图像?
- c# - 如何将数据从控制器传递到 Blazor 上的视图?
- java - 在textView中一一显示ArrayList中的值
- arrays - 将排序数组转换为低高数组
- c++ - 如何从二叉搜索树 C++ 中正确删除节点
- javascript - 将firebaseui CDN与nuxt.js集成,firebase未定义错误