我正在尝试创建一个学生对象,它采用姓名、身份证、电子邮件和三个整数成绩。
我的代码很简单,如下所示:
studentObj* newStudent = new studentObj;
cout << "Student First Name: ";
getline(cin, newStudent->name);
cout << "Student ID: ";
getline(cin, newStudent->id);
cout << "Student Email: ";
getline(cin, newStudent->email);
cout << "Grade 1: ";
cin >> newStudent->gradeOne;
cout << "Grade 2: ";
cin >> newStudent->gradeTwo;
cout << "Term Grade: ";
cin >> newStudent->termGrade;
cout << "Student Name: " + newStudent->name << endl;
cout << "Student ID: " + newStudent->id << endl;
cout << "Student Email: " + newStudent->email << endl;
cout << "Grade 1: " + newStudent->gradeOne << endl;
cout << "Grade 2: " + newStudent->gradeTwo << endl;
我以为这会完美运行,但不幸的是事实并非如此。getline()
混合和似乎是一个问题cin
。
输出是:
Student Name: Test Tester
Student ID: abcdef
Student Email: email@test.com
rade 1:
ade 2:
m Grade:
我试过cin.ignore(numeric_limits<streamsize>::max(),'\n');
在几个地方添加,但没有运气。有什么建议么?`