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

Just write the C++ code uand comments I will do the dowlnloading of the file so

ID: 3816648 • Letter: J

Question

Just write the C++ code uand comments I will do the dowlnloading of the file so dont worry

Q: Baseball is a sport with constantly changing statistics. Every time a player is at bat, their statistics change. Create a C++ program to control a team’s statistics. 1. Import the team's current statistics. To do this, create an array of structures that includes the player’s NUMBER, NAME, POSITION, AT BATS, BATTING AVERAGE, STRIKEOUTS, ON BASES, and HOMERUNS. Now import the Stats.txt file (DOWNLOAD THIS FILE and store it in your project folder) and write the file contents to the array of structures. The contents of each line of the file is written in the order of the stats above and are separated by a space. 2. Ask the user to enter the current player at bat. Have the user enter the player’s number and search the array to find the matching player of structures and output that player’s stats. NO INPUT VALIDATION IS NEEDED. 3. Ask the user to enter whether the player strikes out, has a base hit, hits a homerun, or gets out. (Assume they enter correctly and no WALKS or ERRORS.) Now adjust the player's stats. Batting average = (homeruns + on_base) / at_bats a. Strike out: at_bats += 1, strikeouts +=1 b. Base hits: at_bats += 1, on_base += 1 c. Homerun: at_bats += 1, homeruns += 1 d. Out: at_bats +=1 4. Finally, a baseball inning ends after three outs. Make a new file “UpdatedStats.txt” that contains the new states based on what has happened in the inning. Make sure your program can update AT BATS, BATTING AVERAGE, STRIKEOUTS, ON BASES, and HOMERUNS in the “UpdatedStats.txt” file. If the code fails to create or open a file for writing, have the program print an error message “Unable to pen file. New file has not been written.”

