#include<fstream>

#include<iostream>

#include<string.h>

#include<string>

#include<iomanip>


using namespace std;


class student{

public:

string fname;

string lname;

string major;

double gpa;

}; // student

student e[100];

int n;

void load(){

ifstream studentFile("students.txt", ios::in);

while(studentFile>>e[n].fname>>e[n].lname>>e[n].major>>e[n].gpa){

n++;}//WHILE

} // LOAD the student

void display(){

cout<<setiosflags(ios::left)<<setw(15)<<"First Name"<<setw(15)<<"Last Name";

cout<<setw(15)<<"major"<<setw(15)<<"gpa";

cout<<endl<<endl;

cout<<setiosflags(ios::fixed|ios::showpoint|ios::left);

for(int i=0;i<n;i++){

if(e[i].fname!=" "){

cout<<setiosflags(ios::left)<<setw(15)<<e[i].fname<<setw(15)<<e[i].lname;

cout<<setprecision(2)<<setiosflags(ios::left)<<setw(15)<<e[i].major;

cout<<setw(15)<<e[i].gpa;

cout<<resetiosflags(ios::left)<<endl;}//IF

}//FOR

}//THIS FUNCTION DISPLAYS ALL RECORDS IN ARRAY

int swap(int i, int j){

student temp;

temp=e[i];

e[i]=e[j];

e[j]=temp;

return 0;

}//swap


main(){

load();

char choice='0';

while (choice!='e'&&choice!='E'){

cout<<"1. Display the original students' Records"<<endl;

cout<<"2. Sorting the Records by GPA"<<endl;

cout<<"Enter the number of your choice:";

cin>>choice;

cout<<endl;

switch (choice){

case '1': display(); break;

case '2':

int n=5 ,a,b;

for(a=0;a<n-1;a++){

b=a+1;

for(b=0;b<n;b++){

if(e[a].gpa>e[b].gpa)

{swap(a,b);}

}

}//END a

display();

break;

} // switch

} // while

}//MAIN

กก

1