帮我检查一下VC语言中的这个程序:通过调用函数向结构数组中输入学员的信息

王朝知道·作者佚名  2009-11-30
窄屏简体版  字體: |||超大  
 
分類: 電腦/網絡 >> 程序設計 >> 其他編程語言
 
問題描述:

#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);

}

小贴士:① 若网友所发内容与教科书相悖,请以教科书为准;② 若网友所发内容与科学常识、官方权威机构相悖,请以后者为准;③ 若网友所发内容不正确或者违背公序良俗,右下举报/纠错。
 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航