Question

Use basic C++

3. A text file, superstars.txt, contains statistics on cricket players. For each player, the file contains the players first

0 0
Add a comment Improve this question Transcribed image text
Answer #1

solution:
This solution has an explanation, full c++ code, comments with code and screenshot of

execution of the program for your clear and better understanding.

EXPLANATION:

Before going to directly answer first let's see the approach for solving this problem.

First, make a .txt file and store required no of players info.

Then make a main.cpp and construct a struct of given structures with first name, last name, total matched played,etc..

and make other functions as well for respective functionality and then call from main as required

Now for more visualization see the code below:

superstars.txt :

shivnarine Chanderpaul 164 11867 30 9
Brian lara 130 11912 34 0
Chris gayle 103 7214 15 73

c++ code :

// header files

#include<iostream>

#include<string>

#include<fstream>

using namespace std;

#define MAX 100

// Player str

struct Player {

string fName;

string lName;

int numOfMatchedPlayed,totalRunsScored;

int numOfCenturies,WicketsTaken;

};

// to read from file

int readPlayers(struct Player p[]) {

int count = 0;

string fname,lname;

int matchplayed,totalrun,totalcenturies,wickets;

// file ptr

fstream fp;

// open a file

fp.open("superstars.txt");

// read while Not end of file

while (fp>>fname>>lname>>matchplayed>>totalrun>>totalcenturies>>wickets)

{ // store into Player str

p[count].fName=fname;

p[count].lName=lname;

p[count].numOfMatchedPlayed = matchplayed;

p[count].totalRunsScored =totalrun;

p[count].numOfCenturies = totalcenturies;

p[count].WicketsTaken = wickets;

count++;

// cout<<"\n"<<fname<<" "<<lname<<" "<<matchplayed<<" "<<totalrun<<" "<<totalcenturies<<" "<<wickets;

}

// return total no of players from file

return count;

}

// check for max run

int maxRuns(Player p[],int n){

int maxRunIndex = -1;

int maxRun = -1; // assume initially max = -1

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

// update max run when found new maximum from earlier

if(maxRun<p[i].totalRunsScored){

maxRunIndex = i;

maxRun = p[i].totalRunsScored;

}

// return index of max run

return maxRunIndex;

}

// to diplay info of player

void displayPlayer(Player p){

cout<<"\nFirstName : "<<p.fName<<"\nLastName : "<<p.lName<<"\nmatch Played = "<<p.numOfMatchedPlayed<<"\nTotal scored Run = "<<p.totalRunsScored<<"\nTotal centuries = "<<p.numOfCenturies<<"\nNum. of Wickets Taken = "<<p.WicketsTaken;

}

// to search for player

int searchPlayer(Player p[],int n,string lname){

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

// searching from last name

if(p[i].lName == lname)

return i;

return -1;

}

// main function

int main()

{

// string to search

string lname = "lara";

// array of player str

struct Player p[MAX];

// calling function to read file

int x = readPlayers(p);

// calling function to know index of max run

int index = maxRuns(p,x);

// calling function to search a player

int lNameFoundIndex = searchPlayer(p,x,lname);

cout<<"\nPlayer who scored Max Run : "<<p[index].fName<<" "<<p[index].lName<<endl;

// if player with last name found then display info of that player

if(lNameFoundIndex>=0){

cout<<"\nInfo. of player with last name : "<<lname<<endl;

displayPlayer(p[lNameFoundIndex]);

}

else cout<<lname<<" NOT FOUND";

cout<<endl;

return 0;

}

screenshots :

This code is compiled and executed in visual studio code ide in ubuntu under LINUX.

Cmain.cpp superstars.txt x superstars.txt shivnarine Chanderpaul 164 11867 30 #include<iost ream> #include<string> #include<fsuperstars.txt x Cmain.cpp x superstars.txt ** shivnarine Chanderpaul 164 11867 36 int searchPlayer ( Player p[], int n, stri


since each and everything is provided for your full understanding.

However further if any difficulty in understanding the code and approach

then feel free to ask in the comments section.

I will definitely help you.

