途中でノードを削除するときに問題があります。真ん中にポジションを置くと、前のノードはすべて消えてしまいます。ありがとうございました。LinkList C++ノードを削除する
フロントのものを削除しても問題ありませんが、途中で問題があります。
今すぐ停止しました。
#include<iostream>
#include<string>
#include <limits>
using namespace std;
struct Student{
string name;
int matricNo;
string course;
double cgpa;
Student* link;
};
int main(){
Student *head = NULL, *last, *newStudent, *target;
int menu = 0;
int select;
while(menu != 6){
cout << "Student Database.\n";
cout << "1.Add a student.\n";
cout << "2.Delete a student.\n";
cout << "3.View a student's information.\n";
cout << "4.View all students' information.\n";
cout << "5.View all students' information with CGPA of 3.0 or higher.\n";
cout << "6.End program.\n";
while(!(cin >> menu)){
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "Invalid input.\n";
}
if(menu == 1){
newStudent = new Student;
if(head == NULL)
head = newStudent;
cin.clear();
cin.ignore(2000,'\n');
cout << "Please enter the student's name : ";
getline(cin, newStudent -> name);
cin.clear();
cout << "Please enter the Matric Number : ";
while(!(cin >> newStudent -> matricNo)){
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "Invalid input. Please enter a number.\n";
}
cin.clear();
cin.ignore(2000,'\n');
cout << "Please enter the Course : ";
getline(cin, newStudent -> course);
cin.clear();
cout << "Please enter the student's CGPA : ";
while(!(cin >> newStudent -> cgpa) || newStudent -> cgpa > 4 || newStudent -> cgpa < 0){
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "Invalid input. Please enter a value between 0.00 and 4.00\n";
}
newStudent -> link = NULL;
if(last != NULL)
last -> link = newStudent;
last = newStudent;
system("cls");
}
if(menu == 2){
if(head != NULL){
cout << "Please enter the matric number of a student : ";
while(!(cin >> select)){
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "Invalid input.\n";
}
for(Student* p = head; p != NULL; p = p -> link){
if(p -> matricNo == select){
target = p;
if(head != NULL)
head = p -> link;
target -> link = NULL;
delete target;
}
}
}
else if(head == last){
head -> link=NULL;
last -> link=NULL;
}
else
cout << "No students in the database.\n";
}
if(menu == 3){
if(head != NULL){
cout << "Please enter the matric number of a student : ";
while(!(cin >> select)){
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "Invalid input.\n";
}
for(Student* p = head; p != NULL; p = p -> link){
if(p -> matricNo == select){
cout << "Student's Name : " << p -> name << endl;
cout << "Matric Number : " << p -> matricNo << endl;
cout << "Course : " << p -> course << endl;
cout << "CGPA : " << p -> cgpa << endl;
cout << "==================================\n";
}
}
}
else
cout << "No students in the database.\n";
}
if(menu == 4){
if(head != NULL){
for(Student* p = head; p != NULL; p = p -> link){
cout << "Student's Name : " << p -> name << endl;
cout << "Matric Number : " << p -> matricNo << endl;
cout << "Course : " << p -> course << endl;
cout << "CGPA : " << p -> cgpa << endl;
cout << "==================================\n";
}
}
else
cout << "No students in the database.\n";
}
if(menu == 5){
if(head != NULL){
for(Student* p = head; p != NULL; p = p -> link){
if(p -> cgpa >=3){
cout << "Student's Name : " << p -> name << endl;
cout << "Matric Number : " << p -> matricNo << endl;
cout << "Course : " << p -> course << endl;
cout << "CGPA : " << p -> cgpa << endl;
cout << "==================================\n";
}
}
}
else
cout << "No students in the database.\n";
}
if(menu == 6)
return 0;
}
system("PAUSE");
return 0;
}
、あなたのタブ移動は助けない、一貫性がありません – UKMonkey
['system(" Pause ")'](http://stackoverflow.com/questions/1107705/systempause-why-is-it-wrong)や['using namespace std;']を使わないでください。 (http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) – amanuel2