0

I'm trying to sort alphabetically by team for my program, but not having any luck. If there is any hints or advice out there I would appreciate it. Below is the program I have minus the data input. Basically I just want to know how I would go about specifically sorting only by team in alphabetical order.

nflrecievers data[100];
ifstream fin;
fin.open("data.txt");
ofstream fout;
fout.open("validationReport.txt");
int i = 0;

while(!fin.eof())
{
    fin >> data[i].fname >> data[i].lname >> data[i].team >> data[i].rec >> data[i].yards >> data[i].avgyrds_percatch >> data[i].tds >> data[i].longest_rec >> data[i].recpasttwenty_yrds >> data[i].yrds_pergame >> data[i].fumbles >> data[i].yac >> data[i].first_dwns ;
    i = i + 1;

}

int a;
int b;
cout << " Select NFL Receivers Statistics. Input 1-4 " << endl;
cout << " 1) Receivers with 25+ Rec and 300+ Yards. " << endl;
cout << " 2) Recievers with 3+ TDs and 3+ Rec over 20 Yards. " << endl;
cout << " 3) Recievers with 100+ Yards per game and 15+ First Downs. " << endl;
cout << " 4) Veiw Total Recievers Statistics. " << endl;
cin >> a;


int c = 0;
if (a==1)
{

    cout << " Receivers with 25+ Rec and 300+ Yards. " << endl;
    while( c < i-1)
    {
        if(data[c].rec > 25 && data[c].yards > 300)
        {
            cout << endl;
            cout << data[c].fname << " " << data[c].lname << " " << data[c].team << endl;
            cout << " Rec: " << data[c].rec << " Yards: " << data[c].yards << endl;
            cout << endl;
        }
        c++;
    }
}

else if(a==2)
{

    cout << " Recievers with 3+ TDs and 3+ Receptions past 20 Yards. " << endl;
    while( c < i-1)
    {
        if(data[c].tds > 3 && data[c].recpasttwenty_yrds > 3)
        {
            cout << endl;
            cout << data[c].fname << " " << data[c].lname << " " << data[c].team << endl;
            cout << " TDs: " << data[c].tds << " Receptions past 20 Yards: " << data[c].recpasttwenty_yrds << endl;
            cout << endl;

        }
        c++;
    }

}
else if(a==3)
{
    cout << " Recievers who average over 100+ yards per game and 15+ First Downs. " << endl;
    while( c < i-1)
    {
        if(data[c].yrds_pergame > 100 && data[c].first_dwns > 15)
        {
            cout << endl;
            cout << data[c].fname << " " << data[c].lname << " " << data[c].team << endl;
            cout << " Average Yards per game: " << data[c].yrds_pergame << " First Downs: " << data[c].first_dwns << endl;
            cout << endl;

        }
        c++;
    }
}
else if(a==4)
{
    cout << " Select a Reciever: "  << endl;
    while( c < i-1)
    {
        cout << c << ") " << data[c].fname << " " << data[c].lname << endl;
        c++;
    }
    cout << " Total NFL Receivers Statistics. " << endl;
    cin >> b;
    cout << data[b].fname << " " << data[b].lname << endl;
    cout << " Team: " << data[b].team << endl;
    cout << " Receptions: " << data[b].rec << endl;
    cout << " Yards: " << data[b].yards << endl;
    cout << " Average Yards Per Catch: " << data[b].avgyrds_percatch << endl;
    cout << " Longest Reception: " << data[b].longest_rec << endl;
    cout << " Receptions over 20 Yards: " << data[b].recpasttwenty_yrds << endl;
    cout << " Yards per game " << data[b].yrds_pergame << endl;
    cout << " Fumbles: " << data[b].fumbles << endl;
    cout << " Average Yards After Catch " << data[b].yac << endl;
    cout << " Total First Downs: " << data[b].first_dwns << endl;
}

return 0;

}
Kara
  • 6,115
  • 16
  • 50
  • 57

3 Answers3

2
std::sort(std::begin(data), std::end(data), 
          [](const nflrecievers& a, const nflrecievers& b) { return a.team < b.team; });
mattnewport
  • 13,728
  • 2
  • 35
  • 39
0

I would use std::sort

bool compare_teams(const nflrecievers &a, const nflrecievers &b) {
  return a.team < b.team;
}

int i = 0;
while(!fin.eof())
{
    fin >> data[i].fname >> data[i].lname >> data[i].team >> data[i].rec >> data[i].yards >> data[i].avgyrds_percatch >> data[i].tds >> data[i].longest_rec >> data[i].recpasttwenty_yrds >> data[i].yrds_pergame >> data[i].fumbles >> data[i].yac >> data[i].first_dwns ;
    i = i + 1;
}

std::sort(data, data+i, compare_teams);
CapelliC
  • 59,646
  • 5
  • 47
  • 90
0
for (int i=0;i<c-1;i++)
   for (int j=0;j<c-1;j++)
      if (data[j].team>data[j+1].team)
      {
           nflrecievers temp = data[j];
           data[j] = data[j+1];
           data[j+1] = temp;
      }

another solution:

int compare(const void *v1, const void *v2)
{
    nflrecievers p1 = *(nflrecievers *)v1, p2 = *(nflrecievers *)v2;
    return strcmp(p1.team,p2.team);
}

qsort(data,c,sizeof(data),compare);
hasan
  • 23,815
  • 10
  • 63
  • 101
  • It still can't get it to work, I guess some more information would help, sorry. What I'm thinking now is that I would like to add a 5th option to the "Main Menu" Where all the receivers can be viewed alphabetically by team. I understand the concept of your ansewers just not sure where to put it in my stucture. this is what i have so far: – user2929498 Oct 29 '13 at 21:26
  • else if (a==5) { cout << " Receivers listed Alpabetically by team: " << endl; for (int i=0; i < c - 1; i++) for (int j=0; j < c - 1; j++) if (data[j].team > data[j+1].team) { nflrecievers temp = data[j]; data[j] = data[j+1]; data[j+1] = temp; } while ( c < i ) { cout << ") " << data[c].team << " " << data[c].fname << " " << data[c].lname << endl; c++; } – user2929498 Oct 29 '13 at 21:26
  • if you don't resort the players when another option is entered you can add the sorting after input without typing. and when user enter 5 you only print the players. otherwise, if you manipulate with the array you have to sort every time user enters 5. – hasan Oct 30 '13 at 06:51