2016-11-01 17 views
2

私が持っている課題のコードです。私が試してコンパイルするたびに、 "ios_base.h"の何かのために私の読み込み関数にエラーが発生します。私は何をすればよいかわかりませんし、コードがファイルを取り出して要素を別々の名前と平均が隣り合っているファイル。filestreamを関数パラメータとして渡すことはできませんか?

#include <iostream> 
#include <fstream> 
#include <sstream> 
#include <iomanip> 

using namespace std; 

struct Student 
{ 
    string fname; 
    string lname; 
    double average; 
}; 

int read(ifstream, Student s[]); 

void print(ofstream fout, Student s[], int amount); 


int main() 
{ 
    const int size = 10; 
    ifstream fin; 
    ofstream fout; 
    string inputFile; 
    string outputFile; 
    Student s[size]; 

    cout << "Enter input filename: "; 
    cin >> inputFile; 
    cout << "Enter output filename: "; 
    cin >> outputFile; 
    cout << endl; 

    fin.open(inputFile.c_str()); 
    fout.open(outputFile.c_str()); 

    read(fin , s); 
    print(fout, s, read(fin, s)); 

} 

int read(ifstream fin, Student s[]) 
{ 
    string line; 
    string firstName; 
    string lastName; 
    double score; 
    double total; 
    int i=0; 
    int totalStudents=0; 
    Student stu; 

    while(getline(fin, line)){ 
     istringstream sin; 
     sin.str(line); 

     while(sin >> firstName >> lastName){ 
      stu.fname = firstName; 
      stu.lname = lastName; 

      while(sin >> score){ 
      total *= score; 
      i++; 
      } 
      stu.average = (total/i); 
     } 
     s[totalStudents]=stu; 
     totalStudents++; 
    } 
    return totalStudents; 
} 

void print(ofstream fout, Student s[], int amount) 
{ 
    ostringstream sout; 
    for(int i = 0; i<amount; i++) 
    { 
     sout << left << setw(20) << s[i].lname << ", " << s[i].fname; 
     fout << sout << setprecision(2) << fixed << "= " << s[i].average; 
    } 
} 
+0

は、実際のエラーコードを記載してください。 – Koga

答えて

3

ストリームオブジェクトはコピーできません。コピーコンストラクタは削除されます。彼らは、値ではなく参照によって渡されている必要があります

int read(ifstream &, Student s[]); 

void print(ofstream &fout, Student s[], int amount); 

などが...

+0

ありがとう、私はそれをコンパイルするようにしました。今のところ問題は出力ファイルには何も入っていないが空白行だけであるということですか?私は適切に配列を埋めることを知っているし、出力ストリームに入れては? –

+0

コードにバグがあります。 'read()'を2回呼び出します。これはファイルの最後まで読み込みます。その後、 'read()'をもう一度呼び出し、その戻り値を 'print()'に渡します。 read()の2回目の呼び出しで、いくつのレコードが読み込まれると思いますか? –

+0

ああ、私の定数は忘れていた。私の出力ファイルは今戻っているものですが、名前と平均グレードの代わりに、ちょっとひどいです。出力は "Coop、Jason = 27.31"の行に沿っていると仮定していますが、乱数と文字を混ぜたものに過ぎません。 –

関連する問題