how can i sort this file alphabetically by first name when in the program???

#include<iostream>
#include<cstdlib>
#include<iomanip>
#include<string>
#include<fstream>
#include<vector>
usingnamespace std;
struct players
{
    string fname;
    string lname;
    int atbats;
    int runs;
    int hits;
    int doubles;
    int triples;
    int hrs;
    int rbi;
    int sos;
    double avg;
};
int main()
{

    void bubblesort2(players team[]);
    string ifile="top100off.txt", ofile="top60def.txt", name;
    players team[45];
    int i, c;

    ifstream inFile; 
    inFile.open(ifile.c_str());
    if (inFile.fail()) 
    {
        cout << "The file was not opened." << endl;
        exit(1);
    }
    inFile >> team[0].fname >> team[0].lname >> team[0].atbats >> team[0].runs
    >> team[0].hits >> team[0].doubles >> team[0].triples >> team[0].hrs 
    >> team[0].rbi >> team[0].sos >> team[0].avg;
    for(i=1; i<45; i++)
    {
        inFile >> team[i].fname >> team[i].lname >> team[i].atbats >> team[i].runs
        >> team[i].hits >> team[i].doubles >> team[i].triples >> team[i].hrs 
        >> team[i].rbi >> team[i].sos >> team[i].avg;
        cout << team[i].fname << " " << team[i].lname << " " << team[i].atbats << " " << team[i].runs
        << " " << team[i].hits << " " << team[i].doubles << " " << team[i].triples << " " << team[i].hrs 
        << " " << team[i].rbi << " " << team[i].sos << " " << team[i].avg << "\n"; 
    }
    ofstream outFile; 
    outFile.open(ofile.c_str());
    if (outFile.fail())
    {
        cout << "Failed!!!." << endl;
    }
    bubblesort2(team);


    for (c=1; c<=5; c++)
    {
        cout << "Type a last from the offensive player list. " << endl;
        getline(cin, name);
        if (islower(name[0]))
        {
            name[0]=toupper(name[0]);
        }
        for (i=0; i<45; i++)
        {
            if (team[i].lname == name)
            {
                outFile << team[i].fname << " " << team[i].lname << " " << team[i].atbats << " " << team[i].runs
                << " " << team[i].hits << " " << team[i].doubles << " " << team[i].triples << " " << team[i].hrs 
                << " " << team[i].rbi << " " << team[i].sos << " " << team[i].avg << endl;
            }

        }
    }
    for(i=0; i<45; i++)
    {
        outFile << team[i].fname << " " << team[i].lname << " " << team[i].atbats << " " << team[i].runs
        << " " << team[i].hits << " " << team[i].doubles << " " << team[i].triples << " " << team[i].hrs 
        << " " << team[i].rbi << " " << team[i].sos << " " << team[i].avg << endl;
    }
    outFile.close();


    cout << "Closed.";
    cin.ignore();
    return 42;
}
void bubbleSort2(players team[]) 
{
    bool doMore;
    do {
        doMore = false; 
        for (int i=0; i<45; i++) 
        {
            if (team[i].lname > team[i+1].lname) 
            {

                players temp = team[i];
                team[i] = team[i+1]; 
                team[i+1] = temp;
                doMore = true; 
            }
        }
    } while (doMore);
}

Edited 3 Years Ago by Dani: Formatting fixed

