(Write a C++ program ) that declares a struct to store the data of a football pl
ID: 3770146 • Letter: #
Question
(Write a C++ 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 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. (An input file has been provided with test data.) Before the program terminates, save the updated data in a file (you can just read in the file before the main menu and write out the file once the use has chosen to quit). Your program should contain two menus. Once which has the following options:
Select a player (by name)
Display player information
Update player information
Quit
If option 3 is selected a second menu should be displayed showing a list of what items can be updated. When the user selects an item, the program should ask the user for the new information and then update the appropriate piece of information in the player list. The user should be allowed to update statistics for that player until they decide to quit.
Analyze and design this program. Implement the design, compile and test the code. Turn in your analysis and design and your source code.
Explanation / Answer
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
const int SIZE = 50;
int Totalcatches(int *, int);
struct Player
{
char name[SIZE]; //Player's Name
int position; //Player's Number
int catches; //Point's Scored
int touchdowns;
int passing_yards;
int receiving_yards;
int rushing_yards;
};
int main()
{
const int NUM_PLAYERS = 12; //Number of Players
// Dynamically allocate the memory needed.
Player *players = new Player[NUM_PLAYERS]; //Array of structures
int index; //Loop
int total=0;
// Get Player data.
cout << " You will need the following information. ";
cout << "The Player's Names, Player's Numbers ";
cout << "Finally you will need the catches Scored by Players. ";
cout << "Number of touchdowns ";
cout << "Number of passing yards ";
cout << "Number of receiving yards ";
cout << "Number of rushing yards ";
cout << "The Player's Names, Player's Numbers ";
for (index = 0; index < NUM_PLAYERS; index++)
{
cout << "Please enter the Player's Name: ";
cin.getline( players[index].name, 50 );
cout << "Please enter the Player's Number: ";
( cin >> players[index].position ).get();
//To test my values for zero, negative
while (players[index].position <=0)
{
cout << "Zero or negative numbers not allowed ";
cout << "Please enter the Player's Number: ";
(cin >> players[index].position).get();
}
cout << "Please enter the catches Scored by the Player: ";
( cin >> players[index].catches ).get();
//To test my values for zero, negative.
while (players[index].catches < 0)
{
cout << "Zero or negative numbers not allowed ";
cout << "Please enter the catches Scored by the Player: ";
(cin >> players[index].catches).get();
}
cout << "Please enter the touchdowns: ";
( cin >> players[index].touchdowns ).get();
//To test my values for zero, negative.
while (players[index].touchdowns < 0)
{
cout << "Zero or negative numbers not allowed ";
cout << "Please enter the touchdowns by the Player: ";
(cin >> players[index].touchdowns).get();
}
cout << "Please enter the passing yards: ";
( cin >> players[index].passing_yards).get();
//To test my values for zero, negative.
while (players[index].passing_yards < 0)
{
cout << "Zero or negative numbers not allowed ";
cout << "Please enter the passing yards by the Player: ";
(cin >> players[index].passing_yards).get();
}
cout << "Please enter the receiving yards: ";
( cin >> players[index].receiving_yards).get();
//To test my values for zero, negative.
while (players[index].receiving_yards < 0)
{
cout << "Zero or negative numbers not allowed ";
cout << "Please enter the touchdowns by the Player: ";
(cin >> players[index].receiving_yards).get();
}
cout << "Please enter the rushing yards";
( cin >> players[index].rushing_yards).get();
//To test my values for zero, negative.
while (players[index].rushing_yards < 0)
{
cout << "Zero or negative numbers not allowed ";
cout << "Please enter the rushing_yards by the Player: ";
(cin >> players[index].rushing_yards).get();
}
cout << endl << endl;
}
//Display the players data
cout << "Here is the players data: ";
cout << " Name Number Score ";
cout << "-------------------------------- ";
for (index = 0; index < NUM_PLAYERS; index++)
{
cout << setw(8) << players[index].name;
cout << setw(8) << players[index].position;
cout << setw(8) << players[index].catches << endl;
cout << setw(8) << players[index].touchdowns << endl;
cout << setw(8) << players[index].passing_yards << endl;
cout << setw(8) << players[index].receiving_yards << endl;
cout << setw(8) << players[index].rushing_yards << endl;
}
//Calculate the total points
for (index = 0; index < NUM_PLAYERS; index++)
{
total += players[index].catches;
}
//Display the results of the total points.
cout << " The total of points scored by the team are: ";
cout << total << endl;
//To get the player with most points
int max = players[0].catches;
int maxIndex = 0;
for (int index = 0; index < 12; index++)
{
if (players[index].catches > max)
{
max = players[index].catches;
maxIndex = index;
}
}
cout << "highest score by: <" << players[maxIndex].name << "> number: " << players[maxIndex].position << endl;
cin.get();
// Delete the memory.
delete [] players;
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.