View the step-by-step solution to:

I have been having problems wit my code. I am so confused and couldn't get it to work. I am new to this.

I have been having problems wit my code. I am so confused and couldn't get it to work. I am new to this.


/* program that declares a struct to store the data of a football player (players name, players 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 and a function to output data. Add functions to search the array

to find the index of a specific player, and update the data of a player. (You may assume that input data is stored in a file.) Before the program

terminates, give the user the option to save data in a file. Your program should be menu driven, giving the user various choices.

Given Data:

Bill Quarter_Back 70 0 8754 0 573

Jackson Receiver 55 87 50 5490 574

Grahm Running_Back 45 30 0 50 2800

McCoy Full_Back 25 10 0 25 3762

Daryl Quarter_Back 50 2 7560 0 450

Santiago Left_Tackle 5 0 0 0 0

Hanks Receiver 35 37 0 3590 876

Johnson Running_Back 25 80 0 100 4000

Miller Receiver 110 250 150 7867 2100

Ruth Quarter_Back 85 0 12901 0 3249*/


#include "pch.h"

#include <iostream>

#include <fstream>

#include <string>

using namespace std;

void dataInput(struct footballPlayer players[], int); // function prototype to input data

void dataOutput(struct footballPlayer players[], int); // function prototype to output data

int playerSearch(struct footballPlayer players[], int, string); // function prototype to search the array

void saveData(struct footballPlayer players[], int); //function prototype to save data

struct footballPlayer

{

 string playerName;

 string playerPosition;

 int numberOfTouchdowns, numberOfCatches, numberOfPassingYards, numberOfReceivingYards, numberOfRushingYards;

};

int main()

{

 const int max = 10;

 footballPlayer players[max];

 int selection = 0;

 int index = 0;

 do {

  cout << "Select one of the following options:n";

  cout << "1. To print a player's datan";

  cout << "2. To print the entire datan";

  cout << "3. To update the number of player's touch downsn";

  cout << "4. To update the number of player's number of catchesn";

  cout << "5. To update the number of player's passing yardsn";

  cout << "6. To update the number of player's receiving yardsn";

  cout << "7. To update the number of player's rushing yardsn";

  cout << "99. To quit the program" << endl;

  cout << "Enter your selection: ";

  cin >> selection;

  if (selection == 1) {

   string name;

   cout << "Enter player's name: ";

   cin >> name;

   playerSearch(players, max, name);

  }

  else if (selection == 2) {

   cout << "Details of all players are as follows:" << endl;

   dataOutput (players, max);

  }

  else if (selection == 3) {

   cout << "Enter the position of the player: ";

   cin >> players[index].playerPosition;

   cout << "The detail is updated." << endl;

  }

  else if (selection == 4) {

   cout << "Enter the the number of touchdowns: ";

   cin >> players[index].numberOfTouchdowns;

   cout << "The detail is updated." << endl;

  }

  else if (selection == 5) {

   cout << "Enter the number of catches: ";

   cin >> players[index].numberOfCatches;

   cout << "The detail is updated." << endl;

  }

  else if (selection == 6) {

   cout << "Enter the number of passing yards: ";

   cin >> players[index].numberOfPassingYards;

   cout << "The detail is updated." << endl;

  }

  else if (selection == 7) {

   cout << "Enter the number of receiving yards: ";

   cin >> players[index].numberOfReceivingYards;

   cout << "The detail is updated." << endl;

  }

  else if (selection == 8) {

   cout << "Enter the number of rushing yards: ";

   cin >> players[index].numberOfRushingYards;

   cout << "The detail is updated." << endl;

  }

  else if (selection == 99) {

   cout << "Would you like to save the data (y,Y/n,N) ";

   cin << endl;

  } 

 }

 while (selection != 99);

  

  system("pause");

 return 0;

}

void dataInput(footballPlayer player[], int length)

{

 ifstream myFile;

 myFile.open("indata.txt");

 int index = 0;

 while (!myFile.eof()) {

  cout << "Name: ";

  myFile >> players[index].playerName;

  cout << "Position: ";

  myFile >> players[index].playerPosition;

  cout << "Touch Downs: ";

  myFile >> players[index].numberOfTouchdowns;

  cout << "Catches: ";

  myFile >> players[index].numberOfCatches;

  cout << "Passing Yards: ";

  myFile >> players[index].numberOfPassingYards;

  cout << "Receiving Yards: ";

  myFile >> players[index].numberOfReceivingYards;

  cout << "Rushing Yards: ";

  myFile >> players[index].numberOfRushingYards;

   index++;

  }

  index--;

  myFile.close();

}

void dataOutput(struct footballPlayer players[], int length)

{

 for (int index = 0; index < length; index++) {

  cout << players[index].playerName << " ";

  cout << players[index].playerPosition << " ";

  cout << players[index].numberOfTouchdowns << " ";

  cout << players[index].numberOfCatches << " ";

  cout << players[index].numberOfPassingYards << " ";

  cout << players[index].numberOfReceivingYards << " ";

  cout << players[index].numberOfRushingYards << endl;

 }

}

int playerSearch(footballPlayer players[], int length, string name)

{

 bool found = false;

 int index;

 for (int index = 0; index < length; index++) {

  if (players[index].playerName == name) {

   return index;

   break;

  }

  else

   return -1;

 }

}

void saveData(footballPlayer players[], int length)

{

 ofstream outFile;

 outFile.open("outData.dat");

 for (int index = 0; index < length; index++) {

  outFile << (index + 1) << " ";

  outFile << players[index].playerName << " ";

  outFile << players[index].playerPosition << " ";

  outFile << players[index].numberOfTouchdowns << " ";

  outFile << players[index].numberOfCatches << " ";

  outFile << players[index].numberOfPassingYards << " ";

  outFile << players[index].numberOfReceivingYards << " ";

  outFile << players[index].numberOfRushingYards << endl;

 }

 outFile.close();

}

Top Answer

The result of the program is:   Case 1 *******Please select your option*******   1. Display Player's Information 2. Search... View the full answer

1a.PNG

Sign up to view the full answer

Why Join Course Hero?

Course Hero has all the homework and study help you need to succeed! We’ve got course-specific notes, study guides, and practice tests along with expert tutors.

-

Educational Resources
  • -

    Study Documents

    Find the best study resources around, tagged to your specific courses. Share your own to gain free Course Hero access.

    Browse Documents
  • -

    Question & Answers

    Get one-on-one homework help from our expert tutors—available online 24/7. Ask your own questions or browse existing Q&A threads. Satisfaction guaranteed!

    Ask a Question
Ask a homework question - tutors are online