Attachments
Juan Pierre 699 87 204 32 13 3 40 38 0.292
Ichiro Suzuki 695 110 224 20 9 9 49 71 0.322
Michael Young 691 93 217 52 3 14 103 96 0.314
Jimmy Rollins 689 127 191 45 9 25 83 80 0.277
Chase Utley 658 131 203 40 4 32 102 132 0.309
Grady Sizemore 655 134 190 53 11 28 76 153 0.29
Rafael Furcal 654 113 196 32 9 15 63 98 0.3
Jeff Francoeur 651 83 169 24 6 29 103 132 0.26
Miguel Tejada 648 99 214 37 0 24 100 79 0.33
Jose Reyes 647 122 194 30 17 19 81 81 0.3
Alfonso Soriano 647 119 179 41 2 46 95 160 0.277
Mark Loretta 635 75 181 33 0 5 59 63 0.285
Hanley Ramirez 633 119 185 46 11 17 59 128 0.292
Mark Teixeira 628 99 177 45 1 33 110 128 0.282
Raul Ibanez 626 103 181 33 5 33 123 115 0.289
Melvin Mora 624 96 171 25 0 16 83 99 0.274
Derek Jeter 623 118 214 39 3 14 97 102 0.343
Adrian Beltre 620 88 166 39 4 25 89 118 0.268
Gary Matthews 620 102 194 44 6 19 79 99 0.313
Ryan Zimmerman 614 84 176 47 3 20 110 120 0.287
Dan Uggla 611 105 172 26 7 27 90 123 0.282
Vernon Wells 611 91 185 40 5 32 106 90 0.303
Vladimir Guerrero 607 92 200 34 1 33 116 68 0.329
Orlando Cabrera 607 95 171 45 1 9 72 58 0.282
Chone Figgins 604 93 161 23 8 9 62 100 0.267
Brian Giles 604 87 159 37 1 14 83 60 0.263
Pedro Feliz 603 75 147 35 5 22 98 112 0.244
Jose Lopez 603 78 170 28 8 10 79 80 0.282
Matt Holliday 602 119 196 45 5 34 114 110 0.326
Garrett Atkins 602 117 198 48 1 29 120 76 0.329
Carl Crawford 600 89 183 20 16 18 77 85 0.305
Edgar Renteria 598 100 175 40 2 14 70 89 0.293
Chad Tracy 597 91 168 41 0 20 80 129 0.281
Curtis Granderson 596 90 155 31 9 19 68 174 0.26
Aramis Ramirez 594 93 173 38 4 38 119 63 0.291
Magglio Ordonez 593 82 177 32 1 24 104 87 0.298
Johnny Damon 593 115 169 35 5 24 80 85 0.285
Justin Morneau 592 97 190 37 1 34 130 93 0.321
Richie Sexson 591 75 156 40 0 34 107 154 0.264
Hank Blalock 591 76 157 26 3 16 89 98 0.266
Luis Gonzalez 586 93 159 52 2 15 73 58 0.271
Luis Castillo 584 84 173 22 6 3 49 58 0.296
David Wright 582 96 181 40 5 26 116 113 0.311
Freddy Sanchez 582 85 200 53 2 6 85 52 0.344
Ryan Howard 581 104 182 25 1 58 149 181 0.313
Lyle Overbay 581 82 181 46 1 22 92 96 0.312
Orlando Hudson 579 87 166 34 9 15 67 78 0.287
Omar Vizquel 579 88 171 22 10 4 58 51 0.295
Miguel Cabrera 576 112 195 50 2 26 114 108 0.339
Mike Lowell 573 79 163 47 1 20 80 61 0.284
Randy Winn 573 82 150 34 5 11 56 63 0.262
Victor Martinez 572 82 181 37 0 16 93 78 0.316
Alex Rodriguez 572 113 166 26 1 35 121 139 0.29
Jason Bay 570 101 163 29 3 35 109 156 0.286
Adrian Gonzalez 570 83 173 38 1 24 82 113 0.304
Kevin Youkilis 569 100 159 42 2 13 72 120 0.279
Jhonny Peralta 569 84 146 28 3 13 68 152 0.257
Prince Fielder 569 82 154 35 1 28 81 125 0.271
Paul Konerko 566 97 177 30 0 35 113 104 0.313
Andruw Jones 565 107 148 29 0 41 129 127 0.262
Brian Roberts 563 85 161 34 3 10 55 66 0.286
Eric Byrnes 562 82 150 37 3 26 79 88 0.267
Adam Dunn 561 99 131 24 0 40 92 194 0.234
David Ortiz 558 115 160 29 2 54 137 117 0.287
Yuniesky Betancourt 558 68 161 28 6 8 47 54 0.289
Michael Cuddyer 557 102 158 41 5 24 109 130 0.284
Torii Hunter 557 86 155 21 2 31 98 108 0.278
Jay Payton 557 78 165 32 3 10 59 52 0.296
Juan Encarnacion 557 74 155 25 5 19 79 86 0.278
Nick Swisher 556 106 141 24 2 35 95 152 0.254
Tadahito Iguchi 555 97 156 24 0 18 67 110 0.281
Jason Kendall 552 76 163 23 0 1 50 54 0.295
Mike Cameron 552 88 148 34 9 22 83 142 0.268
Marcus Giles 550 87 144 32 2 11 60 105 0.262
Craig Biggio 548 79 135 33 0 21 62 84 0.246
Mark Grudzielanek 548 85 163 32 4 7 52 69 0.297
Ivan Rodriguez 547 74 164 28 4 13 69 86 0.3
Todd Helton 546 94 165 40 5 15 81 64 0.302
Aaron Hill 546 70 159 28 3 6 50 66 0.291
Joe Crede 544 76 154 31 0 30 94 58 0.283
Carlos Guillen 543 100 174 41 5 19 85 87 0.32
Garret Anderson 543 63 152 28 2 17 85 95 0.28
Jack Wilson 543 70 148 27 1 8 35 65 0.273
Brandon Inge 542 83 137 29 2 27 83 128 0.253
Craig Monroe 541 89 138 35 2 28 92 126 0.255
Troy Glaus 540 105 136 27 0 38 104 134 0.252
Jermaine Dye 539 103 170 27 3 44 120 118 0.315
Josh Barfield 539 72 151 32 3 13 58 81 0.28
Bill Hall 537 101 145 39 4 35 85 162 0.27
Brandon Phillips 536 65 148 28 1 17 75 88 0.276
Lance Berkman 536 95 169 29 0 45 136 106 0.315
Albert Pujols 535 119 177 33 1 49 137 50 0.331
Ronny Cedeno 534 51 131 18 7 6 41 109 0.245
Jacque Jones 533 73 152 31 1 27 81 116 0.285
Willy Taveras 529 83 147 19 5 1 30 88 0.278
Emil Brown 527 77 151 41 2 15 81 95 0.287
Scott Podsednik 524 86 137 27 6 3 45 96 0.261
Carlos Delgado 524 89 139 30 2 38 114 120 0.265
Joe Mauer 521 86 181 36 4 13 84 54 0.347
Scott Rolen 521 94 154 48 1 22 95 69 0.296

