Write a C program that declares a struct to store the data o

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 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

Solution

Here is the C++ program for the above scenario:

#include <iostream>

#include <string>

#include <fstream>                                                                       

#include <iomanip>

using namespace std;

struct players

{

char name[20], position[50];

int touchdowns, catches, passingYards,

receivingYards, runningYards;

};

void printmenu();

void printData(struct players s[], int size);

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

int main()

{

int i,choice, notquit = 1;

ifstream infile;

string searchName;

bool found = false;

int index;

infile.open(\"footballPlayers.txt\");           

struct players s[10];

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

{

infile >> s[i].name >> s[i].position >> s[i].touchdowns >> s[i].catches >> s[i].passingYards >>

s[i].receivingYards >> s[i].runningYards;

}             

while (notquit)

{

printmenu();

cin >> choice;

switch (choice)

{

case 1:printData(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, please try again\" << endl;

system(\"pause\");

}

}

infile.close();                                                                     

ofile.close();                                                                      

system(\"pause\");

return 0;

}

void printmenu()

{

system(\"CLS\");

cout << \"*******Please select your option*******\" << endl;

cout << \"\ 1. Display Player\'s Information \";

cout << \"\ 2. Search a Player\";

cout << \"\ 3. Update a Player\'s Information\";

cout << \"\ 0. Exit\" << endl;

cout << \"\ Selection: \";

}

void printData(struct players s[], int size)

{

int i;

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

{

cout << \"--------------------------------\";

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)

{

cout << \"Enter the name of the player: \";

cin >> searchName;

int flag = 0;    // set flag to off

for (int i = 1; i < 10; i++)    // start to loop through the array

{

if (s[i].name == searchName)   // if match is found

{

flag = 1;   // turn flag on

break;    // break out of for loop

}

}

for (int i = 0; i<10; i++)

if (flag)    // if flag is TRUE (1)

{

cout << \"The number of touchdowns \" << s[i].touchdowns << \".\ \" << \"The Number of catches: \" << s[i].catches << endl;

}

else

{

cout << \"Sorry, I could not find that player in this array.\" << endl << endl;

}

}

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, nu
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, nu
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, nu
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, nu

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site