我得到以下错误建议:
注意:预期为'char *',但参数类型为'int *'_CRTIMP __cdecl __MINGW_NOTHROW char * fgets (char *, int, FILE *);
这与t->名称等有关,我猜,有什么办法可以解决这个问题吗?
现在输赢的存储方式也很奇怪,程序是这样工作的:
inputs:
Name of team > goo
Wins of team > 3
Losses of team > 4
Name of team > goo
Wins of team > 4
Losses of team > 3
outputs
team 1 name is goo
team 2 name is goo
我的代码
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_SIZE 20
#define MAXC 1024
typedef struct {
char name[MAX_SIZE];
char wins[MAX_SIZE];
char losses[MAX_SIZE];
} teams;
int addteam(teams *t);
int addteam(teams *t) {
char buffer[MAXC];
printf("Name of team > ");
fgets(t->name, MAXC, stdin);
printf("Wins of team > ");
getchar();
fgets(t->wins, MAXC, stdin);
printf("Losses of team > ");
getchar();
fgets(t->losses, MAXC, stdin);
return 0;
}
int main() {
int numOfTeams;
char array[MAX_SIZE];
char * Ptr;
teams team[MAX_SIZE] = {{ .name = "", .wins = 0, .losses = 0}};
for(int i=0; i < 2; i++) {
addteam( &team[i] );
}
for(int j=0; j<2; j++) {
printf("team %d name is %s %s %sn",j+1, team[j].name, team[j].wins, team[j].losses);
}
}
fgets
将文本行读入字符缓冲区。fgets
对数字一无所知。
代替
fgets(t->wins, MAXC, stdin);
char buffer[100]; // temporary buffer for line containing a number
fgets(buffer, sizeof(buffer), stdin); // read line
t->wins = atoi(buffer); // extract number
对t->losses
做同样的处理。您可能应该将其放在int ReadIntFromLine(FILE *input)
之类的函数中。
免责声明:
我们可以使用strtol
来代替atoi
,这对于现实世界的程序来说是一个更好的选择,因为它允许检测格式错误的输入。