Sample Output:
Current player at bat (enter player's number): 23
Smith, #23 (C)
AT BATS: 322
BATTING AVERAGE: 0.469
STRIKEOUTS: 171
ON BASE: 95
HOMERUNS: 56
(1) STRIKEOUT (2) ON BASE (3) HOMERUN (4) OUT : 1
=== OUTS: 1 ===========================================
Current player at bat (enter player's number): 0
Holland, #0 (2B)
AT BATS: 266
BATTING AVERAGE: 0.211
STRIKEOUTS: 210
ON BASE: 54
HOMERUNS: 2
ECE114   Intro   to   C/C++   Programming
Spring   2017
2
(1) STRIKEOUT (2) ON BASE (3) HOMERUN (4) OUT : 2
=== OUTS: 1 ===========================================
Current player at bat (enter player's number): 43
Jackson, #43 (SS)
AT BATS: 287
BATTING AVERAGE: 0.192
STRIKEOUTS: 232
ON BASE: 32
HOMERUNS: 23
(1) STRIKEOUT (2) ON BASE (3) HOMERUN (4) OUT : 4
=== OUTS: 2 ===========================================
Current player at bat (enter player's number): 4
Kirk, #4 (P)
AT BATS: 122
BATTING AVERAGE: 0.205
STRIKEOUTS: 97
ON BASE: 19
HOMERUNS: 6
(1) STRIKEOUT (2) ON BASE (3) HOMERUN (4) OUT : 2
=== OUTS: 2 ===========================================
Current player at bat (enter player's number): 12
Jennings, #12 (P)
AT BATS: 143
BATTING AVERAGE: 0.469
STRIKEOUTS: 76
ON BASE: 44
HOMERUNS: 23
(1) STRIKEOUT (2) ON BASE (3) HOMERUN (4) OUT : 3
=== OUTS: 2 ===========================================
Current player at bat (enter player's number): 24
Player not found. Enter player's number: 8
Anderson, #8 (OF)
AT BATS: 333
BATTING AVERAGE: 0.375
STRIKEOUTS: 208
ON BASE: 102
HOMERUNS: 23
(1) STRIKEOUT (2) ON BASE (3) HOMERUN (4) OUT : 4
=== OUTS: 3 ===========================================
3 OUTS, INNING COMPLETED.
NOW ATTEMPTING TO CREATE NEW STATISTICS FILE
(Continued   on   next   page)
ECE114   Intro   to   C/C++   Programming
Spring   2017
3
For this run, here is what the UpdatedStats.txt should look like (changes bolded):
[changed do not need to be bolded in your file]
23 Smith C 323 0.467 172 95 56
45 Collins 3B 326 0.202 260 34 32
65 Rodgers OF 223 0.247 168 43 12
12 Jennings P 144 0.472 76 44 24
42 Flores P 195 0.503 97 66 32
17 Clausen 1B 244 0.594 99 102 43
6 Marks C 354 0.407 210 123 21
7 Hope SS 312 0.356 201 88 23
4 Kirk P 123 0.211 97 20 6
9 Cooper P 96 0.156 81 15 0
0 Holland 2B 267 0.213 210 55 2
43 Jackson SS 288 0.191 232 32 23
99 Reyes P 132 0.492 67 44 21
25 Dawson 3B 212 0.198 170 23 19
66 Walker P 121 0.289 86 34 1
11 Stein SS 223 0.462 120 102 1
21 Rivera OF 342 0.307 237 102 3
31 Harvey 1B 312 0.468 166 123 23
19 Williams OF 271 0.531 127 112 32
3 Jones 2B 332 0.497 167 132 33
1 Johnson OF 199 0.427 114 73 12
8 Anderson OF 334 0.374 208 102 23
68 Jacobson C 213 0.442 119 73 21
77 George OF 200 0.47 106 74 70
27 Carter 1B 288 0.385 177 49 10
***HINTS***
For importing the file:
ifstream inputFile;
inputFile.open("Stats.txt");
STATS * roster= new STATS [ROSTER_SIZE]; // STATS is the struct name.
For updating the stats:
Strikeout :
roster[cp].average=(double(roster[cp].on_bases)+
double(roster[cp].homeruns))/(double(roster[cp].at_bats + 1));
Base hit:
roster[cp].average=((double(roster[cp].on_bases + 1)+
double(roster[cp].homeruns))/(double(roster[cp].at_bats + 1));
Homerun:
roster[cp].average=(double(roster[cp].on_bases)+ double(roster[cp].homeruns +
1))/( double(roster[cp].at_bats + 1));
Out:
roster[cp].average=(double(roster[cp].on_bases)+double(roster[cp].homeruns))/(f
loat(roster[cp].at_bats + 1));
For creating the output file:
ofstream newStats("UpdatedStats.txt");
Setting precision of 3 decimals in output file:
myFile << std::setprecision(3) << ...

Explanation / Answer

//note you have to change size of roster_size to number of players in stats.txt file

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

using namespace std;

struct STATS
{
   int number;
   string name;
   string pos;
   double at_bats;
   double average;
   int strikeouts;
   double on_bases;
   int homeruns;
};


int main() //start of our program
{
   int ROSTER_SIZE = 25;
   ifstream inputFile;
   inputFile.open("Stats.txt");
   STATS * roster = new STATS[ROSTER_SIZE]; // STATS is the struct name.
   int cp = 0;
   STATS s;
   int num;
   int option = 0;
   int outs = 0;
  
   if (!inputFile.is_open())
   {
       cout << "error : unable to open a file";
       return 0;
   }
   while (inputFile>>s.number>>s.name>>s.pos>>s.at_bats>>s.average>>s.strikeouts>>s.on_bases>>s.homeruns)
   {

       roster[cp] = s;
       STATS s;
       cp++;
   }

   while (outs != 3)
   {
       cout << "enter cuurent player at bat " << endl;
       cin >> num;
       cin.ignore();
       for (int i = 0; i < ROSTER_SIZE; i++)
       {
           if (roster[i].number == num)
           {
               cp = i;
           }
       }

       cout << roster[cp].name << " # " << roster[cp].number << endl;
       cout << "AT BATS: ";
       cin >> roster[cp].at_bats;
       cin.ignore();
       cout << " ";
       cout << "BATTING AVERAGE ";
       cin >> roster[cp].average;
       cin.ignore();
       cout << "STRIKEOUTS : ";
       cin >> roster[cp].strikeouts;
       cin.ignore();

       cout << "   ON BASE : ";
       cin >> roster[cp].on_bases;
       cin.ignore();

       cout << "   HOMERUNS : ";
       cin >> roster[cp].homeruns;
       cin.ignore();

       cout << "   (1) STRIKEOUT(2) ON BASE(3) HOMERUN(4) OUT : ";
       cin >> option;
       cin.ignore();

       switch (option) {
       case 1:
           roster[cp].average = (double(roster[cp].on_bases) +
               double(roster[cp].homeruns)) / (double(roster[cp].at_bats + 1));
           outs++;
           break;
       case 2:
           roster[cp].average = ((double(roster[cp].on_bases + 1) +
               double(roster[cp].homeruns)) / (double(roster[cp].at_bats + 1)));
           break;
       case 3:
           roster[cp].average = (double(roster[cp].on_bases) + double(roster[cp].homeruns +
               1)) / (double(roster[cp].at_bats + 1));
           break;
       case 4:
           roster[cp].average = (double(roster[cp].on_bases) + double(roster[cp].homeruns)) / (float(roster[cp].at_bats + 1));
           outs++;
           break;

       default:
           cout << "Invalid grade" << endl;
       }

   }


   ofstream newStats("UpdatedStats.txt");
      
   if (!newStats.is_open())
   {
       cout << "enable to open updatestast.txt";
       return 0;
   }
   std::setprecision(3);

       for (int i = 0; i < ROSTER_SIZE; i++)
       {
           newStats << roster[i].number<<" "<<roster[i].name << " " << roster[i].pos << " " << roster[i].at_bats << " " << roster[i].average << " " << roster[i].strikeouts << " " << roster[i].on_bases << " " << roster[i].homeruns << " " << endl;

       }
       inputFile.close();
       newStats.close();


   return 0;
}