C中的函数问题:在';int'/';字符';,参数太少,无法运行,其他参数太少
好的,首先,我应该让你们都知道我是一个相对较新的程序员,我似乎不能正确地使用函数,所以如果这些问题真的很愚蠢/明显,我要提前道歉 不管怎样,现在开始实际编程。这是一个学校项目,有点像刽子手。我已经做了好几个星期了,我快要完成了,但是这些讨厌的错误阻碍了我,我就是无法修复它们 如果有人能帮我消除最后几个错误,我将不胜感激! 再说一次,这里的初学者程序员,我可能犯了一些值得畏缩的错误。也很抱歉代码太长C中的函数问题:在';int'/';字符';,参数太少,无法运行,其他参数太少,c,function,C,Function,好的,首先,我应该让你们都知道我是一个相对较新的程序员,我似乎不能正确地使用函数,所以如果这些问题真的很愚蠢/明显,我要提前道歉 不管怎样,现在开始实际编程。这是一个学校项目,有点像刽子手。我已经做了好几个星期了,我快要完成了,但是这些讨厌的错误阻碍了我,我就是无法修复它们 如果有人能帮我消除最后几个错误,我将不胜感激! 再说一次,这里的初学者程序员,我可能犯了一些值得畏缩的错误。也很抱歉代码太长 #include <stdio.h> #include <string.h&g
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <ctype.h>
typedef struct {
char title[50];
char hidden[50];
}Title;
void Film(Title* pT)
{
int i=0;
int number;
char movies[44][50];
FILE *fp = NULL;
fp = fopen("Film.txt", "r");
for(i=0; i<44; ++i)
{
fgets(movies[i], sizeof(movies[i]), fp);
}
fclose(fp);
number = (rand() % 44);
strcpy(pT->title, movies[number]);
}
char Star(Title* pT, char lowerc, char higherc, char character)
{
int val;
char c;
int lenMovie;
lenMovie = strlen(pT->title);
strcpy(pT->hidden, pT->title);
for(val=0; val <= lenMovie; val++)
{
c = pT->hidden[val];
if(c == lowerc || c == higherc)
{
pT->hidden[val] = character;
}
else if(c >= 'a' && c<= 'z')
{
pT->hidden[val] = '*';
}
else if(c >= 'A' && c<= 'Z')
{
pT->hidden[val] = '*';
}
else
{
pT->hidden[val] = c;
}
}
printf("%s", pT->hidden);
}
char Film_Guess(Title* pT, int attempt)
{
int guess[50], answer, size;
printf("What movie do you think it is: ");
scanf("%s", &guess);
size = strlen(pT->title);
answer = strncmp(pT->title, guess[50], size);
if(answer = 0)
{
printf("You beat the Film Genie, nce work!");
return 0;
}
else
{
attempt++;
return main();
}
}
int main(void)
{
char option;
int attempt = 0;
char lowerc, higherc, character, reply;
Title t;
srand(time(NULL));
printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\t\t\t\t Welcome Player\n\n\n");
while(attempt <= 5)
{
printf("\nWould you like to try: ");
scanf("%c", &reply);
if(reply == 'y' || reply == 'Y')
{
Film (&t);
Star(&t, lowerc, higherc, character);
printf("\n Would you like to guess a character(c) or the whole film(f):");
scanf("%c",&option);
if(option =='c' || option =='C')
{
printf("\nPlease enter a character: ");
scanf("%c", &character);
lowerc = tolower(character);
higherc = toupper(character);
Star(&t, lowerc, higherc, character);
}
else if(option =='f' || option =='F')
{
Film_Guess(&t, attempt);
}
else
{
printf("\nInvalid response");
return main();
}
}
else{
break;
}
}
if(reply == 'n' || reply == 'N')
{
printf("\nLoser");
return 0;
}
else
{
printf("\nInvalid response");
return main();
}
}
#包括
#包括
#包括
#包括
#包括
类型定义结构{
字符标题[50];
字符隐藏[50];
}头衔;
无效胶片(标题*pT)
{
int i=0;
整数;
电影[44][50];;
FILE*fp=NULL;
fp=fopen(“Film.txt”、“r”);
对于(i=0;ititle,电影[编号]);
}
字符星型(标题*pT,字符低,字符高,字符)
{
int-val;
字符c;
国际电影;
lenMovie=strlen(pT->title);
strcpy(pT->hidden,pT->title);
for(val=0;val隐藏[val];
如果(c==lowerc | | c==higherc)
{
pT->hidden[val]=字符;
}
如果(c>='a'&&chidden[val]='*';
}
如果(c>='A'&&chidden[val]='*';
}
其他的
{
pT->hidden[val]=c;
}
}
printf(“%s”,pT->hidden);
}
char Film_Guess(标题*pT,int尝试)
{
int猜测[50],答案,大小;
printf(“你认为这是什么电影:”);
scanf(“%s”,猜测(&guess));
尺寸=strlen(pT->title);
答案=strncmp(pT->title,guess[50],size);
如果(答案=0)
{
printf(“你打败了电影精灵,干得漂亮!”);
返回0;
}
其他的
{
尝试++;
返回main();
}
}
内部主(空)
{
字符选项;
int尝试=0;
字符lowerc,higherc,字符,回复;
标题t;
srand(时间(空));
printf(“\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\t\t\t欢迎玩家\n\n\n\n\n”;
虽然(尝试我发现了一些错误(可能还有更多错误):
strncmp()
接受两个char*
参数,您将提供一个int*
作为其中一个参数。检查手册页:
在第117行:Star(&t,char lowerc,char higherc,char character);
--传递参数时不应提供类型,仅在声明参数时提供类型(*注意如下)
在第129行:Star(&t);
——您的函数是用4个参数声明的。您不能只提供一个参数,必须提供全部四个参数
在第135行:Film_Guess(&t,int-trunt);
——与第117行的问题相同
*注意:如果您对什么是“参数”和什么是“参数”感到困惑,请参阅。给定:
int guess[50], answer, size;
...
answer = strncmp(pT->title, guess, size);
您需要char guess[50]
或差不多
main()
中的此行:
是声明和函数调用之间的交叉
Star (&t);
您需要在传递给函数的参数数量上保持一致
另一个问题似乎类似:
Film_Guess(&t, int attempt);
这真的很有帮助,它现在正在编译,但在它询问用户是否想要玩并且他们输入“Y”后,它会绕过所有内容并返回到该问题,知道为什么吗?好吧,它会返回到问题,因为while
循环。至于为什么它会绕过所有内容,我不太确定。我建议修复您的缩进ough,可能有助于您找到问题;我很难说出哪个条件(if
语句)中的内容还有什么不好。好的,谢谢,我现在将它修好一点,并在上面编辑它,再看看为什么它可能会绕过所有内容,再次感谢!@Realt:它可能会跳过东西,因为它在读了第一个字母后会读换行符。使用%c”
跳过空白。
Film_Guess(&t, int attempt);