1

So I have to allow the user to create a certain amount of structs with 5 different types of information, and then sort based on one of those types. For example, they would input all the data and then sort by grade or name. How would I go about creating an array of just the different names or grades across the different structs?

#include <iostream>
#include <string>

struct student
{
    std::string studentName;
    std::string studentIDNumber;
    int currentExamGrade;
    int priorExamGrade;
    double GPA;
};

void createStudent()
{
    int numStudents;
    std::cout << "Enter number of students\n";
    std::cin >> numStudents;

    while (numStudents > 0)
    {
        student name;

        std::cout << "Enter the student's name\n";
        std::cin >> name.studentName;

        std::cout << "Enter the student's ID number\n";
        std::cin >> name.studentIDNumber;

        std::cout << "Enter the student's current exam grade\n";
        std::cin >> name.currentExamGrade;

        std::cout << "Enter the student's prior exam grade\n";
        std::cin >> name.priorExamGrade;

        std::cout << "Enter the student's GPA\n";
        std::cin >> name.GPA;

        numStudents -= 1;
    }
}

int main()
{
    createStudent();

    int choice;
    std::cout << "How do you want to sort the list?\n (Enter 1 for name, 2     for ID number, 3 for current exam grade, 4 for prior exam grade, 5 for GPA\n";
    std::cin >> choice; 

    return 0;

}     
4
  • So, Captn, where's your array to be sorted? You read student alright, but after reading one, you discard the data and read another. Commented Sep 26, 2016 at 4:45
  • That's part of my question Commented Sep 26, 2016 at 4:48
  • 1
    Ok. Time for you to read about std::vector - look over examples too. Commented Sep 26, 2016 at 4:50
  • One step at a time. First get the input working, then worry about sorting. Garbage in, garbage out. Garbage in, you sort garbage. Why debug sorting garbage? Commented Sep 26, 2016 at 5:05

1 Answer 1

0

Ok. I assume you don't know the in-built sort functions in C++. Take a look at std::sort()

Take Input :

input values into struct array + printing out

Now to make an array of structs :

How do you make an array of structs in C?

Creating an array of structs in C++

Now to sort these arrays :

Sorting a vector of custom objects

How to approach this problem :

std::sort() allows you to give custom functions based on which non-tivial objects are comparable. Example of one such function is :

bool gpa_comparision_function (student s1,student s2)
{
    return s1.GPA < s2.GPA;
}

It takes argument as 2 students that need to be compared by the sorting function and returns the ordering of students s1 and s2 based on GPA.

This is why the statement s1.GPA < s2.GPA is important.

So if the student s1 has GPA = 5.0 and s2 has GPA = 4.0 we want the order to be s2,s1 (ascending) so this function returns false, stating that s2 should come before s1.

So in the call of the sorting function :

sort (arr_of_students,arr_of_students+N,gpa_comparision_function);

notice the 3rd argument, gpa_comparision_function, this is telling the in-built function to use our comparison function.

Talk is cheap show me the code :

#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
struct student
{
    std::string studentName;
    std::string studentIDNumber;
    int currentExamGrade;
    int priorExamGrade;
    double GPA;
};
bool gpa_comparision_function (student s1,student s2)
{
    return s1.GPA < s2.GPA;
}
bool currentExamGrade_comparision_function (student s1,student s2)
{
    return s1.currentExamGrade < s2.currentExamGrade;
}
int main ()
{
    // Create an array of structs
    student arr_of_students[100]; // A better way will be vector<student>

    // Take input
    // ...

    // Now lets sort, assuming number of students is N
    // Based on GPA
    sort (arr_of_students,arr_of_students+N,gpa_comparision_function);

    // Based on currentExamGrade
    sort (arr_of_students,arr_of_students+N,currentExamGrade_comparision_function);

    return 0;
}
Sign up to request clarification or add additional context in comments.

2 Comments

If you stepped on the providing the code path, at least go all the way - show him how to read into those structures and explain to him what is happening. Otherwise this is an exercise of vanity at best or reputation wh..ing at worst - it has nothing to do with helping him learn.
@AdrianColomitchi I have added the explanation for the use of custom comparision function. I am not going to explain how the input is to be taken. I will find a link a provide it with the answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.