求助!这样写哪里错了。。编写一个函数print,打印一个学生的成绩数组,该数组中有5个学生的数据,每个学生的数据包括

bolixin36 2016-06-08 08:41:49
//2、编写一个函数print,打印一个学生的成绩数组,该数组中有5个学生的数据,每个学生的数据包括
//num学号,name姓名,score[3]三门课的成绩。用主函数输入这些数据,用print函数输出这些数据。
#include<iostream>
using namespace std;
struct Student
{
int num;
char name[20];
int score[3];
};
Student s[5];
void print (Student *p);

int main()
{
int j = 0;
for (int i = 0; i < 5; i ++)
{
cout<<"please input the num and the name of the student:"<<i + 1;
cin>>s[i].num>>s[i].name;
for (int j = 0; j <3; j ++)
{
cout<<"please input the score of subject"<<j + 1<<" of the student:"<<i + 1;
cin>>s[i].score[j];
}
}
Student *p = s;
print(p);
return 0;
}

void print (Student *p)
{
int j = 0;
for (int i = 0; i < 5; i ++)
{
cout<<"num: "<<*p -> s[i].num<<'\t'<<"name: "<<s[i].name<<'\t';
for (int j = 0; j <3; j ++)
cout<<"scores: "<<s[i].score[j]<<'\t';
cout<<endl;
}
return;
}


编译信息为
1>------ 已启动生成: 项目: ao, 配置: Debug Win32 ------
1>正在编译...
1>ao.cpp
1>e:\cpp\ao\ao.cpp(35) : error C2039: “s”: 不是“Student”的成员
1> e:\cpp\ao\ao.cpp(4) : 参见“Student”的声明
1>e:\cpp\ao\ao.cpp(35) : error C2228: “.num”的左边必须有类/结构/联合
1>e:\cpp\ao\ao.cpp(35) : error C2228: “.name”的左边必须有类/结构/联合
1>e:\cpp\ao\ao.cpp(37) : error C2228: “.score”的左边必须有类/结构/联合
1>生成日志保存在“file://e:\Cpp\ao\Debug\BuildLog.htm”
1>ao - 4 个错误,0 个警告
========== 生成: 成功 0 个,失败 1 个,最新 0 个,跳过 0 个 ==========
...全文
1477 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
蓝域小兵 2016-06-12
  • 打赏
  • 举报
回复
求给分求给分
蓝域小兵 2016-06-12
  • 打赏
  • 举报
回复
你传数组的首地址进去,那就直接按照数组的用法用就行了,你传的*p,就可以直接用p[1],p[2]...不用再使用指针的用法,而且p是数组的首地址指针,也是一个结构体的指针,*p就代表这个结构体,*p.num和p->num是等价的。*p->s[i].num这一句毫无来由。
蓝域小兵 2016-06-12
  • 打赏
  • 举报
回复
//2、编写一个函数print,打印一个学生的成绩数组,该数组中有5个学生的数据,每个学生的数据包括
//num学号,name姓名,score[3]三门课的成绩。用主函数输入这些数据,用print函数输出这些数据。
#include<iostream>
using namespace std;
struct Student
{
    int num;
    char name[20];
    int score[3];
};
struct Student s[5];
void print (Student *p);
 
int main()
{
    int j = 0;
    for (int i = 0; i < 5; i ++)
    {
        cout<<"please input the num and the name of the student:"<<i + 1;
        cin>>s[i].num>>s[i].name;
        for (int j = 0; j <3; j ++)
        {
            cout<<"please input the score of subject"<<j + 1<<" of  the student:"<<i + 1;
            cin>>s[i].score[j];
        }
    }
    struct Student *p = s;
    print(p);
    return 0;
}
 
void print (struct Student *p)
{
    int j = 0;
    for (int i = 0; i < 5; i ++)
    {
        cout<<"num: "<<p[i].num<<'\t'<<"name: "<<s[i].name<<'\t';
        for (int j = 0; j <3; j ++)
            cout<<"scores: "<<s[i].score[j]<<'\t';
        cout<<endl;
    }
    return;
}
楼主有几个错误的地方: 1,我记得结构体的声明和定义如果没有用typedef定义别名就需要在前面加上struct(此处存疑) 2,你传的是数组的首地址,传进去后直接用就行了,你用*p->s[i].num这一句明显是对指针和数组理解不够。
Topazpeach 2016-06-11
  • 打赏
  • 举报
回复
#include<iostream>
using namespace std;
struct Student
{
    int num;
    char name[20];
    int score[3];
};
void print (Student s[5])
{
    for (int i = 0; i < 5; i++)
    {
		cout<<"num: "<<s[i].num<<","<<"name: "<<s[i].name<<",";
        for (int j = 0; j <3; j ++)
            cout<<"scores: "<<s[i].score[j];
        cout<<endl;
    }
}
int main()
{
	Student s[5];
    for (int i = 0; i < 5; i ++)
    {
        cout<<"please input the num and the name of the student:"<<i + 1<<endl;
        cin>>s[i].num>>s[i].name;
        for (int j = 0; j <3; j ++)
        {
            cout<<"please input the score of subject"<<j + 1<<" of  the student:"<<i + 1;
            cin>>s[i].score[j];
        }
    }
    print(s);
    return 0;
}
 
没明白楼主为什么要用指针……
060 2016-06-08
  • 打赏
  • 举报
回复

for (int i = 0; i < 5; i ++)
    {
        cout<<"num: "<<s[i].num<<'\t'<<"name: "<<s[i].name<<'\t';
        for (int j = 0; j <3; j ++)
            cout<<"scores: "<<s[i].score[j]<<'\t';
        cout<<endl;
    }
在你的代码里void print (Student *p)的参数 p 是没用的,按照print的声明,print只是输出一个学生的成绩,而你的代码print输出了全部学生的成绩。 建议: 1、不要使用全局变量 Student s[5];,将其改为 main的局部变量,通过print输出每个学生的成绩。 2、pring改为

void print (Student *p)
{
    int j = 0;
        cout<<"num: "<<p ->num<<'\t'<<"name: "<<p->name<<'\t';
        for (int j = 0; j <3; j ++)
            cout<<"scores: "<<p->score[j]<<'\t';
        cout<<endl;
    return;
}
在main中循环调用print函数。
bolixin36 2016-06-08
  • 打赏
  • 举报
回复
第20行没有错啊,为什么第37行就错了

64,662

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

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