Hello
The book I am using is not of help, so I would love if someone could give me a few hints.
I have already declared three 2D arrays of lname, fname, and grades and have written a function to read the input file "scores.txt" into the arrays and returned the length.
The problem I am having is working on a void function to sort records of students by last name. I have to use sort selection and sort the names in ascending order by last name. The last name, first name, and grades are all 2D arrays, which I have declared as lname, fname, and grades. The list below is an example of what I need to sort. It includes the last name, first name, and 5 grades.
Code:
Smith Tom 25 25 20 15 24
Broom Linda 22 23 25 25 20
Tanner Joe 22 15 24 18 20
This is what I have so far
Code:
#include <fstream>
#include <iostream>
#include <cstdlib>
#include <cstring>
using namespace std;
const char LNAME_SIZE = 20;
const char FNAME_SIZE = 15;
const char GRADES_SIZE = 20;
const char STUDENTS = 32;
int readScores(ifstream& fin, char lname[][LNAME_SIZE + 1],
char fname[][FNAME_SIZE + 1], char grades[][GRADES_SIZE + 1]);
// reads scores.txt into the arrays and returns the length
void sortRecords(char lname[][LNAME_SIZE + 1], int length);
// sorts the records by last name in ascending order
void main(void)
{
ifstream fin;
ofstream fout;
char lname[STUDENTS][LNAME_SIZE + 1];
char fname[STUDENTS][FNAME_SIZE + 1];
char grades[STUDENTS][GRADES_SIZE + 1];
int length;
// connect the input stream to the file scores.txt
fin.open("scores.txt");
// tests the input stream
if ( fin.fail() )
{
cerr << "Error opening file scores.txt for reading. Aborting!"
<< endl
<< endl;
exit(1);
}
// read the file into the arrays
length = readScores(fin, lname, fname, grades);
// sorts the records in alphabetical order by last name
sortRecords(lname, length);
// connect the output stream to the file records.txt
fout.open("records.txt");
// tests the output stream
if ( fout.fail() )
{
cerr << "Error opening file records.txt for reading. Aborting!"
<< endl
<< endl;
exit(1);
}
fin.close();
fout.close();
}// end main()
int readScores(ifstream& fin, char lname[][LNAME_SIZE + 1],
char fname[][FNAME_SIZE + 1], char grades[][GRADES_SIZE + 1])
{
int length = 0;
fin.getline(lname[length], LNAME_SIZE + 1, ' ');
while ( !fin.eof() )
{
fin.getline(fname[length], FNAME_SIZE + 1, ' ');
fin.getline(grades[length], GRADES_SIZE + 1, ' ');
length++;
fin.getline(lname[length], LNAME_SIZE + 1, ' ');
}// end while
cout << length << endl << endl;
for (int i = 0; i < length; i++)
{
cout << lname[i] << " ";
cout << fname[i] << " ";
cout << grades[i] << " ";
}
cout << endl << endl;
return(length);
}// end readScores()
void sortRecords(char lname[][LNAME_SIZE + 1], int length)
{
int minPos;
// finds minimum position
for (int i = 0; i < length; i++) {
if (lname[i] < lname[minPos]) {
minPos = i;
}
}
}// end sortRecords()
I have no clue what to do next for the sortRecords. Like I said, my book is of no help. If someone could please help, it would be greatly appreciated.