0
我想創建一個程序,它從.txt文件讀取三個變量(姓,UIN和他們的GPA)。該程序編譯,但是當我嘗試運行它時,它給我一個超出範圍的錯誤。有人可以告訴我發生了什麼,或者爲什麼會在我的程序中發生?嘗試從文件讀取時超出範圍錯誤?
#include "std_lib_facilities_4.h"
struct Student{
private:
string last_name;
int UIN;
double GPA;
public:
Student(string l_name, int number, double grade): last_name(l_name), UIN(number), GPA(grade){}
string getlast_name() const{return last_name;}
int getUIN() const {return UIN;}
double getGPA() const{return GPA;}
};
istream &operator >>(istream &in, Student &student){
string last_name;
int UIN;
double GPA;
char c1, c2;
in>>last_name>>UIN>>GPA;
student = Student{last_name, UIN, GPA};
return in;
}
ostream &operator <<(ostream &out, const Student &student){
return out<<student.getlast_name()<<" "<<student.getUIN()<<" "<<student.getGPA();
}
int main(){
vector<Student>vi;
int i = 0;
ifstream readStudent;
readStudent.open("student.txt");
while (readStudent.good()){
readStudent>>vi[i];
++i;
}
for(i=0; i<3; i++){
cout<<vi[i]<<endl;
}
}