输入空格退不出循环,要把结构属性填满才能退出
#include<iostream>using namespace std;
const int SLEN = 30;
struct student
{
char fullname[SLEN];
char hobby[SLEN];
int ooplevel;
};
//getinfo ()有两个参数:指向学生结构数组第一个元素的指针和表示数组元素数的int。
//该功能请求并存储有关学生的数据。它在填充数组时或遇到学生姓名的空行时终止输入。函数返回实际填充的数组元素数。
int getinfo(student pa[], int n);
//display 1 ()以学生结构为参数并显示其内容
void display1(student st);
//display 2 ()以学生结构的地址作为参数并显示结构的内容
void display2(const student*ps);
//display 3 ()将学生结构数组的第一个元素的地址和数组元素的数量作为参数,并显示结构的内容
void display3(const student pa[], int n);
int main()
{
cout << "输入类别大小: ";
int class_size;
cin >> 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";
system("pause");
return 0;
}
int getinfo(student pa[], int n) //char fullname;hobby,,ooplevel
{
int i;
for (i = 0; i < n; i++)
{
cout << "第" << i + 1 << "个学员的姓名:";
cin.get(pa->fullname, SLEN).get(); //输入学员姓名
int j;
for (j = 0; pa->fullname[j] != '\0'; j++) //检测学员姓名有无空格 有空格 退出循环
{
if (pa->fullname[j] == ' ')
break;
}
if (pa->fullname[j] != '\0') //如果fullname[j]变量是空格 就退出循环
break;
cout << "第" << i + 1 << "个学员的爱好:";
cin.get(pa->hobby, SLEN).get();
cout << "第" << i + 1 << "个学员的分数:";
while (!(cin >> pa->ooplevel))
{
if (cin.fail())
cin.clear();
cin.sync();
cout << "输入错误!重新输入:";
}
cin.get();
}
return i;
}
void display1(student st) //char fullname;hobby,,ooplevel
{
cout << "学员姓名: " << st.fullname << endl;
cout << "学员爱好: " << st.hobby << endl;
cout << "学员分数: " << st.ooplevel << endl;
}
void display2(const student*ps)
{
cout << "学员姓名: " << ps->fullname << endl;
cout << "学员爱好: " << ps->hobby << endl;
cout << "学员分数: " << ps->ooplevel << 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].ooplevel << endl;
}
}