为什么这个循环不是运行而是'if statement'?
Why does this loop not run the 'if statement'?
我不知道为什么这个 C 代码不起作用。一切正常,直到循环中的 if 语句。题目基本上是用凯撒加密打印密文。
。我上传了问题的截图。我在 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;
}
问题:
- 变量 pt[] 在调用 get_string()
后为空
文档:
- https://man.cs50.io/3/get_string
- get_string - 提示用户输入来自标准输入的一行文本,returns 将其作为字符串
使用代码:
char *pt;
pt = get_string("plaintext: ");
我不知道为什么这个 C 代码不起作用。一切正常,直到循环中的 if 语句。题目基本上是用凯撒加密打印密文。
#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;
}
问题:
- 变量 pt[] 在调用 get_string() 后为空
文档:
- https://man.cs50.io/3/get_string
- get_string - 提示用户输入来自标准输入的一行文本,returns 将其作为字符串
使用代码:
char *pt;
pt = get_string("plaintext: ");