只有main()函数在c语言中执行



开始学习c。main函数执行得很好,但是程序没有执行第二个函数就结束了运行。我感觉我在main中的for循环中犯了一个错误。

int check_key_length(int count);
int main(void)
{
char key[20];
int count = 0; 
printf("Enter key: ");
scanf("%s", key);

for(int i = 0; i < strlen(key); i++) 
{  
if (key[i] != ' ')
count++;  
}  
printf("Total number of characters in a string: %d", count); 
}

int check_key_length(int count)
{
int set_amount = 26;
if (count < set_amount)
printf("Error: Your key is too short! Please input 26 charsn");
else if (count > set_amount)
printf("Error: Your key is too long! Please input 26 charsn");
else
string message = get_string("Enter string to encrypt: ");
return 0;
}

你转发声明了你的函数,为它提供了一个定义,但是你需要在你的主程序中调用这个函数,这样你的机器才能执行它,像这样调用你的函数

#include <stdio.h>
#include <string.h>
int check_key_length(int count);
int main(void)
{
char key[27];
int count = 0;
int strLength;
do {
printf("Enter key: ");
scanf("%s", key);
strLength = strlen(key);
} while(check_key_length(strLength) != 0);


for(int i = 0; i < strLength; i++) 
{
if (key[i] != ' ')
{
count++;  
}
}
printf("Total number of characters in a string: %dn", count);
return 0;
}
int check_key_length(int count)
{
int set_amount = 26;
if (count < set_amount)
{
printf("Error: Your key is too short! Please input 26 charsn");
return -1;
}
else if (count > set_amount)
{
printf("Error: Your key is too long! Please input 26 charsn");
return -2;
}
else
{
return 0;
}
}

注意,我不得不修改代码一点,它构建没有任何警告或错误,我可能改变了行为的方式,你不期望,所以检查我的代码之前粘贴到

最新更新