Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++ Please! Write a program that declares a struct to store the data of a footba

ID: 3859371 • Letter: C

Question

C++ Please!

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  components to store the data of 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.

Explanation / Answer

the program for the above struct program for fotball player is as follows

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


struct players
{
   char name[20], position[50];
   int touchdowns, catches, passingYards,
       receivingYards, runningYards;


};
void printmenu();
void printPlayersData(struct players s[], int size);
void searchNamestr(struct players s[], int size, string searchName);

int main()
{
   int i,choice, notquit = 1;
   ifstream inputfile;
   string searchName;
   bool found = false;
   int index;

   inputfile.open("footballPlayers.txt");  

   struct players s[10];

   for (i = 1; i < 10; i++)
   {

       inputfile >> s[i].name >> s[i].position >> s[i].runningYards >> s[i].catches
       >> s[i].passingYards >>s[i].receivingYards >> s[i].touchdowns;
   }
  
   while (notquit)
   {

       printmenu();
       //take choice from menu
       cin >> choice;
       switch (choice)
       {
       case 1:printPlayersData(s,10);
           system("pause");
           break;
       case 2:
       searchNamestr(s,10, searchName );
           system("pause");
           break;
       case 3:
           system("pause");
           break;
       case 0: notquit = 0;
           break;
       default:
           cout << "Wrong Entry" << endl;
           system("pause");//lets pause it
       }
   }

//close the file
   inputfile.close();                  
   system("pause");
   return 0;
}

void printmenu()
{
   system("CLS");
   cout << "--------Please select your option-------" << endl;
   cout << " 1. Display Player Information ";
   cout << " 2. Search Player";
   cout << " 3. Update Player's Information";
   cout << " 0. Exit :) " << endl;
   cout << " Selection: ";
  
}

void printPlayersData(struct players s[], int size)
{
   int i;
   for (i = 1; i<10; i++)
   {
       cout << " Name: " << s[i].name;
       cout << " Position: " << s[i].position;
       cout << " Touchdowns: " << s[i].touchdowns;
       cout << " Catches: " << s[i].catches;
       cout << " Passing Yards: " << s[i].passingYards;
       cout << " Receiving Yards: " << s[i].receivingYards;
       cout << " Running Yards: " << s[i].runningYards;
       cout << " ------------------------------" << endl;
   }

}
void searchNamestr(struct players s[], int size, string searchName)
{
   string searchNameString;
   cout << "Please enter name of the player: ";
   cin >> searchNameString;
  
   for (int i = 0; i < size; i++)
   {
       if (s[i].name == searchNameString)   
       {
           return i; //return index of the player name
       }
   }
   return -1;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote