Hakim Muhaimin
Hakim Muhaimin

Reputation: 87

How to swap and remove lines at output?

I am trying to out put the GPA based on their value. Highest GPA on top and Lowest GPA below. And also output 2 out of 5 of my output. Below is my output. As you can see, the person with the most GPA (3.9) is all the way below. is it possible to output him at the top followed by the second highest, third, fourth and so on. I got these data off a txt file.

Also if possible, I also need to print out only 3 out of he five with highest to lowest GPA. So 3 outputs with highest on top to lowest below out of the 5.

Apologies for the terrible code. I am very weak in this but am trying my very best.

This is my txt file. the first column is first choice, second choice, third choice, name, gpa.


CC,DR,TP,Alex Kong ,3.8
SN,SM,TP,Marcus Tan,4
DR,TP,SC,Asta Goodwin,3.6
SC,TP,DR,Malcum Yeo,2.8
SN,SM,TP,David Lim,3.7

and so on...


This is my output.

CC Students:
Alex Kong  3.8 Choice 1
Damian Koh 3.7 Choice 1
Matt Wiliiams 3.3 Choice 1
Irfan Muhaimin 3.3 Choice 1
Muru Arun 3.9 Choice 1

This is the code I have so far.

void sortCC(){

    ifstream File;
    File.open("DSA.txt");

    while (File.good()) {

        string Name;
        string GPA;
        string First;
        string Second;
        string Third;

        getline(File, First, ',');
        getline(File, Second, ',');
        getline(File, Third, ',');
        getline(File, Name, ',');
        getline(File, GPA, '\n');


        vector <string> v;

        v.push_back(First);
        v.push_back(Second);
        v.push_back(Third);
        v.push_back(Name);
        v.push_back(GPA);

        if (First == "CC"){
                cout << Name << " " << GPA << " " << "Choice 1" << '\n';
        }
    }
}

This is the kind of output I need.

Muru Arun  3.9 Choice 1
Alex Kong 3.8 Choice 1

Upvotes: 0

Views: 93

Answers (1)

Joseph Larson
Joseph Larson

Reputation: 9078

Hakim, this isn't going to be fully trivial, but you'll learn a lot doing it. I'm going to outline what you need to do. You need to work on this as individual pieces.

First, you need to create a class that holds the individual records of data. The class would look something like this:

class Student {
public:
    std::string first;
    std::string second;
    std::string third;
    std::string name;
    double gpa;
};

That's part one. Part two is to get data into it. Now, that's going to be from your input file, but for testing purposes, let's do it manually.

std::vector<Student> students;
Student firstStudent{"a", "b", "c", "First", 3.5};
students.push_back(firstStudent);

... Repeat until you have several of them.

Then you want a method that prints them, something like this:

std::ostream & operator<<(std::ostream &output, std::vector<Student> vec) {
    for (const Student &student: vec) {
        output << "Student: " << student.name << " GPA: " << student.gpa << endl;
    }
    return output;
}

You can call that method like this (for instance);

std::cout << students;

Try all that. It might take you a bit to get it right. Once you can create an array with sample data and print it out, you have only two more parts.

First, instead of hard-coding data, you'll need to read it from your input. You already kind of know how to do that. I think between what you know and the code above, you can figure this out.

Last, you have to sort it. Sorting is a little tricky. So I'm going to give you the line of code that will do it then explain how it works.

#include <algorithm> // Do this at the top of your .cpp
std::sort(students.begin(), students.end(),
    [](const Student &first, const Student &second) {
    return first.gpa > second.gpa;
});

There's a lot going on here. I'll explain it in parts.

std::sort() is part of the standard C++ library. It can sort based on natural order (which is great for numbers or strings), or you can pass in what's called a Comparator. We're doing it the second way.

You have to pass it the range of what you're sorting. (Unless you're on C++20, but you probably aren't.) That's what the students.begin() and students.end() part is -- we're telling it to sort the entire vector.

Finally, the comparator I've given you is called a lambda. You could also have written a comparison method and referenced it, but I do it this way. This is basically saying, "Here's the method that returns true if they're in the right order".

The syntax is weird, I know.

At this point, you just have to put it all together.

With this much help, it'll take you about another hour.

Upvotes: 2

Related Questions