#include <stdio.h>
#define n 50;
struct student
{
int numb;
char name[15];
float score[3];
float avg;
};
struct student stu[n];
void main
{
int j=0,i=0;
printf("\n\t\t请输入学员的信息:\n");
char ans='y';
do
{
stu[j]=input();
j++;
i++;
printf("要继续输入吗?[y/n]");
scanf("%c",&ans);
}while(ans=='y'||ans=='Y');
};
struct student input ()
{
struct student s;
printf("\n输入学员的学号:");
scanf ("%d",&s.numb);
printf("\n输入学员的姓名:");
fflush(stdin);
gets(s.name);
printf("\n输入学员的成绩:");
scanf("%f %f %f",&s.score[0],&s.score[1],&s.score[2]);
return s;
}
说明:目的是通过调用函数向结构数组中输入学员的信息
调试后是这样的
C:\Program Files\Microsoft Visual Studio\MyProjects\练习\练习.cpp(250) : error C2143: syntax error : missing ']' before ';'
C:\Program Files\Microsoft Visual Studio\MyProjects\练习\练习.cpp(250) : error C2143: syntax error : missing ';' before ']'
C:\Program Files\Microsoft Visual Studio\MyProjects\练习\练习.cpp(259) : error C2065: 'input' : undeclared identifier
C:\Program Files\Microsoft Visual Studio\MyProjects\练习\练习.cpp(268) : error C2373: 'input' : redefinition; different type modifiers
执行 cl.exe 时出错.
练习.obj - 1 error(s), 0 warning(s)
參考答案:试试改过的程序:
#include <stdio.h>
#define n 50
struct student
{
int numb;
char name[15];
float score[3];
float avg;
};
struct student stu[n];
void input(struct student *);
main()
{
int j=0,i=0;
int ans='y';
printf("\n\t\t请输入学员的信息:\n");
do
{
input(&stu[j]);
j++;
printf("要继续输入吗?[y/n]");
scanf("%c",&ans);
}while(ans=='y'||ans=='Y');
for (i=0; i<j; ++i) printf("%d: %10s %f %f %f\n", stu[i].numb, stu[i].name, stu[i].score[0], stu[i].score[1], stu[i].score[2]);
};
void input (struct student *s)
{
printf("\n输入学员的学号:");
scanf ("%d",&s->numb);
printf("\n输入学员的姓名:");
scanf("%s", s->name);
printf("\n输入学员的成绩:");
scanf("%f %f %f",&(s->score[0]), &(s->score[1]), &(s->score[2]));
fflush(stdin);
}