C,如何正确使用退出代码?
当我尝试提交工作时,系统告诉我使用退出代码。当我使用返回0和重新检查时,系统告诉我使用返回1 ... https://i.sstatic.net/dnvlv.png )
:) caesar.c exists.
:) caesar.c compiles.
:( encrypts "a" as "b" using 1 as key
expected "ciphertext: b\...", not "ciphertext: b"
:( encrypts "barfoo" as "yxocll" using 23 as key
expected "ciphertext: yx...", not "ciphertext: yx..."
:( encrypts "BARFOO" as "EDUIRR" using 3 as key
expected "ciphertext: ED...", not "ciphertext: ED..."
:( encrypts "BaRFoo" as "FeVJss" using 4 as key
expected "ciphertext: Fe...", not "ciphertext: Fe..."
:( encrypts "barfoo" as "onesbb" using 65 as key
expected "ciphertext: on...", not "ciphertext: on..."
:( encrypts "world, say hello!" as "iadxp, emk tqxxa!" using 12 as key
expected "ciphertext: ia...", not "ciphertext: is..."
:( handle lack of argv[1]
expected exit code 1, not 0
:( handles non-numeric key
timed out while waiting for program to exit
:( handles too many arguments
expected exit code 1, not 0
( 我的代码怎么了?
#include <stdio.h>
#include <cs50.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
int main(int argc, string argv[])
{
int ok;
char r1;
if (argc == 2)
{
for (int i = 0, s = strlen(argv[1]); i < s; i++)
{
if (!isdigit(argv[1][i]))
{
printf("Sorry\n");
return 0;
}
else
{
ok = atoi(argv[1]);
string c = get_string("Enter:");
printf("ciphertext: ");
for (int t = 0, a = strlen(c); t < a; t++)
{
if (c[t] < 91 && c[t] > 64)
{
r1 = (c[t] - 64 + ok) % 26 + 64;
printf("%c", r1);
}
else if (c[t] < 123 && c[t] > 96)
{
r1 = (c[t] - 96 + ok) % 26 + 96;
printf("%c", r1);
}
else
{
printf("%c", c[t]);
}
}
return 0;
}
}
}
else
{
printf("Sorry\n");
}
printf("\n");
return 0;
}
我尽力做好我的作业和所有绿色...
When I try to submit my work, the system told me to use the exit code. When I use return 0 and recheck, the system told me to use return 1...
(https://i.sstatic.net/dnVLV.png)
:) caesar.c exists.
:) caesar.c compiles.
:( encrypts "a" as "b" using 1 as key
expected "ciphertext: b\...", not "ciphertext: b"
:( encrypts "barfoo" as "yxocll" using 23 as key
expected "ciphertext: yx...", not "ciphertext: yx..."
:( encrypts "BARFOO" as "EDUIRR" using 3 as key
expected "ciphertext: ED...", not "ciphertext: ED..."
:( encrypts "BaRFoo" as "FeVJss" using 4 as key
expected "ciphertext: Fe...", not "ciphertext: Fe..."
:( encrypts "barfoo" as "onesbb" using 65 as key
expected "ciphertext: on...", not "ciphertext: on..."
:( encrypts "world, say hello!" as "iadxp, emk tqxxa!" using 12 as key
expected "ciphertext: ia...", not "ciphertext: is..."
:( handle lack of argv[1]
expected exit code 1, not 0
:( handles non-numeric key
timed out while waiting for program to exit
:( handles too many arguments
expected exit code 1, not 0
How can I fix it and what's wrong with my code?
#include <stdio.h>
#include <cs50.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
int main(int argc, string argv[])
{
int ok;
char r1;
if (argc == 2)
{
for (int i = 0, s = strlen(argv[1]); i < s; i++)
{
if (!isdigit(argv[1][i]))
{
printf("Sorry\n");
return 0;
}
else
{
ok = atoi(argv[1]);
string c = get_string("Enter:");
printf("ciphertext: ");
for (int t = 0, a = strlen(c); t < a; t++)
{
if (c[t] < 91 && c[t] > 64)
{
r1 = (c[t] - 64 + ok) % 26 + 64;
printf("%c", r1);
}
else if (c[t] < 123 && c[t] > 96)
{
r1 = (c[t] - 96 + ok) % 26 + 96;
printf("%c", r1);
}
else
{
printf("%c", c[t]);
}
}
return 0;
}
}
}
else
{
printf("Sorry\n");
}
printf("\n");
return 0;
}
I try to do well with my homework and all green...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
代码中有多个问题:
您应该在错误时返回非零退出状态。
如果给出的数字作为命令行参数的参数超过1位,则执行多个迭代(每个数字一个)。您应该将编码循环移出循环的第一个
。
使用硬编码的ASCII值对上和下部的案例字母,使代码的便携程度较低且难以读取。您应该使用字符常数
'a'
,'z'
,等等。'a ' 64;
是不正确的,并且可能会为某些输入产生@
而不是z
。您应该使用r1 =(c [t] -65 + ok)%26 + 65;
或更好的r1 =(c [t] - 'a'a' + ok)%26 +' a';
r1 =(c [t] -96 + ok)%26 + 96;
相同的错误
这是一个修改版本:
There are multiple issues in your code:
you should return a non zero exit status upon error.
if the number given as a command line argument has more than 1 digit, you perform multiple iterations (one for each digit). You should move the encoding loop out of the first
for
loop.using hard coded ASCII values for upper and lower case letters makes the code less portable and hard to read. You should use character constants
'A'
,'Z'
, etc.r1 = (c[t] - 64 + ok) % 26 + 64;
is incorrect and may produce@
instead ofZ
for some inputs. You should user1 = (c[t] - 65 + ok) % 26 + 65;
or betterr1 = (c[t] - 'A' + ok) % 26 + 'A';
same mistake for
r1 = (c[t] - 96 + ok) % 26 + 96;
Here is a modified version:
您可以通过在适当的代码行中添加
返回&lt; value&gt;,
来使用退出代码。使用
&lt; value&gt;
是与您的接口定义相匹配的,如果您的在线法官似乎是1。在您的代码中,您至少在这里没有这样做:
这应该是
一种替代方案是更明确的 https://en.cppreference.com/w/c/program/exit 对于通往程序结束的路径并不那么明显的情况。
(这主要是Lundin提到的评论。我将其变成了一个明确的答案。)
但是,要完全满足法官,您需要处理您的输出。
有了问题中提供的信息,无法解决这些问题。
You use the exit code by adding a
return <value>,
at the appropriate code line.With
<value>
being what matches your interface definition, in case of your online judge it seems to be a 1.In your code you at least fail to do so here:
which should be
An alternative is the more explicit https://en.cppreference.com/w/c/program/exit for situations in which the path to the end of the program is not as obvious.
(This is mostly what the comment by Lundin mentions. I turned it into an explicit answer.)
However, to completely satisfy the judge you need to work on your output.
With the info given in the question, a solution for those problems is not possible.