Question

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 a struct to store the data for a Soccer player storing the player’s name (use the
struct created earlier), position (Forward, etc.), number of goals scored, number of
penalties committed, his/her Jersey number, and status.
c) Declare an array of 30 (use a named constant) components/structs, one for each player.


Include the following capabilities:


1) A bool function to check if a given file (its parameter) exists or not. The function will
have the file name as parameters. The function should return false, if the file cannot be
found. You can use the returned value to terminate the program, if a valid file name has
not been entered by the user. You can call this function twice, once to check for the
player name file and once to check for the player information file.


  2) A void function to read the data into the array from the data files (player name and
       player information data files). The function should accept as parameters the input data
    file names and the array of struct. The file names are supplied through the command line
    arguments.


    3) A void function to identify the status of a player: 0-1 goals scored is Poor, 2-4 goals
    scored is Good, and more than 4 goals scored is Excellent. The identification is
   INVALID for any other situation. This value will be stored in the status field of the
struct.


4) A void function to write the data to a file whose named is entered through the command
line argument. The function accepts the file name and the data array as its parameter.
Display the data using a tabular format placing a space after each data item. See the
   sample output a bit later.


The main function should only be a collection of function calls in addition to the required
declaration of variables. You will also process the command line arguments in the main function.


Input data file example (player.txt):
Item 1: ID
Item 2: First name
Item 3: Middle initial
Item 4: last name


Input data file example (soccer.txt):
Item 1: ID
Item 2: position
Item 3: Number of goals scored
Item 4: Number of penalties
Item 5: Jersey number


Here is an example of the input data file (player.txt):
1 Larry L Page
2 Sergey B Brin
3 Marissa A Mayer


Here is an example of the input data file (soccer.txt):
1 forward 8 2 21
2 defense 12 0 82
3 goalkeeper -1 0 5


Here are some sample runs:
./a.out
Error: program needs arguments – Example: ./a.out player.txt soccer.txt output.txt


./a.out player.txt soccer.txt output.txt
Here is a sample output file – output.txt
Larry L Page 8 2 21 forward Excellent
Sergey B Brin 12 0 82 defense Excellent
Marissa A Mayer -1 0 5 goalkeeper INVALID


Of course, above is just a sample example. Anyone with 2 – 4 goals will be considered Good,
etc. make sure to fully test the code with various data values.


./a.out playerzz.txt soccer.txt output.txt
Missing input data file: playerzz.txt - terminating program


./a.out player.txt socceroo.txt output.txt
Missing input data file: socceroo.txt - terminating program

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

Here is the program

#include <iostream>
#include <fstream>
#include <string.h>
#include <stdio.h>
using namespace std;

// define player structure
struct player
{
   int id;
   string first_name;
   char middle_initial;
   string last_name;
};

//define soccer structure
struct soccer
{
   int id;
   player name;
   string position;
   int goal;
   int penalty;
   int jersey;
   string status;
};

const int TOTAL = 30;
int size = 0;
soccer player_data[TOTAL];

//check if the file exists
bool is_exist(string fileName)
{
ifstream infile(fileName);
return infile.good();
}

// set the status of player
void set_status(soccer *s)
{
s->status = "INVALID";
if(s->goal==0 || s->goal==1) s->status = "Poor";
if(s->goal==2 || s->goal==2 || s->goal == 3) s->status = "Good";
if(s->goal >= 4) s->status = "Excellent";
}

// read input files and store the data in struct array
void read_data(string playerFile, string dataFile, soccer data[] )
{
ifstream pfile(playerFile);
ifstream sfile(dataFile);
int i = 0;
char * pch;
int n = 0;
int j = 0;
if (pfile.is_open()) {
string pline;
while (getline(pfile, pline)) {
player pname;
n = pline.length();
char char_array[n + 1];
// copying the contents of the string to char array
strcpy(char_array, pline.c_str());
pch = strtok (char_array," ");
while (pch != NULL)
{
if(j == 0){
//cout << "id is " << pch;
pname.id = stoi(pch);
} else if(j == 1) {
//cout << "first name is " << pch;
pname.first_name = pch;
} else if(j == 2) {
//cout << "middle name is " << pch;
pname.middle_initial = pch[0];
} else if(j == 3) {
//cout << "last name is " << pch;
pname.last_name = pch;
}
pch = strtok (NULL, " ");
j++;
//cout << endl;
}
data[i].id = pname.id;
data[i].name = pname;
j=0;
i++;
}
pfile.close();
  
}
i=0;
if (sfile.is_open()) {
string sline;
while (getline(sfile, sline)) {
n = sline.length();
char char_array[n + 1];
// copying the contents of the string to char array
strcpy(char_array, sline.c_str());
pch = strtok (char_array," ");
j=0;
while (pch != NULL)
{
if(j == 0) {
//cout << "id is " << pch;
} else if(j == 1) {
//cout << "position is " << pch;
data[i].position = pch;
} else if(j == 2) {
//cout << "goal is " << pch;
data[i].goal = stoi(pch);
} else if(j == 3) {
//cout << "penalty is " << pch;
data[i].penalty = stoi(pch);
} else if(j == 4) {
//cout << "jersey is " << pch;
data[i].jersey = stoi(pch);
}
//printf ("%s\n",pch);
pch = strtok (NULL, " ");
j++;
}
j=0;
set_status( &data[i]);
//cout << "status is " << data[i].status;
//cout << endl;
i++;
}
sfile.close();
}
size =i;
}

