133k views
5 votes
Football Club Roster

Write a c++ program that creates an array with the names of your favorite football club (or any other group if you like, as long as it has at least 20 members). Include first and last names. You must read in the data from an external file (you can have one name per line). Your data must reside in an external file that you will provide. Attach both the text file and your program together.
Make sure to declare the array with the right size. Your program should then sort the members in alphabetical order of last name and display the name of each member (one per line).

1 Answer

4 votes

Final answer:

To manage a football club roster in C++, create an external file with at least 20 member names, read the names into an array, sort them by last name, and display them. Example code is provided to illustrate file operations and array sorting.

Step-by-step explanation:

You've been tasked with creating a C++ program to manage a football club roster. To accomplish this, you will need to perform several steps. First, you will create an external text file to store the names of at least 20 football club members. Each name will be on a separate line and will include both first and last names. Once your file is ready, your C++ program will read in these names and store them in an array that is adequately sized to hold all members. After reading in all the names, your program should then sort the array in alphabetical order based on the members' last names. Finally, your sorted roster will be displayed on the screen, with each member's full name on a separate line. Remember to handle file reading operations with proper error checking to ensure robustness.

Here is an example of what the beginning of your program might look like:

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

const int MEMBERS = 20;
string roster[MEMBERS];

int main() {
ifstream inFile("football_roster.txt");
if (!inFile) {
cerr << "Unable to open file" << endl;
return 1;
}

// Read names into the array
for (int i = 0; i < MEMBERS; ++i) {
getline(inFile, roster[i]);
}

inFile.close();

// Sort the array
sort(roster, roster + MEMBERS);

// Output sorted names
for (const auto& name : roster) {
cout << name << endl;
}

return 0;
}

User Condit
by
7.3k points