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:
#include <iostream>
#include <fstream>
#include <string>
usingnamespace 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
constint 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;
}
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"
#include <iostream>
#include <fstream>
#include <string>
usingnamespace 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
constint 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;
}
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);