0

Hey guys, how would i convert the code in option ascending and descendingGPA to where it sorts all students in ascending order by lastname and all students in descending order by gpa. thanks!

#include <iomanip>
#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;

struct StudentRecord
{
char lastName [16];
char firstName [16];
char hometown [16];
char major[5];
int studentNumber;
double balance;
int earnedHours;
double gpa;
};
void printList (StudentRecord s[], int n);
void ascending (StudentRecord s[], int n);
void descendingGPA (StudentRecord s[], int n);
void balanceThreshold (StudentRecord s[], int n);
void majorAndGpaThreshold (StudentRecord s[], int n);

void displayMenu();
int main ()
{
char textFilename[50];
char binaryFilename[50];
StudentRecord nextStudent;
StudentRecord student[100];
StudentRecord *ptr;
int n;
fstream studentFile;
ifstream textFile;
char lastIn [16];
char firstIn[16];
char townIn [16];
char majorIn[5];
int numberIn;
double balanceIn;
int hoursIn;
double gpaIn;
bool menuActive = true;
char menuChoice;

cout << "Enter filename of source text file: ";
cin >> textFilename;
cout << endl;
textFile.open (textFilename);

cout << "Enter filename of binary file of student records: ";
cin >> binaryFilename;
cout << endl;
studentFile.open (binaryFilename, ios:: out | ios:: binary);
n = 0;

while (!textFile.eof())
{
textFile >> lastIn >> firstIn >> townIn >> majorIn >> numberIn >> balanceIn >> hoursIn >> gpaIn;
if (!textFile.eof())
{
strcpy (nextStudent.lastName, lastIn);
strcpy (nextStudent.firstName, firstIn);
strcpy (nextStudent.hometown, townIn);
strcpy (nextStudent.major, majorIn);
nextStudent.studentNumber = numberIn;
nextStudent.balance = balanceIn;
nextStudent.earnedHours = hoursIn;
nextStudent.gpa = gpaIn;
studentFile.write (reinterpret_cast<char *>(&nextStudent), sizeof(nextStudent));
n++;
}
}
cout << "Records created in binary student file = " << n << endl;
cout << endl;

textFile.close();
studentFile.close();
studentFile.clear();

studentFile.open (binaryFilename, ios:: in | ios:: binary);
ptr = student;
n = 0;

while (!studentFile.eof())
{
studentFile.read (reinterpret_cast<char *>(ptr), sizeof(nextStudent));
if (!studentFile.eof())
{
ptr++;
n++;
}
}
studentFile.close();

cout << "Records read from binary student file = " << n << endl;
cout << endl << endl;
return 0;
}

void ascending (StudentRecord s[], int n)
{
     StudentRecord student[100];
     int last;
     int j;
     bool haveSwap;
     int pass; 
     double temp;
     last = 1;
     double x[100];
     for (pass = 1; pass <= n-1; pass++)
     {
          haveSwap = false;
          for (j = n-1; j >= last; j--)
          {   
              if (x[j] < x[j-1])
              {
                 haveSwap = true; 
                 temp = x[j];
                 x[j] = x[j-1];
              }
          }
     }
}

void descendingGPA (StudentRecord s[], int n)
{
     double x[100];
     int last;
     int j;
     bool haveSwap;
     int pass; 
     double temp;
     last = 1;
     for (pass = 1; pass <= n-1; pass++)
     {
         haveSwap = false;
         for (j = n-1; j >= last; j--)
         {
             if (x[j] > x[j-1])
             {
                haveSwap = true;
                temp = x[j];
                x[j] = x[j-1];
                x[j-1] = temp;
             }
         }
     if (haveSwap == false)
                  pass = n;
     last++;
     }
}

Edited by react05: n/a

2
Contributors
1
Reply
2
Views
7 Years
Discussion Span
Last Post by Ancient Dragon
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.