// write data to output file
void write_data(string output_file, soccer data[])
{
ofstream outfile(output_file);
string output;
/* Same output as below
Larry L Page 8 2 21 forward Excellent
Sergey B Brin 12 0 82 defense Excellent
Marissa A Mayer -1 0 5 goalkeeper INVALID
*/
for (int i = 0; i < size; i++)
{
/*
cout << data[i].name.first_name;
cout << data[i].name.middle_initial;
cout << data[i].name.last_name;
cout << data[i].goal;
cout << data[i].penalty;
cout << data[i].jersey;
cout << data[i].position;
cout << data[i].status;
*/
outfile << data[i].name.first_name << " " << data[i].name.middle_initial
<< " " << data[i].name.last_name << " " << data[i].goal << " "<< data[i].penalty
<< " " << data[i].jersey << " " << data[i].position << " " << data[i].status << endl;
}
outfile.close();
}

// The main function
int main (int argc, char** argv)
{
if(argc != 4) {
cout << "wrong format. Enter in format ./a.out player.txt soccer.txt output.txt";
exit(1);
}
string player_file = argv[1];
if(!is_exist(player_file)) {
cout << "Missing input data file:" << player_file << " - terminating program";
exit(1);
}
  
string soccer_file = argv[2];
if(!is_exist(soccer_file)) {
cout << "Missing input data file:" << soccer_file << " - terminating program";
exit(1);
}
  
string output_file = argv[3];
read_data(player_file, soccer_file, player_data );
write_data(output_file, player_data);
return 0;
}

Add a comment
Know the answer?
Add Answer to:
need help on C++ Discussion: The program should utilize 3 files. player.txt – Player name data...
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
  • 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...

  • Use basic C++ 3. A text file, superstars.txt, contains statistics on cricket players. For each player,...

    Use basic C++ 3. A text file, superstars.txt, contains statistics on cricket players. For each player, the file contains the player's first name, last name, number of matches played, total number of runs scored, number of times the player scored 100 runs in a match, and the number of wickets taken. The last row of data in the file contains the word "END" only. Some rows are shown below 30 11867 164 Chanderpaul Shivnarine 34 11912 130 Lara Brian 73...

  • Need C programming help. I've started to work on the program, however I struggle when using...

    Need C programming help. I've started to work on the program, however I struggle when using files and pointers. Any help is appreciated as I am having a hard time comleting this code. #include <stdio.h> #include <stdlib.h> #include <string.h> #define MAX_LINE 100 #define MAX_NAME 30 int countLinesInFile(FILE* fPtr); int findPlayerByName(char** names, char* target, int size); int findMVP(int* goals, int* assists, int size); void printPlayers(int* goals, int* assists, char** names, int size); void allocateMemory(int** goals, int** assists, char*** names, int size);...

  • (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...

  • In C Program This program will store the roster and rating information for a soccer team. There w...

    In C Program This program will store the roster and rating information for a soccer team. There will be 3 pieces of information about each player: Name: string, 1-100 characters (nickname or first name only, NO SPACES) Jersey Number: integer, 1-99 (these must be unique) Rating: double, 0.0-100.0 You must create a struct called "playData" to hold all the information defined above for a single player. You must use an array of your structs to to store this information. You...

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

  • c++ The program will be recieved from the user as input name of an input file...

    c++ The program will be recieved from the user as input name of an input file and and output.file. then read in a list of name, id# and score from input file (inputFile.txt) and initilized the array of struct. find the student with the higher score and output the students info in the output file obtain the sum of all score and output the resurl in output file. prompt the user for a name to search for, when it find...

  • C++ Write a program that reads students’ names followed by their test scores from the given...

    C++ Write a program that reads students’ names followed by their test scores from the given input file. The program should output to a file, output.txt, each student’s name followed by the test scores and the relevant grade. Student data should be stored in a struct variable of type StudentType, which has four components: studentFName and studentLName of type string, testScore of type int and grade of type char. Suppose that the class has 20 students. Use an array of...

  • C++ program that reads students' names followed by their test scores (5 scores) from the given...

    C++ program that reads students' names followed by their test scores (5 scores) from the given input file, students.txt. The program should output to a file, output.txt, each student's first name, last name, followed by the test scores and the relevant grade. All data should be separated by a single space. Student data should be stored in a struct variable of type StudentType, which has four components: studentFName and studentLName of type string, an array of testScores of type int...

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

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