程序出错,错在哪里啊?
程序编译过,但运行输入时有错误,怎么改才能正常运行?
代码如下:
#include <iostream>
using namespace std;
const int SLEN=30;
struct student
{
char fullname[SLEN];
char hobby[SLEN];
int ooplevel;
};
int getinfo(student pa[],int n);
/*getinfo() has two arguments:apointer to the first element of
an array of student structures and an int representint the
number of elements of the array.The function solicts and stores
data about students.It terminates input upon filling
the array of upon encountering a blank line for the student name
The function returns the actual number of array elements filled*/
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;
while(cin.get()!= '\n ')
continue;
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)
{
char ch;
for(int i=0;i <n;i++)
{
cout < < "Fullname:\n ";
cin.getline(pa[i].fullname,SLEN);
cin.getline(&ch,1);
cout < < "Hobby:\n ";
cin.getline(pa[i].hobby,SLEN);
cin.getline(&ch,1);
cout < < "Opplevel:\n ";
cin> > pa[i].ooplevel;
}
return i;
}
void display1(student st)
{
cout < < "Fullname: " < <st.fullname < <endl;
cout < < "Hobby: " < <st.hobby < <endl;
cout < < "Ooplevel " < <st.ooplevel < <endl;
}
void display2(const student *ps)
{
cout < < "Address of Fullname: " < <ps-> fullname < <endl;
cout < < "Address of hobby: " < <ps-> hobby < <endl;
cout < < "Address of ooplevel: " < <ps-> ooplevel < <endl;
}
void display3(const student pa[],int n)
{
for(int i=0;i <n;i++)
{
cout < < "Address of First element of an array:\n "
< < "Address of Fullname: " < <pa[0].fullname < <endl;
cout < < "Address of Fullname: " < <pa[0].hobby < <endl;
}
}
[解决办法]
输入输出流的问题,缓冲没处理好,你参考下
#include <iostream>
using namespace std;
const int SLEN=30;
struct student
{
char fullname[SLEN];
char hobby[SLEN];
int ooplevel;
};
int getinfo(student pa[],int n);
/*getinfo() has two arguments:apointer to the first element of
an array of student structures and an int representint the
number of elements of the array.The function solicts and stores
data about students.It terminates input upon filling
the array of upon encountering a blank line for the student name
The function returns the actual number of array elements filled*/
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;
cin.ignore(1);
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)
{
for(int i=0;i <n;i++)
{
cout < < "Fullname: ";
cin.getline(pa[i].fullname,SLEN);
cout < < "Hobby: ";
cin.getline(pa[i].hobby,SLEN);
cout < < "Opplevel: ";
cin> > pa[i].ooplevel;
cin.ignore(1);
}
return i;
}
void display1(student st)
{
cout < < "\n Call display1() " < < endl;
cout < < "Fullname: " < <st.fullname < <endl;
cout < < "Hobby: " < <st.hobby < <endl;
cout < < "Ooplevel: " < <st.ooplevel < <endl;
}
void display2(const student *ps)
{
cout < < "\n Call display2() " < < endl;
cout < < "Address of Fullname: " < < ps-> fullname < < endl;
cout < < "Address of hobby: " < < ps-> hobby < < endl;
cout < < "Address of ooplevel: " < < ps-> ooplevel < < endl;
}
void display3(const student pa[],int n)
{
cout < < "\n Call display2() " < < endl;
for(int i=0;i <n;i++)
{
cout < < "Address of First element of an array:\n "
< < "Address of Fullname: " < < pa[0].fullname < < endl;
cout < < "Address of Fullname: " < < pa[0].hobby < < endl;
}
}