1

I am trying to print out whatever is necessary from my program. What it does is it takes a long list from a text file and sort it based on first choice and GPA and put it into a vector. I manage to sort by First choice and GPA however how can I remove whatever output that isn't necessary?

This is an example of my Txt File (The sequence of each line is 1st choice, 2nd choice, 3rd choice, GPA, Name):

CC,DR,TP,3.8,AlexKong
SN,SM,TP,4,MarcusTan
DR,TP,SC,3.6,AstaGoodwin
SC,TP,DR,2.8,MalcumYeo
SN,SM,TP,3.7,DavidLim
SN,SM,TP,3.2,SebastianHo
SC,TP,DR,4,PranjitSingh
DR,TP,SC,3.7,JacobMa
and so on...

This is my output now (it is a long vector):

TP,DR,SC,4,SitiZakariah
TP,DR,SC,3.9,MuttuSami
TP,DR,SC,3.5,SabrinaEster
TP,DR,SC,3,KarimIlham
TP,DR,SC,3,AndryHritik
SN,SM,TP,4,MarcusTan
SN,SM,TP,3.8,MarcusOng
SN,SM,TP,3.7,DavidLim
SN,SM,TP,3.4,MollyLau
SN,SM,TP,3.2,SebastianHo
SN,SM,TP,3.2,NurAfiqah
SN,SM,TP,2.4,TanXiWei
SC,TP,DR,4,SallyYeo
SC,TP,DR,4,PranjitSingh
SC,TP,DR,3.6,RanjitSing
SC,TP,DR,2.8,MalcumYeo
SC,TP,DR,2.8,AbdulHalim
SC,TP,DR,2.7,AlifAziz
DR,TP,SC,3.9,SitiAliyah
DR,TP,SC,3.9,LindaChan
DR,TP,SC,3.8,SohLeeHoon
DR,TP,SC,3.7,PrithikaSari
DR,TP,SC,3.7,NurAzizah
DR,TP,SC,3.7,JacobMa
DR,TP,SC,3.6,AstaGoodwin
CC,DR,TP,3.9,MuruArun
CC,DR,TP,3.8,AlexKong
CC,DR,TP,3.7,DamianKoh
CC,DR,TP,3.3,MattWiliiams
CC,DR,TP,3.3,IrfanMuhaimin

And this is the output that I need (Basically students with CC as their 1st choice without displaying the 3 options):

3.9,MuruArun
3.8,AlexKong
3.7,DamianKoh
3.3,MattWiliiams
3.3,IrfanMuhaimin

This is my program.

#include <iostream>
#include <vector>
#include <fstream>
#include <string>
#include <algorithm>
using namespace std;

struct greater
{
    template<class T>
    bool operator()(T const &a, T const &b) const { return a > b; }
};

void main()
{
    vector<string> v;
    int p = 0;
    ifstream File;
    File.open("DSA.txt");
    if (!File.is_open()) return;

    string First;

    cout << "Round 1:\n";
    
        while (File >> First)
        {
            v.push_back(First);
            p++;
        }
        for (int i = 0; i < v.size(); i++)
        {
            sort(v.begin(), v.end(), greater());

            cout << v[i] << endl;
        }
}
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97

2 Answers2

0

as I propose in the comment, since the data is well defined as a structure, you can interpret semantically each row and filter according to that: here is what am talking about

int main()
{
    std::vector<std::string> v;
    std::string r = "CC,DR,TP,3.9,MuruArun";
    std::string delimiter = ",";
    std::string token = r.substr(0, r.find(delimiter));
    if(token == ??)// compare to what ever you want 
    {
        v.emplace_back(r);
    }

    cout << "token: " << token << endl; 
    cout << v.size() << endl; 
   
   return 0;
}
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
0

your last for loop:

 for (int i = 0; i < v.size(); i++)
 {
     sort(v.begin(), v.end(), greater());
     cout << v[i].substr(9) << endl;
 }

EDIT: If you want to only display ones with CC as 1st choice you can add if statement to your loop:

 for (int i = 0; i < v.size(); i++)
 {
     if (v[i].substr(0,2) != "CC") continue; 
     cout << v[i].substr(9) << endl;
 }

Also, I noticed another problem in your code. You should not sort the vector at every iteration. You should do it only once before the loop:

 sort(v.begin(), v.end(), greater()); 
 for (int i = 0; i < v.size(); i++)
 {
     if (v[i].substr(0,2) != "CC") continue; 
     cout << v[i].substr(9) << endl;
 }
roshi
  • 46
  • 4