2016-11-01 4 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()'를 두 번 호출한다. 이것은 파일의 끝까지 읽는다. 그런 다음 코드는'read()'를 다시 호출하고 반환 값을'print()'에 전달합니다. read()에 대한 두 번째 호출이 얼마나 많은 레코드를 읽었을 것이라고 생각합니까? –

+0

Oh duh는 크기에 대한 나의 상수를 잊어 버렸습니다. 내 출력 파일은 이제 물건을 반환하지만 대신 이름과 평균 학년 그냥 재잘 거림입니다. 출력은 "Coop, Jason = 27.31"의 줄을 따라 있다고 가정되지만 임의의 숫자와 문자가 혼합 된 것입니다. –