2013-06-04 3 views
0

컴파일하지 않을 코드가 있습니다.C++ 바이너리 파일 정렬

#include "stdafx.h" 
#include <iostream> 
#include <fstream> 
#include <string> 
#include <Windows.h> 
#include <iomanip> 
using namespace std; 


int average (int n); 

int main() 
{ 

string filename; 

int i,j; 
int n = 0; 
int sum = 0; 
int size = 4; 
const int nameLength = 19; 
const int maxRecords = 100; 
int index[100]; 


int scores[maxRecords]; 
char names[maxRecords][nameLength]; 

int count = 0; 

cout << "Enter binary data directory and file name: "; 
getline (cin, filename); 
// Open file for binary read-write access. 
    ifstream fin(filename.c_str(), ios::binary); 

if (!fin) { 
cout << "Could not open " << filename << endl; 
system("PAUSE"); 
return -1; 
} 

// Read data from the file. 
while (
fin.read(names[count], sizeof(names[0])) 
    && fin.read((char *) &scores[count], sizeof(scores[0])) 
) 
{ 

    count++; 
    fin.ignore(); // skip the padding byte 
} 


// Display the data and close. 
cout << "Your file's data unsorted: " << endl; 
cout << endl; 
    cout << setw(10) << "Name" << setw(20) << "Test Score" << endl; 

    for (i=0;i<n;i++){ 
      cout << setw(10) << names[i] << setw(20) << scores[i] << endl; 
    } 

      for (i=0;i<n;i++) 
    { 
      index[i]=i; 
    } 

    for (i=0;i<n;i++) 
    { 

      for (j=i+1;j<n;j++) 
      { 
        int temp; 
        if (scores[index[i]] > scores[index[j]]) 
        { 
          temp = index[i]; 
          index[i] = index[j]; 
          index[j] = temp; 
        } 
      } 
    } 

    cout << "The average of the test scores in your file is: " << average   (sum); 

sum=sum+scores[i]; 


fin.close(); 
system("PAUSE"); 
return 0; 
} 

int average (int sum, int size) 
{ 
return sum/size; 
} 

나는 말한다 컴파일 오류 받고 있어요 : fatal error LNK1120: 1 unresolved externals, 그리고 그 이유를 알아낼 수 없습니다. 또한 또 다른 질문은 원래 바이너리 파일에서 읽은 데이터가 훼손되지 않고 출력되고 새로운 편집 된 바이너리 파일에 저장되도록 어떻게 포맷합니까?

int average (int n); 

하지만 당신은 이런 식으로 평균 구현 :

+0

정확한 오류 메시지 란 무엇인가요? –

+0

정확하게 ''은 포함되었지만 ''은 포함되지 않는 이유는 무엇입니까? 나는 여기서 전자를 보지 못했지만'system'은 후자의 것입니다.'system ("PAUSE")는 코드가 가져야하는 것이 아닙니다. – chris

+0

오류 메시지에 "치명적인 오류 LNK1120 : 1 개의 해결되지 않은 외부" "가 있습니다. 이전에 을 가지고 있었기 때문에 거기서 MAX_PATH 일을 꺼 냈습니다. – user2444400

답변

0

당신은 앞으로과 같이 average를 선언 당신이 당신의 앞으로 선언을 업데이트하는 경우

int average (int n, int size); 

을하고 여기에 두 번째 인수를 추가

cout << "The average of the test scores in your file is: " << average(sum,size); 

고쳐야합니다.

+0

좋아 감사를 고정,하지만 지금이 내 출력은 다음과 같습니다 C : 바이너리 데이터 디렉토리와 파일 이름을 입력 이름 테스트의 평균 점수 : \ 사용자 재키 \ 바탕 화면 \이 파일의 데이터가 정렬되지 않은 을 project6.dat \ 파일의 테스트 점수는 다음과 같습니다. 0 계속하려면 아무 키나 누르십시오. . . – user2444400

+2

이제 프로그램을 수정해야합니다. :) – Joe

+0

그래,하지만 출력을 망쳐 놓는 내 분류와 함께 할 수 있을까? – user2444400