0

I have a piece of code that won't compile.

#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;
}

I'm getting a compile error that says: fatal error LNK1120: 1 unresolved externals, and I can't figure out why. Also another question is how do you format it so that the data read from the original binary file does not get tampered and is then outputted and saved in a new edited binary file?

Valerij
  • 27,090
  • 1
  • 26
  • 42
user2444400
  • 139
  • 5
  • 14
  • What is the exact error message? – Oliver Charlesworth Jun 04 '13 at 02:00
  • Why exactly is `` included, but not ``? I don't see anything from the former in here, but `system` is from the latter, not that `system("PAUSE")` is something code should have. – chris Jun 04 '13 at 02:05
  • The Error message says, ""fatal error LNK1120: 1 unresolved externals"", and I had in there earlier cause I had a MAX_PATH thing in there that I took out. – user2444400 Jun 04 '13 at 02:09
  • @user2444400, http://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix – chris Jun 04 '13 at 02:10
  • @user2444400, And I only ask about `` because it's extremely large. It's a huge set of headers. – chris Jun 04 '13 at 02:15
  • Yeah, since I don't have any use of it I just removed the header. i fixed the compile issue, but now I have an output issue where it just outputs nothing. – user2444400 Jun 04 '13 at 02:26

1 Answers1

0

You forward declare average like so:

int average (int n);

but you implement average this way:

int average (int n, int size);

If you update your forward declaration and you add the second argument here:

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

that should fix it.

Shafik Yaghmour
  • 154,301
  • 39
  • 440
  • 740
  • Alright thanks that fixed it, but now this is my output: Enter binary data directory and file name: C:\Users\Jacky\Desktop\project6.dat Your file's data unsorted: Name Test Score The average of the test scores in your file is: 0Press any key to continue . . . – user2444400 Jun 04 '13 at 02:17
  • 2
    Now you have to fix your program. :) – Joe Jun 04 '13 at 02:22
  • Yeah, but could it be with my sorting that's screwing the output up? – user2444400 Jun 04 '13 at 02:24
  • @user2444400 A couple of hints: Read your input loop carefully. Then check the bounds in your output and sorting loops just as carefully. And look at how you're computing the sum. – molbdnilo Jun 04 '13 at 11:42