Hello,

usingnamespace std;

Change this to using namespace std; . Your code will compile fine now. If there's another problem with it, please be more specific and tell us what the problem is. Also attach the files you are trying to open or describe them.

Regards Niek

top100off.txt

there is one i cant upload the other but i fixed namespace and it wont sort them. How can i sort player alphabetically by first name??

Attachments
Juan Pierre 699 87 204 32 13 3 40 38 0.292
Ichiro Suzuki 695 110 224 20 9 9 49 71 0.322
Michael Young 691 93 217 52 3 14 103 96 0.314
Jimmy Rollins 689 127 191 45 9 25 83 80 0.277
Chase Utley 658 131 203 40 4 32 102 132 0.309
Grady Sizemore 655 134 190 53 11 28 76 153 0.29
Rafael Furcal 654 113 196 32 9 15 63 98 0.3
Jeff Francoeur 651 83 169 24 6 29 103 132 0.26
Miguel Tejada 648 99 214 37 0 24 100 79 0.33
Jose Reyes 647 122 194 30 17 19 81 81 0.3
Alfonso Soriano 647 119 179 41 2 46 95 160 0.277
Mark Loretta 635 75 181 33 0 5 59 63 0.285
Hanley Ramirez 633 119 185 46 11 17 59 128 0.292
Mark Teixeira 628 99 177 45 1 33 110 128 0.282
Raul Ibanez 626 103 181 33 5 33 123 115 0.289
Melvin Mora 624 96 171 25 0 16 83 99 0.274
Derek Jeter 623 118 214 39 3 14 97 102 0.343
Adrian Beltre 620 88 166 39 4 25 89 118 0.268
Gary Matthews 620 102 194 44 6 19 79 99 0.313
Ryan Zimmerman 614 84 176 47 3 20 110 120 0.287
Dan Uggla 611 105 172 26 7 27 90 123 0.282
Vernon Wells 611 91 185 40 5 32 106 90 0.303
Vladimir Guerrero 607 92 200 34 1 33 116 68 0.329
Orlando Cabrera 607 95 171 45 1 9 72 58 0.282
Chone Figgins 604 93 161 23 8 9 62 100 0.267
Brian Giles 604 87 159 37 1 14 83 60 0.263
Pedro Feliz 603 75 147 35 5 22 98 112 0.244
Jose Lopez 603 78 170 28 8 10 79 80 0.282
Matt Holliday 602 119 196 45 5 34 114 110 0.326
Garrett Atkins 602 117 198 48 1 29 120 76 0.329
Carl Crawford 600 89 183 20 16 18 77 85 0.305
Edgar Renteria 598 100 175 40 2 14 70 89 0.293
Chad Tracy 597 91 168 41 0 20 80 129 0.281
Curtis Granderson 596 90 155 31 9 19 68 174 0.26
Aramis Ramirez 594 93 173 38 4 38 119 63 0.291
Magglio Ordonez 593 82 177 32 1 24 104 87 0.298
Johnny Damon 593 115 169 35 5 24 80 85 0.285
Justin Morneau 592 97 190 37 1 34 130 93 0.321
Richie Sexson 591 75 156 40 0 34 107 154 0.264
Hank Blalock 591 76 157 26 3 16 89 98 0.266
Luis Gonzalez 586 93 159 52 2 15 73 58 0.271
Luis Castillo 584 84 173 22 6 3 49 58 0.296
David Wright 582 96 181 40 5 26 116 113 0.311
Freddy Sanchez 582 85 200 53 2 6 85 52 0.344
Ryan Howard 581 104 182 25 1 58 149 181 0.313
Lyle Overbay 581 82 181 46 1 22 92 96 0.312
Orlando Hudson 579 87 166 34 9 15 67 78 0.287
Omar Vizquel 579 88 171 22 10 4 58 51 0.295
Miguel Cabrera 576 112 195 50 2 26 114 108 0.339
Mike Lowell 573 79 163 47 1 20 80 61 0.284
Randy Winn 573 82 150 34 5 11 56 63 0.262
Victor Martinez 572 82 181 37 0 16 93 78 0.316
Alex Rodriguez 572 113 166 26 1 35 121 139 0.29
Jason Bay 570 101 163 29 3 35 109 156 0.286
Adrian Gonzalez 570 83 173 38 1 24 82 113 0.304
Kevin Youkilis 569 100 159 42 2 13 72 120 0.279
Jhonny Peralta 569 84 146 28 3 13 68 152 0.257
Prince Fielder 569 82 154 35 1 28 81 125 0.271
Paul Konerko 566 97 177 30 0 35 113 104 0.313
Andruw Jones 565 107 148 29 0 41 129 127 0.262
Brian Roberts 563 85 161 34 3 10 55 66 0.286
Eric Byrnes 562 82 150 37 3 26 79 88 0.267
Adam Dunn 561 99 131 24 0 40 92 194 0.234
David Ortiz 558 115 160 29 2 54 137 117 0.287
Yuniesky Betancourt 558 68 161 28 6 8 47 54 0.289
Michael Cuddyer 557 102 158 41 5 24 109 130 0.284
Torii Hunter 557 86 155 21 2 31 98 108 0.278
Jay Payton 557 78 165 32 3 10 59 52 0.296
Juan Encarnacion 557 74 155 25 5 19 79 86 0.278
Nick Swisher 556 106 141 24 2 35 95 152 0.254
Tadahito Iguchi 555 97 156 24 0 18 67 110 0.281
Jason Kendall 552 76 163 23 0 1 50 54 0.295
Mike Cameron 552 88 148 34 9 22 83 142 0.268
Marcus Giles 550 87 144 32 2 11 60 105 0.262
Craig Biggio 548 79 135 33 0 21 62 84 0.246
Mark Grudzielanek 548 85 163 32 4 7 52 69 0.297
Ivan Rodriguez 547 74 164 28 4 13 69 86 0.3
Todd Helton 546 94 165 40 5 15 81 64 0.302
Aaron Hill 546 70 159 28 3 6 50 66 0.291
Joe Crede 544 76 154 31 0 30 94 58 0.283
Carlos Guillen 543 100 174 41 5 19 85 87 0.32
Garret Anderson 543 63 152 28 2 17 85 95 0.28
Jack Wilson 543 70 148 27 1 8 35 65 0.273
Brandon Inge 542 83 137 29 2 27 83 128 0.253
Craig Monroe 541 89 138 35 2 28 92 126 0.255
Troy Glaus 540 105 136 27 0 38 104 134 0.252
Jermaine Dye 539 103 170 27 3 44 120 118 0.315
Josh Barfield 539 72 151 32 3 13 58 81 0.28
Bill Hall 537 101 145 39 4 35 85 162 0.27
Brandon Phillips 536 65 148 28 1 17 75 88 0.276
Lance Berkman 536 95 169 29 0 45 136 106 0.315
Albert Pujols 535 119 177 33 1 49 137 50 0.331
Ronny Cedeno 534 51 131 18 7 6 41 109 0.245
Jacque Jones 533 73 152 31 1 27 81 116 0.285
Willy Taveras 529 83 147 19 5 1 30 88 0.278
Emil Brown 527 77 151 41 2 15 81 95 0.287
Scott Podsednik 524 86 137 27 6 3 45 96 0.261
Carlos Delgado 524 89 139 30 2 38 114 120 0.265
Joe Mauer 521 86 181 36 4 13 84 54 0.347
Scott Rolen 521 94 154 48 1 22 95 69 0.296
This article has been dead for over six months. Start a new discussion instead.