DUE TONIGHT Can someone fix my array for a file I created?

Hi. I have finished working on a code that requires me to create a file that displays all records from a disc file.

My only issue is that for some reason, the outFile << sortArray(SIZE, values) lines do not work. Can someone please tell me why and help me fix it before I submit it to my professor? I've been stuck on this part of my project and it's due in nearly a few hours. I've tried almost everything. The output for the file I created is supposed to read from the input.

It should look like this from the input (creating a new file called "SortingDiscFiles.txt":

The records in the original file are:

Student 1: Sanna Saunders	89
Student 2: Kelly Handley	95
Student 3: Arif Crane			76

The sorted records are:

Student 3: Arif Crane			76
Student 1: Sanna Saunders	89
Student 2: Kelly Handley	95

The class average is: 86.6667

Here is my code below, if anyone can take a look at it:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

struct students{
string studentName; //allows student name/characters.
float testScore; //a number that allows decimals (required when finding average).
};

//functions used to sort student names according to the test scores.
void sortArray (int size, students array[]);
void showArray (int size, const students array []);

int main(){

  ofstream outFile;
  outFile.open("SortingDiscFiles.txt");

  float average, sumTestScore; //considers decimals
  const int SIZE = 3; //SIZE = # of students.

  students values[SIZE];
  
  values[0].studentName = "\n1 Sanna Saunders\t";
  values[0].testScore = 89;
  
  values[1].studentName = "\n2 Kelly Handley\t\t";
  values[1].testScore = 95;
  
  values[2].studentName = "\n3 Arif Crane\t\t";
  values[2].testScore = 76;
  
  //Displays values and names that aren't sorted.
  outFile << "\nThe records in the original file are:\n";
  outFile << showArray (SIZE, values); //original file.
  
  //Sorts and holds the values and names in ascending order.
  sortArray (SIZE, values);
  
  //Display the values and names again, but now in ascending order.
  outFile << "\nThe sorted records are:\n";
  outFile << showArray (SIZE, values); //calls the function that sorted values.
  
  //calculates and holds average.
  sumTestScore = (values[0].testScore) + (values[1].testScore) + (values[2].testScore);
  average = sumTestScore / SIZE;
  //displays average.
  outFile << "\nThe class average is: " ;
  outFile << average;
}

//Calculates the ascending order between the test scores and name using arrays.
void sortArray (int size, students array []){
  bool swapped;

  do{
    swapped = false;
    for (int count = 0; count < (size - 1); count++){ //if the test score is greater than the previous score, then both values will swap places and get displayed in the second showArray.
      if (array [count].testScore > array[count + 1].testScore){
        swap (array[count], array [count + 1]);
        swapped = true;
      }
    }
  } while (swapped); //loop again if swapped occurred
}

void showArray (int size, const students array[]){
//Displays both sorted arrays (original and sorted) as a loop (up to index of 2 students).
  for (int count = 0; count < size; count++)
  cout << array[count].studentName << " " << array [count].testScore << " ";
  cout << endl;
}
Last edited on
For some reason, the code works but only without the outFile << sortArray (SIZE, values) in line 36 and 43. Therefore, it only reads properly from the output, but I need it to read from the input, creating a new file called "SortingDiscFiles.txt"
Last edited on
showArray prints values, nothing is returned from the function (its return type is void).

If you want to stream to the file instead of to standard out (cout), you can do something like:
1
2
3
4
5
void showArray (std::ostream& os, int size, const students array[]){
  for (int count = 0; count < size; count++)
    os << array[count].studentName << " " << array [count].testScore << " ";
  os << endl;
}


showArray(outFile, SIZE, values);

So, do you mean replacing the void showArray function I created with what you wrote?

I did just that by there are still errors as there are now no matching functions to 'showArray'

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

struct students{
string studentName; //allows student name/characters.
float testScore; //a number that allows decimals (required when finding average).
};

//functions used to sort student names according to the test scores.
void sortArray (int size, students array[]);
void showArray (int size, const students array []);

int main(){

  ofstream outFile;
  outFile.open("SortingDiscFiles.txt");

  float average, sumTestScore; //considers decimals
  const int SIZE = 3; //SIZE = # of students.

  students values[SIZE];
  
  values[0].studentName = "\n1 Sanna Saunders\t";
  values[0].testScore = 89;
  
  values[1].studentName = "\n2 Kelly Handley\t\t";
  values[1].testScore = 95;
  
  values[2].studentName = "\n3 Arif Crane\t\t";
  values[2].testScore = 76;
  
  //Displays values and names that aren't sorted.
  outFile << "\nThe records in the original file are:\n";
  outFile << showArray (outFile,SIZE, values); //original file.
  
  //Sorts and holds the values and names in ascending order.
  sortArray (SIZE, values);
  
  //Display the values and names again, but now in ascending order.
  outFile << "\nThe sorted records are:\n";
  outFile << showArray (outFile, SIZE, values); //calls the function that sorted values in line 39.
  
  //calculates and holds average.
  sumTestScore = (values[0].testScore) + (values[1].testScore) + (values[2].testScore);
  average = sumTestScore / SIZE;
  //displays average.
  outFile << "\nThe class average is: " ;
  outFile << average;
}

//Calculates the ascending order between the test scores and name using arrays.
void sortArray (int size, students array []){
  bool swapped;

  do{
    swapped = false;
    for (int count = 0; count < (size - 1); count++){ //if the test score is greater than the previous score, then both values will swap places and get displayed in the second showArray.
      if (array [count].testScore > array[count + 1].testScore){
        swap (array[count], array [count + 1]);
        swapped = true;
      }
    }
  } while (swapped); //loop again if swapped occurred
}

void showArray (std::ostream& os, int size, const students array[]){
  for (int count = 0; count < size; count++)
    os << array[count].studentName << " " << array [count].testScore << " ";
  os << endl;
}
Last edited on
If you define your function below where you actually call it, you need to make sure your function "prototype" (declaration) matches it, on line 13.
void showArray (std::ostream& os, int size, const students array []);

But you still did the same mistake, sorry to say.
The << operator requires two operands: whatever is on the left, and whatever is on the right.
(e.g., left << right).
Currently, you are attempting to do:
outFile << showArray (outFile,SIZE, values);
The left operand is outFile
The right operand is the return value of your showArray function.
The problem is, this doesn't make sense, because showArray doesn't return anything; its return value is void.

Just have it be:
showArray(outFile, SIZE, values);
Last edited on
Apologies for that.

I just realized that I didn't match it in the code I pasted above, which I fixed right after.

Anyway, thank you so much! It works well now. I just realized what you implied by "just have it be:
showArray(outFile, SIZE, values)


I knew it would've been an easy fix.

I'll make sure to keep that in mind next time.

Thanks again,
Allison
Topic archived. No new replies allowed.