嘿伙计们,我正在处理我的第一个链接列表以保存学生记录(名称 id gpa addr),但我遇到了错误,我想知道你们是否能发现错误?
#include <iostream>
#include <string>
using namespace std;
int main()
{
struct Student
{
string name;
string address;
double id;
double gpa;
Student *next;
};
Student *head;
head = NULL;
string Student_name;
double Student_id;
double Student_gpa;
string Student_address;
for (int i = 0; i<20; i++)
{
cout << "What is the student's name?";
getline (cin, Student_name);
cout << "What is " << Student_name << "'s ID Number?";
cin >> Student_id;
cout << "What is " << Student_name << "'s GPA?";
cin >> Student_gpa;
cout << "What is " << Student_name << "'s address?";
getline (cin, Student_address);
}
Student *newStudent;
Student *Student_ptr;
newStudent = new Student;
newStudent->name = Student_name;
newStudent->id = Student_id;
newStudent->gpa = Student_gpa;
newStudent->address = Student_address;
newStudent->next = NULL;
if (!head)
head = newStudent;
else
{
Student_ptr = head;
while (Student_ptr -> next)
Student_ptr = Student_ptr->next;
Student_ptr->next = newStudent;
}
cout << endl;
Student *Display_ptr;
Display_ptr = head;
while (Display_ptr)
{
cout << Display_ptr-> name << endl;
cout << Display_ptr-> id << endl;
cout << Display_ptr-> gpa << endl;
cout << Display_ptr-> address << endl;
Display_ptr = Display_ptr->next;
cout << endl;
}
return 0;
}