C语言 :结构体 编写函数print,打印学生成绩

Asashall· 2020-12-17 10:36:55
求助:题目为谭浩强C程序设计p330T3:

代码如下

#include<stdio.h>
#define N 5
#define STU struct Student
//能不能将 STU stu[N]; 放在此处,去掉main和函数中的STU stu[N];

void print();

struct Student
{
int num;
char name;
float score[3];
};

void print()
{
STU stu[N];
int i;
for(i=0;i<N;i++)
{
printf("No.%d %s %.2f %.2f %.2f\n",&stu[i].num,stu[i].name,&stu[i].score[0],&stu.score[1],&stu.score[2]); //此处报错
}
printf("\n");
}

int main()
{
STU stu[N];
int i;
printf("请依次输入学生的学号、姓名以及3门课程成绩:\n");
for(i=0;i<N;i++)
{
printf("->");
scanf("%d %s %f %f %f",&stu[i].num,stu[i].name,&stu[i].score[0],&stu.score[1],&stu.score[2]); //此处报错
getchar();
printf("\n");
}
print(); //调用函数
return 0;
}



报错如下:
F:\Microsoft Visual Studio\MyProjects\sdffsa\sdfas.cpp(19) : error C2228: left of '.score' must have class/struct/union type
F:\Microsoft Visual Studio\MyProjects\sdffsa\sdfas.cpp(19) : error C2228: left of '.score' must have class/struct/union type
F:\Microsoft Visual Studio\MyProjects\sdffsa\sdfas.cpp(32) : error C2228: left of '.score' must have class/struct/union type
F:\Microsoft Visual Studio\MyProjects\sdffsa\sdfas.cpp(32) : error C2228: left of '.score' must have class/struct/union type

不清楚是什么原因,此外有无dalao告知在报错的地方,编写代码时如何换行编写,这一行太长了看着不方便,我按照网上查找,
在末尾处加了 / ,之后程序报错:
F:\Microsoft Visual Studio\MyProjects\sdffsa\sdfas.cpp(19) : error C2059: syntax error : '/'
F:\Microsoft Visual Studio\MyProjects\sdffsa\sdfas.cpp(33) : error C2059: syntax error : '/'

求dalao指导,编程软件为 VC 6.0
...全文
1081 7 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
Asashall· 2020-12-17
  • 打赏
  • 举报
回复
引用 1 楼 chxchxkkk的回复:
struct Student { int num; char name; float score[3]; }; 写成如下形式: typedef struct Student { int num; char name; float score[3]; }STU; printf("No.%d %s %.2f %.2f %.2f\n",&stu[i].num,stu[i].name,&stu[i].score[0],&stu.score[1],&stu.score[2]); //此处报错 printf里就不要用&了,除非要输出这个学号的地址
改了之后,还是报错 error C2236 :unexpected ' struct ' 'Student' warning C4091 :'typedef' :ignored on left 'struct Stident' when no variable is declared
Asashall· 2020-12-17
  • 打赏
  • 举报
回复
引用 1 楼 chxchxkkk的回复:
struct Student { int num; char name; float score[3]; }; 写成如下形式: typedef struct Student { int num; char name; float score[3]; }STU; printf("No.%d %s %.2f %.2f %.2f\n",&stu[i].num,stu[i].name,&stu[i].score[0],&stu.score[1],&stu.score[2]); //此处报错 printf里就不要用&了,除非要输出这个学号的地址
哦哦哦,这里是复制过来时没注意
chxchxkkk 2020-12-17
  • 打赏
  • 举报
回复
void print() { STU stu[N]; int i; for(i=0;i<N;i++) { printf("No.%d %s %.2f %.2f %.2f\n",&stu[i].num,stu[i].name,&stu[i].score[0],&stu.score[1],&stu.score[2]); //此处报错 } printf("\n"); } 这里写的不对, void print(STU stu[]) { //STU stu[N]; int i; for(i=0;i<N;i++) { printf("No.%d %s %.2f %.2f %.2f\n",&stu[i].num,stu[i].name,&stu[i].score[0],&stu.score[1],&stu.score[2]); //此处报错 } printf("\n"); }
chxchxkkk 2020-12-17
  • 打赏
  • 举报
回复
struct Student { int num; char name; float score[3]; }; 写成如下形式: typedef struct Student { int num; char name; float score[3]; }STU; printf("No.%d %s %.2f %.2f %.2f\n",&stu[i].num,stu[i].name,&stu[i].score[0],&stu.score[1],&stu.score[2]); //此处报错 printf里就不要用&了,除非要输出这个学号的地址
qzjhjxj 2020-12-17
  • 打赏
  • 举报
回复
希望能帮到你
Asashall· 2020-12-17
  • 打赏
  • 举报
回复
dalao又是你,再次感谢dalao,就是还不知道argc和argv是什么,这个我自己去查。 哦对了,发现dalao用system("pause")时总是忘记加头文件,已经两次了。
qzjhjxj 2020-12-17
  • 打赏
  • 举报
回复
修改如下,供参考:
#include<stdio.h>

#define N 5

//#define STU struct Student
typedef struct Student
{
  int   num;
  char  name[10];//名字是字符串
  float score[3];
}STU;

STU stu[N]; //定义结构变量stu[N]为全局变量,方便打印函数输出

void print();

int main(int argc, char* argv[])
{
    int i;
    printf("请依次输入学生的学号、姓名以及3门课程成绩:\n");
    for(i=0;i<N;i++)
      {
         printf("->");
         scanf("%d %s %f %f %f",&stu[i].num,&stu[i].name,&stu[i].score[0],&stu[i].score[1],&stu[i].score[2]);//此处报错
       //scanf("%d %s %f %f %f",&stu[i].num,stu[i].name,&stu[i].score[0],&stu.score[1],&stu.score[2]);    //此处报错
         //getchar();
         //printf("\n");
     }
    print();               //调用函数
    system("pause");
    return 0;
}
//---------------------------------------------------------------------------

void print()
{
  //STU stu[N];
  int i;
  for(i=0;i<N;i++)
  {
    printf("No.%d %s %.2f %.2f %.2f\n",stu[i].num,stu[i].name,stu[i].score[0],stu[i].score[1],stu[i].score[2]);//此处报错
  }
  printf("\n");
}

33,322

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 新手乐园
社区管理员
  • 新手乐园社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