#include<iostream> //#include<cstring> //为什么这里注释了程序还能正常运行?程序里有strcpy、strlen using namespace std; const int SLEN = 30; struct student { char fullname[SLEN]; char hobby[SLEN]; int ooplevel; }; 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; 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) { int num_array_elem = n; char temp[SLEN]; for (int i = 0; i < n; i++) { cout << "Enter name: "; cin.getline(temp, SLEN); bool blank_line = true; for (unsigned j = 0; j < strlen(temp); j++)//strlen { if (!isspace(temp[j])) { blank_line = false; break; } } if (blank_line) { num_array_elem = i; break; } strcpy_s(pa[i].fullname, temp); //strcpy cout << "Enter hobby: "; cin.getline(pa[i].hobby, SLEN); cout << "Enter ooplevel: "; cin >> pa[i].ooplevel; cin.get(); //这一句什么作用? } cout << endl; return num_array_elem; } void display1(student st) { cout << st.fullname << '\t' << st.hobby << '\t' << st.ooplevel << endl; } void display2(const student *ps) { cout << ps->fullname << '\t' << ps->hobby << '\t' << ps->ooplevel << endl; } void display3(const student pa[], int n) { for (int i = 0; i < n; i++) { cout << pa[i].fullname << '\t' << pa[i].hobby << '\t' << pa[i].ooplevel << endl; } }
木柘