Em viết code nhưng lúc chạy chương trình thì có hiện lỗi “vector subscript out of range”. Em mong mn xem code của em và chỉ ra lỗi sai chỗ nào dùm e ạ. Em cảm ơm mn nhiều ạ.
#include<iostream>
#include<fstream>
#include<string>
#include<vector>
using namespace std;
class Student
{
private:
string name;
string phoneNumber;
float GPA;
public:
Student()
{
}
Student(string name, string phoneNumber, float GPA)
{
this->name = name;
this->phoneNumber = phoneNumber;
this->GPA = GPA;
}
void insertStudent(vector<Student> student);
int checkName(vector<Student> student,int n);
void deleteStudent(vector<Student> student, int n);
void sort(vector<Student> student, int n);
void printStudentInfo(vector<Student> student, int id);
};
int Student::checkName(vector<Student> student,int n)
{
for (int i = 0; i < n; i++)
{
if (name == student[i].name)
return 1;
else return -1;
}
}
void Student::insertStudent(vector<Student> student)
{
string temp;
getline(cin, temp);
Student a;
cout << "Input name of student: ";
getline(cin, a.name);
cout << "Input phone number of student: ";
cin >> a.phoneNumber;
cout << "Input GPA of student: ";
cin >> a.GPA;
}
void Student::deleteStudent(vector<Student> student, int n)
{
string name;
Student a;
cout << "Input name of student: ";
getline(cin, a.name);
if (checkName(student, n) != 1)
{
cout << "This student is not in the class";
}
//student.erase(Student a +1);
}
void Student::sort(vector<Student> student, int n)
{
Student tmp;
for (int i = 0; i < n; ++i)
{
for (int j = i + 1; j < n; ++j)
{
if (student[i].GPA < student[j].GPA)
{
tmp = student[i];
student[i] = student[j];
student[j] = tmp;
}
}
}
}
void Student::printStudentInfo(vector<Student> student, int n)
{
for (int i = 1; i < n; i++)
{
cout << "Name: " << student[i].name << endl;
cout << "Phone number: " << student[i].phoneNumber << endl;
cout << "GPA: " << student[i].GPA << endl;
}
}
int main()
{
Student a;
vector<Student> st;
int n, choice;
string name, phoneNumber;
float GPA;
fstream myFile;
myFile.open("LopHoc.txt", fstream::in);
if (myFile.fail())
cout << "Failed to open this file!" << endl;
myFile >> n;
//for (int i = 0; i < n; i++)
getline(myFile, name);
myFile >> phoneNumber;
myFile >> GPA;
cout << "================MENU================" << endl;
cout << "0. Exit." << endl;
cout << "1. Add new student." << endl;
cout << "2. Delete a student." << endl;
cout << "3. Sort the list of students." << endl;
cout << "4. Export all the information of all students in the class." << endl;
cout << "================END================" << endl;
do
{
cout << "Enter your choice: ";
cin >> choice;
switch (choice)
{
case 0:
break;
case 1:
cout << "Input information of new student." << endl;
a.insertStudent(st);
if (a.checkName(st, n) == 1)
st.push_back(a);
else
cout << "This student is in the class." << endl;
break;
case 2:
a.deleteStudent(st, n);
break;
case 3:
cout << "List of students after sorting descending:" << endl;
a.sort(st, n);
break;
case 4:
cout << "Information all the students." << endl;
a.printStudentInfo(st, n);
break;
}
} while (choice);
myFile.close();
return 0;
}