返回信息流程序的目的是输入一个班同学的信息到student结构并显示
#include <iostream>
using namespace std;
const int SLEN = 30;
struct student{
char fullname[SLEN];
char hobby[SLEN];
int coplevel;
};
int getinfo(student pa[], int n);
void display1(student st);
void display2(const student*ps);
void display3(const student pa[], int n);
int main()
{
cout << "Enter class size:";
int class_size;
cin >> class_size;
student* ptr_stu = new student[class_size];
int entered = getinfo(ptr_stu, class_size);
for (int i = 0; i < entered; i++)
{
display1(ptr_stu[i]);
display2(&ptr_stu[i]);
}
display3(ptr_stu, entered);
delete[]ptr_stu;
cout << "Done\n";
return 0;
}
int getinfo(student pa[], int n)
{
int i;
for ( i = 0; i < n; i++)
{
cout << "Enter the name of student " << i + 1 << ":";
cin.getline(pa[i].fullname, SLEN);
cin.get();
cout << endl;
cout << "Enter the hobby of student " << i + 1 << ":";
cin.getline(pa[i].hobby, SLEN);
cout << endl;
cin.get();
cout << "Enter the opplever of student " << i + 1 << ":";
cin >> pa[i].coplevel;
cin.get();
cout << endl;
}
return i;
}
void display1(student st)
{
cout << st.fullname << endl;
cout << st.hobby << endl;
cout << st.coplevel << endl;
}
void display2(const student*ps)
{
cout << ps->fullname << endl;
cout << ps->hobby << endl;
cout << ps->coplevel << endl;
}
void display3(const student pa[], int n)
{
for (int i = 0; i < n; i++)
{
cout << pa[i].fullname << endl;
cout << pa[i].hobby << endl;
cout << pa[i].coplevel << endl;
}
}
这是一条镜像帖。来源:北邮人论坛 / cpp / #87385同步于 2015/6/5
CPP机器人发帖
帮忙看看问题出在了哪里,只输入了第一位同学的姓名爱好就显示
herbice
2015/6/5镜像同步0 回复
订阅后,新回复会通过你的通知中心匿名送达。
0 条回复
暂无回复 · 你可以订阅本帖等待新回复。