Hi, I guess we're in different timezones, hence the delayed reply.
There are several cases where you aren't making use of the information which is already there.
A minor point, take a look at these two lines:
1 2
|
const int maxRecords = 100;
int index[100];
|
Notice the value 100 is stated on both lines. Now what happens if you need to change the 100 to different number? You need to remember to change it in both places.
It's a better design to do this:
1 2
|
const int maxRecords = 100;
int index[maxRecords];
|
Now, if you change the value 100 to some other number, all of the arrays will adjust to the new size and keep in step with one another.
Later on, there is another, more important example of using a "magic number" typed in multiple places throughout the code.
Take a look at this code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
for (i=0;i<5;i++)
{
cout << setw(10) << names[i] << setw(20) << scores[i] << endl;
}
for (i=0;i<5;i++)
{
index[i]=i;
}
for (i=0;i<5;i++)
{
for (j=i+1;j<5;j++)
{
|
See how the value '5' occurs in multiple different places? What happens if you need to change it? You would need to change the code in multiple places - always a risky procedure, for two reasons:
• You might miss an occurrence and forget to change it.
• The value '5' might be in the code for some other reason, for example it might specify the width of a column, or the number of decimal places etc. Thus there is a risk of accidentally changing some unrelated figure.
The solution - use a named constant or variable and type its name in each place where it is required.
Now as it happens, 5 is the wrong figure, that explains why the output contains all sorts of strange characters. It also happens that the required value is already held in a variable named
count
. This variable was used to count how many records were read from the input file, hence the name. So replace the figure 5 with the variable count in each case:
1 2
|
for (i=0; i<count; i++)
{
|
etc. etc.
Now, the calculation of the average. There are two problems here.
This line is standing alone
It should be inside a loop, in order to accumulate the total for all the records. Also, since it isn't inside the loop, what is the value of
i
? It is whatever value caused the previous for-loop to terminate. in other words it is the index of an array element just past the last record.
Secondly, the function average is called like this:
average (sum,size)
What is size? it is initialised with the value 4. What happens if the file contains a different number of records? If only we had a variable somewhere which contained a count of the number of records which were actually read from the file... see where this is going? Get rid of size and replace it with count.
And lastly we come to the output file. There are a couple of issues.
1 2 3
|
ofstream fout ("project6-edited.dat", ios::binary);
fout.write ((char*)names[index[i]], sizeof(20) );
fout.write ((char*)scores[index[i]], sizeof(int) );
|
One is this:
sizeof(20)
, 20 is an integer, so the expression is the same as
sizeof(int)
which is not what is required. What you need here is the length of the char strings, which you could find using either
sizeof(names[0])
or directly as it is specified in the constant
nameLength
This line needs to pass the address of the variable to be output:
1 2 3
|
fout.write ((char*)scores[index[i]], sizeof(int) );
// cange it to:
fout.write ((char*) &scores[index[i]], sizeof(int) ); // notice & operator
|
and of course the
write()
statements need to be inside a loop, in order to output all of the records.