各記録の名前、年齢、GPA を含むテキスト ファイルがあります。ファイル (クラス全体) の平均 gpa を計算する関数 getgpa を作成しました。問題は、フィールド 1 (名前)、フィールド 2 (年齢)、フィールド 3 (gpa) を抽出すると、クラス全体の平均 gpa を簡単に計算できることです。しかし、field3 だけを抽出しようとすると、結果がありません。各レコードの field3 (gpa) のみを抽出するにはどうすればよいですか? 抽出する私の関数はすぐ下にあります。Dev-C++ を使用しています。あなたの助けは非常に高く評価されます。ありがとう。
int student::getgpa()
{
double field3, gpa; double sumGPA=0; int count=0;
string field1; int field2;
char line[256];
cout<<"call of the function 'getgpa' " <<endl;
ifstream IS ("student.dat", ios::in);
while (!IS.eof())
{
if(IS>>field1>>field2>>field3) //where the problem is!
{
sumGPA=sumGPA+field3;
count++;
}
}
IS.close();
if (count>0) //just to make sur not to divide by zero!!
cout<<"gpa of the students:" <<sumGPA/count<<endl;
}