我是 C 初学者,所以我可能不理解所有术语。 该程序旨在通过 vigenere 将一些纯文本转换为密文。这是第三个问题:http://cdn.cs50.net/2016/x/psets/2/pset2/pset2.html
但是,我一直收到 CS50 IDE 中的警告“控件可能到达非 void 函数的末尾”。感谢您的帮助!
#include <stdio.h>
#include <stdlib.h>
#include <cs50.h>
#include <math.h>
#include <string.h>
#include <ctype.h>
string k = "";
int testalpha ();
int main(int argc, string argv[])
{
k = argv[1];
testalpha ();
if (argc !=2 || testalpha(1))
{
printf("Please retry.");
return 1;
}
string p= GetString();
for (int i=0;i<strlen(p); i++)
{ int c [50];
int a=0;
if (isupper(p[i]))
{
c [i] = (((int)p[i] + k[a] -65) % 26)+65;
printf ("%c", c[i]);
a+=1;
if (a==strlen(k))
{a=0;}
continue;
}
if (islower(p[i]))
{
c [i] = (((int)p[i] + k[a]-97) % 26)+97;
printf ("%c", c[i]);
if (a==strlen(k))
{a=0;}
continue;
}
if (isalpha(p[i])==0)
{
c [i] = p[i];
printf ("%c", c[i]);
}
}
printf("\n");
}
int testalpha (void)
{
for (int x=0; x<strlen(k); x++)
{
if(isalpha(k[x])==0)
{
return 1;
}
else
{
return 0;
}
}
}
最佳答案
仔细观察函数int testalpha (void)
。
它不会在所有控制路径上返回值:例如,如果 strlen(k)
为零。
这是 C 中未定义的行为:除了 main
之外,如果函数的返回类型不是 void
,那么您必须返回一个值。
关于c - 如何在C中解决 "control may reach end of non-void function"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40178747/