Need to figure out how to sort in alphabetic order, already got it in order for grade in descending form

Write a program that allows a teacher to enter name-grade pairs, Ex: The user will type in the name of the student followed by the student’s grade. Dynamically allocates an array of Structs large enough to hold the information.

The user should then be given the option to:

1. sort the array of structs by student name in ascending order (A-Z).
2. sort the array of structs by student grade in descending order (100-0) while at the same time showing the name of the student for that grade.
3. calculate the class average.
4. store the sorted information (name in ascending with student grade) in a text file.

#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
#include <algorithm>

using namespace std;

struct Student
{
double grade;
char firstName[101], lastName[101];
};

void bubbleSortV2(Student array[], int size);
void sortArray(Student array[], int size);

int main()
{

Student *data;
int numStudents, firstChoice, secondChoice;
char repeat;


data = nullptr;

do
{
cout << "How many students?: ";
cin >> numStudents;

data = new Student[numStudents];

for (int i = 0; i < numStudents; i++)
{
cout << "\nEnter first name of student #" << i + 1 << " ";
cin >> data[i].firstName;

cout << "Enter second name of student #" << i + 1 << " ";
cin >> data[i].lastName;

cout << "Enter grade of student #" << i + 1 << " ";
cin >> data[i].grade;
}

//Open up menu
cout << "\n\t---Choose an option from below to begin---\n";
cout << "1. Sort Students by first name\n";
cout << "2. Sort Students by last name\n";
cout << "3. Sort Students bt grade\n";
cout << "4. Calculate grade average\n";
cout << "5. Write data into text file\n";
cout << "0. Exit program\n";
cin >> firstChoice;

//Validation
while (firstChoice < 0 || firstChoice > 5)
{
cout << "\nYou have entered an invalid number. Please try again\n";
cout << "\n\t---Choose an option from below to begin---\n";
cout << "1. Sort Students by first name\n";
cout << "2. Sort Students by last name\n";
cout << "3. Sort Students bt grade\n";
cout << "4. Calculate grade average\n";
cout << "5. Write data into text file\n";
cout << "0. Exit program\n";
cin >> firstChoice;
}

switch (firstChoice)
{
case 1:cout << "\nSorting students by first name...\n" ;
bubbleSortV2(data, numStudents);
break;
case 2:cout << "\nSorting students by last name...\n";

break;
case 3:cout << "\nSorting students by grade...\n";
sortArray(data, numStudents); break;
case 4:cout << "\nCalculating grade average...\n";

break;

//Open up a second menu so the user can choose how to write the data into the file
case 5:cout << "\n\t---How would you like to data to be written into the text file?---\n";
cout << "1. Unsorted\n";
cout << "2. Sorted by first name\n";
cout << "3. Sorted by last name\n";
cout << "4. Sorted by grade\n";
cout << "0. Exit menu\n";
cin >> secondChoice;

//Validation
while (secondChoice < 0 || secondChoice > 4)
{
cout << "\nYou have entered an invalid number. Please try again\n";
cout << "\n\t---How would you like to data to be written into the text file?---\n";
cout << "1. Unsorted\n";
cout << "2. Sorted by first name\n";
cout << "3. Sorted by last name\n";
cout << "4. Sorted by grade\n";
cout << "0. Exit menu\n";
cin >> firstChoice;
}

switch (secondChoice)
{

case 1: //Open up file to write into it


cout << "Data has been written successfully\n";
break;
case 2: //Open up file to write into it


cout << "Data has been written successfully\n";
break;
case 3: //Open up file to write into


cout << "Data has been written successfully\n";
break;
case 4: // Open up file to write into it


cout << "Data has been written successfully\n";
break;
case 0:

break;

}
break;
case 0:cout << "Now exiting...\n";

//Delete allocated memory before exiting
delete[] data;
}

//Delete allocated memory before exiting
delete[] data;
cout << "\n\nWould you like to try again? (Y/N)";
cin >> repeat;

} while (toupper(repeat) == 'Y');

return 0;
}


void sortArray(Student array[], int size)
{
Student temp;
bool madeAswap;

do
{
madeAswap = false;
for (int count = 0; count < (size - 1); count++)
{
if (array[count].grade < array[count + 1].grade)
{
temp = array[count];
array[count] = array[count + 1];
array[count + 1] = temp;
madeAswap = true;
}
}
} while (madeAswap);

//Display array
for (int count = 0; count < size; count++)
cout << array[count].grade << " " << array[count].firstName << " "
<< array[count].lastName << " " << endl;
}

void bubbleSortV2(Student array[], int size)
{
Student temp;
bool madeAswap;

do
{
madeAswap = false;
for (int count = 0; count < (size - 1); count++)
{
if (array[count].firstName > array[count + 1].firstName)
{
temp = array[count];
array[count] = array[count + 1];
array[count + 1] = temp;
madeAswap = true;
}
} while (madeAswap);

for (int count = 0; count < size; count++)
cout << array[count].firstName << " " << array[count].lastName << " "
<< array[count].grade << " " << endl;
}
Last edited on
Topic archived. No new replies allowed.