を何をするか分からない:(。各フォーマットで同じことを見て、ラインの未知の量があるであろう)構造体のベクタと私はテキストの各行について
-Get first name
-Get last name
-Get Student Number
フォーマット:
First Last 111111111
First Last 111111112
...
私はそれぞれの生徒のそれぞれを独自の構造にしたいと思っています。次のように私は、構造体を設定している次のように
struct Student{
string lastName;
string firstName;
string stdNumber;
double assgn1;//doubles will be used later in prog.
double assign2;
double assign3;
double assign4;
double midTerm;
double finalGrade;
};
マイファイル入力機能がある:ファイル入力で
int getFileInfo()
{
int failed=0;
ifstream fin;
string fileName;
vector<Student> students;
Student s; // A place to store data of one student
cout<<"Please enter the filename of the student grades (ex. filename_1.txt)."<<endl;
do{
if(failed>=1)
cout<<"Please enter a correct filename."<<endl;
cin>>fileName;
fin.open(fileName.c_str());// Open the file
failed++;
}while(!fin.good());
while (fin >> s.firstName >> s.lastName >> s.stdNumber)
students.push_back(s);
fin.close();
return 0;
}
、私はベクトルを作るが、私はアクセスする方法がわかりませんよそれぞれの個々の部分、または学生、s
、それは1人の学生を作るように見えるので。私はファイルの各行が分割されてstudents
ベクトルに入力されたと言われましたが、その情報の抽出方法はわかりません。それぞれの学生をstudents
ベクトルとそれ自身の構造体からどのようにして得るのですか?各学生をそれ自身の構造体として使うことができますか? だから最後に、私が出力できるようにしたいです。助けを事前に
Student1: First Last 111111111
Student2: First Last 111111112
However More students are in the file
感謝!!
@Loki ループをloop != end
に変更しましたが、同じ問題が発生しました。ここに私のコードです:再び
int getFileInfo()
{
int failed=0;
ifstream fin;
string fileName;
vector<Student> students;// A place to store the list of students
vector<Student>::iterator loop = students.begin();
vector<Student>::iterator end = students.end();
Student s; // A place to store data of one student
cout<<"Please enter the filename of the student grades (ex. filename_1.txt)."<<endl;
do{
if(failed>=1)
cout<<"Please enter a correct filename."<<endl;
cin>>fileName;
fin.open(fileName.c_str());// Open the file
failed++;
}while(!fin.good());
while (fin >> s.firstName >> s.lastName >> s.stdNumber){
cout<<"Reading "<<s.firstName<<" "<<s.lastName<<" "<<s.stdNumber<<endl;
students.push_back(s);
}
fin.close();
for(loop;loop!=end;++loop)
cout<<loop->firstName<<" "<<loop->lastName<<" "<<loop->stdNumber<<endl;
return 0;
}
、Visual Studioは
が指差す、vector iterators incompatible
を言っているに "C:\プログラムファイル(x86の)は、MicrosoftのVisual Studio \ 10.0 \ VCの\は\含まベクトル"
[はifstream ::読みを()]を読んfancy.Justナッシング(http://www.cplusplus.com/reference/iostream/ istream/read /)をファイルから取り出し、[vector :: push_back](http://www.cplusplus.com/reference/stl/vector/push_back/)を使用してベクトルに保存します。 –
私は既にデータファイルをテキストファイルからベクトルに取り除いています。私は問題を抱えている構造体のベクトルから保存されたデータを取得しています。 – b3orion
私はあなたに役立つ希望の答えを追加しました。 –