'Stuck on project

I have an assignment in my c++ class which is: Write a program that declares a struct to store the data of a football player (player’s name, player’s position, number of touchdowns, number of catches, number of passing yards, number of receiving yards, and the number of rushing yards). Declare an array of 10 components to store the data of 10 football players. Your program must contain a function to input data from a file (You will need to create the input file) and functions to output data to the console. Add functions to search the array to find the index of a specific player, and update the data of a player. Before the program terminates, give the user the option to save data in a file (write a function for this). Your program should be menu driven, giving the user various choices. Your main function should mostly have variable declarations and function calls in it. Most operations should be handled by other functions.

I am stuck on getting my program to read the info on my input file. please help

#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>

using namespace std;

struct player {
    string name;
    string position;
    int touchdowns;
    int catches;
    int pass;
    int receive;
    int rush;
};

    int addData( ifstream& ifile, int count , player football[])
    {
            if (count == 0)
            {
                ifile >> football[count].name;
                ifile >> football[count].position;
                ifile >> football[count].touchdowns;
                ifile >> football[count].catches;
                ifile >> football[count].pass;
                ifile >> football[count].receive;
                ifile >> football[count].rush;
                count++;
            }
            else if (count != 0)
            {
                int i = 0;
                string name;
                string position;
                int touchdowns;
                int catches;
                int pass;
                int receive;
                int rush;

                while (i != -1)
                {
                    ifile >> name >> position >> touchdowns >> catches >> pass >> receive >> rush;
                    if((*(&football + 1) - football) > 0)
                    {
                        if (name == football[i].name)
                        {
                        i++;
                        }
                        if (name != football[i].name)
                        {
                        football[count].name = name;
                        football[count].position = position;
                        football[count].touchdowns = touchdowns;
                        football[count].catches = catches;
                        football[count].pass = pass;
                        football[count].receive = receive;
                        football[count].rush = rush;
                        i = -1;
                        count++;
                        }
                    }
                }
            }
            return count;
        }

void display(int count, player football[])
{
        cout << "There are " << count << " players," << endl;
        if (count <= 0)
        {
            cout << "No players to display. ";
        }
        else
        {
            for (int i = 0; i < count;i++)
            {
                cout << i + 1 <<". " << football[i].name << endl;
                cout << "Position: " << football[i].position << endl;
                cout << "Touchdowns: " << football[i].touchdowns << endl;
                cout << "Catches: " << football[i].catches << endl;
                cout << "Passes: " << football[i].pass << endl;
                cout << "Receiving Yards: " << football[i].receive << endl;
                cout << "Rushing Yards: " << football[i].rush << endl;
            }
        }
}

void findandUpdate(string search, int count, player football[])
{
        string name;
        string position;
        int touchdowns;
        int catches;
        int pass;
        int receive;
        int rush;

        bool found = false;

        for (int i = 0; i < count; i++)
        {

            if (search == football[i].name)
            {
                found = true;
                cout << "Searched player is at index " << i + 1 << endl;
                cout << "You may now update the player's name, player's position, number of touchdowns, number of catches,"
                    << "number of passing yards, number of receiving yards, and the number of rushing yards \n";
                cout << "Name: "; cin >> name;
                cout << "Position: "; cin >> position;
                cout << "Touchdowns: "; cin >> touchdowns;
                cout << "Catches: "; cin >> catches;
                cout << "Passes: "; cin >> pass;
                cout << "Received: "; cin >> receive;
                cout << "Rushed: "; cin >> rush;

                football[i].name = name;
                football[i].position = position;
                football[i].touchdowns = touchdowns;
                football[i].catches = catches;
                football[i].pass = pass;
                football[i].receive = receive;
                football[i].rush = rush;
            }
        }
        if (found == false)
        {
            cout << "No results.";
        }
}

    int main()
    {
        cout << "Press 1 to add a player.\nPress 2 to display players.\nPress 3 to find a player.\nPress 4 to update player information.\nPress 5 to save data.\n";
        int x = 0;
        cin >> x;

        ifstream ifile;
        player football[10];
        ifile.open("footballplayers.txt");

        int i = 0;
        int count = 0;

        while (i != -1)
        {
            switch (x)
            {
            case 1:
            {
                count = addData(ifile, count, football);
            }
            break;
            case 2: display(count, football);
                break;
            case 3:
            {
                string name;
                cout << "Enter football player name: ";
                cin >> name;
                findandUpdate(name,count, football);
            }
                    break;
            default: i = -1;
            }
        }
        ifile.close();

    string answer;
    cout << "Save data? Type yes or no.";
    cin >> answer;
    if (answer == "yes")
    {
        ofstream ofile;
        ofile.open("SavePlayerData.txt");
        for (int i = 0; i < count; i++) {
            ofile << football[i].name + " ";
            ofile << football[i].name + " ";
            ofile << football[i].position + " ";
            ofile << football[i].touchdowns + " ";
            ofile << football[i].catches + " ";
            ofile << football[i].pass + " ";
            ofile << football[i].receive + " ";
            ofile << football[i].rush + " ";
            ofile << "\n";
        }
        ofile.close();
    }
}

Input file:

George RB 12 5 0 47 578
Brett QB 5 0 2017 0 44
Peyton WR 3 15 0 246 12
Logan WR 2 7 0 89 6
Nolan WR 1 2 3 0 0
John TE 5 17 0 402 0
Brandon QB 4 0 310 0 0
Dalton RB 1 2  43 5
Justin WR 1 10 0 441 0
Mason WR 0 2 0 12 0
c++


Solution 1:[1]

your basic logic does not do as asked in the specs. First thing it should do is read the file, before asking for any input from a menu. You have tried to create a strange hybrid of reading the file and interactively adding a player (which is not part of the requirements)

So simplify and get it so that you read the whole file in a function called 'loadFile' and then print the whole thing out (like your option 2).

Get those things going first then add the menu to ask for modify, find and print

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 pm100