Add a comment
Know the answer?
Add Answer to:
Use basic C++ 3. A text file, superstars.txt, contains statistics on cricket players. For each player,...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • need help on C++ Discussion: The program should utilize 3 files. player.txt – Player name data...

    need help on C++ Discussion: The program should utilize 3 files. player.txt – Player name data file – It has: player ID, player first name, middle initial, last name soccer.txt – Player information file – It has: player ID, goals scored, number of penalties, jersey number output file - formatted according to the example provided later in this assignment a) Define a struct to hold the information for a person storing first name, middle initial, and   last name). b) Define...

  • Need done in C++ Can not get my code to properly display output. Instructions below. The...

    Need done in C++ Can not get my code to properly display output. Instructions below. The code compiles but output is very off. Write a program that scores the following data about a soccer player in a structure:            Player’s Name            Player’s Number            Points Scored by Player      The program should keep an array of 12 of these structures. Each element is for a different player on a team. When the program runs, it should ask the user...

  • c++ A menu-driven program gives the user the option to find statistics about different baseball players....

    c++ A menu-driven program gives the user the option to find statistics about different baseball players. The program reads from a file and stores the data for ten baseball players, including player’s team, name of player, number of homeruns, batting average, and runs batted in. (You can make up your data, or get it online, for example: http://espn.go.com/mlb/statistics ) Write a program that declares a struct to store the data for a player. Declare an array of 10 components to...

  • (C++) Write a program that declares a struct to store the data of a football player...

    (C++) 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 and a function to output data. Add functions to search the array to find the index of...

  • CAN YOU SOLVE THIS WITH EXPLANATION? The first part of your program will read some player...

    CAN YOU SOLVE THIS WITH EXPLANATION? The first part of your program will read some player information from a file, format the information, put it into an array of structures, and then display that array on the screen. You will find the following structure useful: The input for your program is in a file called players.txt. Each record in the file represents one player and is made up of multiple fields that are separated by colons. The fields for each...

  • 1. Create a file that contains 3 rows of data of the form: First Name Middle...

    1. Create a file that contains 3 rows of data of the form: First Name Middle Name Last Name Month Day Year Month Day Year GPA. This file represents student data consisting of first name, middle name, last name, registration month, registration day, registration year, birth month, birth day, birth year, current gpa. ...or you may download the data file inPut.txt. inPut.txt reads:​​​​​​​ Tsia Brian Smith 9 1 2013 2 27 1994 4.0 Harper Willis Smith 9 2 2013 9...

  • In C program Consider a file that contains employee records, called "empstat.txt." The data for each...

    In C program Consider a file that contains employee records, called "empstat.txt." The data for each employee consist of the employee’s last name (up to 20 characters), employee id number (up to 11 characters), gross pay for the week (double), taxes deducted (double) for the week, and net pay (double) for the week. Create a struct to hold the employee information. Write a void function called writeEmpFile that prompts the user to enter last name, id number, gross pay and...

  • Write a C++ program that demonstrates use of programmer-defined data structures (structs), an array of structs, passing...

    Write a C++ program that demonstrates use of programmer-defined data structures (structs), an array of structs, passing an array of structs to a function, and returning a struct as the value of a function. A function in the program should read several rows of data from a text file. (The data file should accompany this assignment.) Each row of the file contains a month name, a high temperature, and a low temperature. The main program should call another function which...

  • In C. I want to read in the data of a text file into an array...

    In C. I want to read in the data of a text file into an array of dynamically allocated structs. The file is read as a command-line argument The text file format is: <number of classes in this file> <department name of class 1>:<number of class 1>:<location of class 1> <department name of class 2>:<number of class 2>:<location of class 2> ... Assume that no piece of information in the file contains a colon: the colons are only used as...

  • In c++ Write a program that contains a class called Player. This class should contain two...

    In c++ Write a program that contains a class called Player. This class should contain two member variables: name, score. Here are the specifications: You should write get/set methods for all member variables. You should write a default constructor initializes the member variables to appropriate default values. Create an instance of Player in main. You should set the values on the instance and then print them out on the console. In Main Declare a variable that can hold a dynamcially...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT