Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * \brief A program that uses files as database for operating a football players card-index.
- *
- * This program has the functionality of:
- * - Adding new players
- * - Searching players by ID
- * - Searching players by Country
- * - Outputting all players
- * - Modifying goals scored by player's last name and country
- *
- * Things that can be added:
- * - Removing from file
- * - Modifying other player data, such as name, country, etc.
- * - More searching methods, such as by name, last_name, goals scored, etc.
- * - A lot of tweaks could be done to the player information
- * - etc...
- *
- * \version: 1.0
- *
- * \date 2018/12/12 13:44:06
- *
- */
- #include <iostream>
- #include <string>
- #include <vector>
- #include <fstream>
- #include <conio.h>
- using namespace std;
- /*!
- * A structure that contains the Football Player information
- */
- struct Player {
- string first_name;
- string last_name;
- string country;
- size_t id;
- size_t scored_goals;
- };
- /*!
- * \brief This function asks the user to input data, makes some checks
- * and if the checks pass, saves the information to a file named
- * players.dat by converting the struct into binary code.
- *
- * \param Player An array of custom data structure representing the data, may be
- * empty, but not null.
- * \param array_size A std::size_t defined variable used for the number of elements
- * in the array we mentioned earlier (Player array).
- *
- * \see std::size_t
- * \see auto specifier
- */
- auto input(Player* array, size_t array_size) -> void;
- /*!
- * \brief This function gets the information from the binary file and converts
- * it to its' representative data type and variable name of the structure.
- * It is the core function of the queries as well. As long as you understand
- * how this function works, the others will seem like they are the same.
- * (which they are, I just copied them from this function and added up more
- * features).
- *
- * \param Player An array of custom data structure representing the data, may be
- * empty, but not null.
- *
- * \see auto specifier
- */
- auto output(Player* array) -> void;
- /*!
- * \brief This function gets the information from the binary file and converts
- * it to its' representative data type and variable name of the structure
- * by a specific argument.In this example, we are using the ID to search
- * for the player(s) we want to show. The function itself asks the user to
- * input a player ID that he wants to search for in the file.
- *
- * For in-depth understanding, look at QueryByCountry function
- *
- * \param Player An array of custom data structure representing the data, may be
- * empty, but not null.
- *
- * \see auto specifier
- * \see QueryByCountry
- */
- auto QueryById(Player* array) -> void;
- /*!
- * \brief This function gets the information from the binary file and converts
- * it to its' representative data type and variable name of the structure
- * by a specific argument.In this example, we are using the country to search
- * for the player(s) we want to show. The function itself asks the user to
- * input a player country that he wants to search for in the file.
- *
- * For in-depth understanding, look at QueryByCountry function
- *
- * \param Player An array of custom data structure representing the data, may be
- * empty, but not null.
- *
- * \see std::size_t
- * \see auto specifier
- */
- auto QueryByCountry(Player* array) -> void;
- /*!
- * \brief This function modifies the information from the binary file and converts
- * it to its' representative data type and variable name of the structure
- * by a specific argument.In this example, we are using the players' country
- * and the players' last name to search for the player(s) we want to modify.
- * The function itself asks the user to input the player's country and
- * last name that he wants to search for in the file. Then if any records are
- * found, asks the user to enter goals scored. The goals scored has validation
- * check. If this check is passed, modifies the current player's goals scored.
- * If multiple players found with this country and last name, modifies them
- * one by one by the algorithm described above.
- *
- * For in-depth understanding, look at QueryByCountry function
- *
- * \param Player An array of custom data structure representing the data, may be
- * empty, but not null.
- *
- * \see std::size_t
- * \see auto specifier
- */
- auto ModifyData(Player* array) -> void;
- /*!
- * The main function which contains:
- * - A menu of choices for each function with the file. You can easily do:
- * -# Inputting data into file
- * -# Outputting the information from the file
- * -# Querying players by ID
- * -# Querying players by Country
- * -# Modifying goals scored on each player, queried by last name and country
- *
- * \brief The main function has been realized with pointers and dynamically allocation of
- * array. It has checks, that disallow negative data being used. It uses files to
- * store the information that has been passed and used in the program.
- *
- * \return int
- *
- * \see input
- * \see output
- * \see QueryById
- * \see QueryByCountry
- * \see ModifyData
- */
- int main()
- {
- size_t choice, array_size(30);
- /// Setting initial value different than null, because of fread function
- Player* array = new Player[array_size];
- do {
- system("cls");
- cout << "Menu: \n";
- cout << "[1] Input players\n";
- cout << "[2] Output Players\n";
- cout << "[3] Query players by ID\n";
- cout << "[4] Query players by Country\n";
- cout << "[5] Modify player data, queried by name and last name\n\n";
- cout << "[0] Exit\n";
- cout << "Make your choice: ";
- cin >> choice;
- switch (choice) {
- case 1:
- system("cls");
- do {
- cout << "Enter number of players u want to input: ";
- cin >> array_size;
- } while (array_size <= 0);
- array = new Player[array_size];
- input(array, array_size);
- cout << "\n\nEnter any key to get back to the menu...\n";
- _getch();
- break;
- case 2:
- system("cls");
- output(array);
- cout << "\n\nEnter any key to get back to the menu...\n";
- _getch();
- break;
- case 3:
- system("cls");
- QueryById(array);
- cout << "\n\nEnter any key to get back to the menu...\n";
- _getch();
- break;
- case 4:
- system("cls");
- cin.ignore();
- QueryByCountry(array);
- cout << "\n\nEnter any key to get back to the menu...\n";
- _getch();
- break;
- case 5:
- system("cls");
- ModifyData(array);
- cout << "\n\nEnter any key to get back to the menu...\n";
- _getch();
- break;
- }
- } while (choice != 0);
- /// Clearing the memory we used
- delete []array;
- return 0;
- }
- auto input(Player* array, size_t array_size) -> void {
- fstream file("players.dat", ios::binary | ios::out);
- for (size_t i = 0; i < array_size; i++) {
- cout << "Enter first name: ";
- cin >> array[i].first_name;
- cout << "Enter last name: ";
- cin >> array[i].last_name;
- cin.ignore();
- cout << "Enter country: ";
- getline(cin, array[i].country);
- do {
- cout << "Enter player id: ";
- cin >> array[i].id;
- } while (array[i].id < 0);
- do {
- cout << "Enter scored goals: ";
- cin >> array[i].scored_goals;
- } while (array[i].scored_goals < 0);
- }
- file.write((char *)array, array_size * sizeof(Player));
- file.close();
- }
- auto output(Player* array) -> void {
- /// Opening the file players.dat as binary for reading
- fstream file("players.dat", ios::binary | ios::in);
- /// Check if the file is present, else exits
- if (!file) {
- cout << "File not found!\n";
- exit(0);
- }
- else {
- /// Goes to the end of the file
- file.seekg(0L, ios::end);
- /// Gets the byte size of the whole file
- auto pos = file.tellg();
- /*!
- * Calculate the needed size of the array.
- *
- * How? We have the file byte size and our struct
- * represents just one item from our array.
- * To get our array size, we need to divide
- * the byte size of the file, where we stored
- * the array of Player, and the size in bytes
- * of our struct Player, thus: sizeof(Player).
- *
- * \see Player
- */
- auto array_size = pos / sizeof(Player);
- /// Getting back to the beginning of the file to read it
- file.seekg(0L, ios::beg);
- /*!
- * Reading the whole file, because we have array_size * sizeof(Player),
- * as a constant string(char *), and writing it by its' places in the struct.
- * - id from the file goes to id from the struct
- * - first_name from the file goes to first_name from the struct
- * - etc...
- *
- * \see Player
- */
- file.read((char *) array, array_size * sizeof(Player));
- /// Iterating through the array and outputting the information
- for (size_t i = 0; i < array_size; i++) {
- cout << "ID: " << array[i].id << endl;
- cout << "First Name: " << array[i].first_name << endl;
- cout << "Last Name: " << array[i].last_name << endl;
- cout << "Country: " << array[i].country << endl;
- cout << "Goals scored: " << array[i].scored_goals << endl;
- cout << "\n\n----------------------------------------------\n\n";
- }
- }
- /// Closing the file
- file.close();
- }
- auto QueryById(Player* array) -> void {
- size_t id, counter(0);
- cout << "Enter ID you want to search for: ";
- cin >> id;
- fstream file("players.dat", ios::binary | ios::in);
- if (!file) {
- cout << "File not found!\n";
- exit(0);
- }
- else {
- file.seekg(0L, ios::end);
- auto pos = file.tellg();
- auto array_size = pos / sizeof(Player);
- file.seekg(0L, ios::beg);
- file.read((char *)array, array_size * sizeof(Player));
- for (size_t i = 0; i < array_size; i++) {
- if (array[i].id == id) {
- cout << "ID: " << array[i].id << endl;
- cout << "First Name: " << array[i].first_name << endl;
- cout << "Last Name: " << array[i].last_name << endl;
- cout << "Country: " << array[i].country << endl;
- cout << "Goals scored: " << array[i].scored_goals << endl;
- counter++;
- }
- cout << "\n\n----------------------------------------------\n\n";
- }
- }
- if (counter == 0) {
- cout << "No players with this id found!\n\n";
- }
- file.close();
- }
- auto QueryByCountry(Player* array) -> void {
- /// Used for user input
- char choice;
- string country;
- /// Counter used for information only
- size_t counter(0);
- /// Opening the file players.dat as binary for reading
- fstream file("players.dat", ios::binary | ios::in);
- /// Checks if the file has opened correctly
- if (!file) {
- cout << "File not found!\n";
- exit(0);
- }
- else {
- /// Setting the pointer to the end of the file
- file.seekg(0L, ios::end);
- /// Get the whole file byte size.
- auto pos = file.tellg();
- /*!
- * Calculate the needed size of the array.
- *
- * How? We have the file byte size and our struct
- * represents just one item from our array.
- * To get our array size, we need to divide
- * the byte size of the file, where we stored
- * the array of Player, and the size in bytes
- * of our struct Player, thus: sizeof(Player).
- *
- * \see Player
- */
- auto array_size = pos / sizeof(Player);
- /// Getting back to the beginning of the file to read it
- file.seekg(0L, ios::beg);
- /*!
- * Reading the whole file, because we have array_size * sizeof(Player),
- * as a constant string(char *), and writing it by its' places in the struct.
- * - id from the file goes to id from the struct
- * - first_name from the file goes to first_name from the struct
- * - etc...
- *
- * \see Player
- */
- file.read((char *)array, array_size * sizeof(Player));
- do {
- /*!
- * Inputting what country we want to search for.
- *
- * \see getline
- * \see std::string
- */
- cout << "Enter country you want to search for: ";
- getline(cin, country);
- /// Iterating through the array
- for (size_t i = 0; i < array_size; i++) {
- /// Check if each element's country is equal to what we inputted
- if (array[i].country == country) {
- cout << "ID: " << array[i].id << endl;
- cout << "First Name: " << array[i].first_name << endl;
- cout << "Last Name: " << array[i].last_name << endl;
- cout << "Country: " << array[i].country << endl;
- cout << "Goals scored: " << array[i].scored_goals << endl;
- /// Counter used for information only
- counter++;
- }
- cout << "\n\n----------------------------------------------\n\n";
- }
- /// Counter used for information only
- if (counter == 0) {
- cout << "No players with this country found!\n\n";
- }
- /*!
- * Asking the user if he wants to query another country.
- *
- * \see _getch()
- * \see char
- */
- cout << "Do you want to search for another country? (y/n) ";
- choice = _getch();
- /*!
- * While the user enters anything different than the character n
- * do this whole thing with the asking and printing.
- */
- } while (choice != 'n');
- }
- /// Closing the opened file
- file.close();
- }
- auto ModifyData(Player* array) -> void {
- string last_name, country;
- size_t counter(0);
- cout << "Enter Country you want to search for: ";
- cin >> country;
- cout << "Enter Last Name you want to search for: ";
- cin >> last_name;
- ifstream file("players.dat", ios::binary | ios::in);
- if (!file) {
- cout << "File not found!\n";
- exit(0);
- }
- else {
- file.seekg(0L, ios::end);
- auto pos = file.tellg();
- auto array_size = pos / sizeof(Player);
- file.seekg(0L, ios::beg);
- file.read((char *)array, array_size * sizeof(Player));
- for (size_t i = 0; i < array_size; i++) {
- if (array[i].country == country && array[i].last_name == last_name) {
- size_t goals_scored;
- cout << "ID: " << array[i].id << endl;
- cout << "First Name: " << array[i].first_name << endl;
- cout << "Last Name: " << array[i].last_name << endl;
- cout << "Country: " << array[i].country << endl;
- cout << "Goals scored: " << array[i].scored_goals << endl;
- cout << "\n\n";
- cout << "Modifying goals scored...\n\n";
- do {
- cout << "Enter goals scored: ";
- cin >> goals_scored;
- } while (goals_scored < 0);
- /// Modifying the value
- array[i].scored_goals = goals_scored;
- cout << "\n\nModified values:\n\n";
- cout << "ID: " << array[i].id << endl;
- cout << "First Name: " << array[i].first_name << endl;
- cout << "Last Name: " << array[i].last_name << endl;
- cout << "Country: " << array[i].country << endl;
- cout << "Goals scored: " << array[i].scored_goals << endl;
- counter++;
- }
- cout << "\n\n----------------------------------------------\n\n";
- }
- if (counter == 0) {
- cout << "No players with this combination of country and last name found!\n\n";
- file.close();
- }
- else {
- file.close();
- cout << "\n\nModifying file...\n\n";
- /// Modifying the file with the new modified structure
- fstream file("players.dat", ios::binary | ios::out);
- file.write((char *)array, array_size * sizeof(Player));
- file.close();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement