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

General Description: Write an application to maintain data for a Golf Club in C+

ID: 3815455 • Letter: G

Question

General Description:

Write an application to maintain data for a Golf Club in C++. The club consists of a list of (up to 10) players, where each player has a list of (up to 10) games played, and each game has a list of (up to 18) scores, one score per hole of golf. The application should allow the user to add and delete players, and add game scores for each player.

It should calculate the total score on each game according to the following club rules:

- Total Score is normally just the sum of the scores for each hole. But since a game may have

fewer than 18 holes, to “normalize” scores for games with fewer than 18 holes, the club uses the following formula to calculate the Total Score:

(sum of hole scores) + ((18 – number_holes_played) X Club Hole Average)
The Club Hole Average is a number set by the club, and can be changed by the club any time they wish. It is usually around 4 or 5.

The application should also calculate and maintain each player’s Average Score (a simple average of the Total Score for all games played). Finally, the application should read the current club data from a file on start up, and save the game data to a file on exit.

On start up:

o Initialize all club data (strings to “” and numbers to 0).
o Load data from a file, or allow the user to start a new (empty) club.

Ask the user to enter the input data file name or new to start a new club.
If the file name entered is new, start a new club
If data file fails to open, start a new club
If the data file opens, read the data from the file using the File Description below. Tip: keep the data file(s) in the same directory as your .cpp file for easy entry.

o To start a new club, ask the user to enter the club’s name and hole average. Store these in the club’s record.

- Main Menu: repeat until the user selects Exit (7). Validate the user’s input is between 1 and 7. 1. Add Player

- Add Player:
- If there are already 10 players, the club is full. Print an error message.
- Initialize the next available player record.
- Ask the user to enter the player’s name and store it in the Player record.

- Drop Player:
- Display a brief list of all player’s names.
- Ask the user to enter a player’s name.
- Search the player list to find the player’s name:
- If found, delete the player from the list and report success (Player dropped.)

If not found, report failure (print “Player not found.”)

- Add Game Data for Player

- Display a brief list of all player’s names.
- Ask the user to enter a player’s name.
- Search the player list to find the player’s name:
- If not found, print a not found error and do not add a new game. - If found:

- If this player already has 10 games, print an error message and do not add a new game.

- Ask the user to enter the course name, date played and number of holes played. Validate the number of holes is between 1 and 18.

- Ask the user to enter the scores for each hole as a sequence of numbers
on a single line. You may assume the user will enter the correct number of integers (tip: use a loop that reads one integer at a time, without a prompt).

- Calculate and print the total score for the game.

Sort Players by Average:
Sorts the Club’s player list by their average score. Prints “Players sorted by Average” when complete.

- Sort Players by Name:
Sorts the Club’s player list by their Name. Prints “Players sorted by Name” when complete.

- Exit:

- Asks the user to enter a file name or none if they do not want to save.

- If the user enters none, do not write the data to a file, just exit.

- If the user enters something other than none:

- If the file fails to open, print an error message and do not save the file.
- If the file opens, writes the data to the file according to the File Description below. - Exits the application.

Clear Screen and Pause:

Before each operation: clear the screen ( system(”cls”); )and print the logo. After each operation: pause ( system(”pause”); ).

File Description:

- Line 1: contains the Number of Players, Club Hole Average, and Club’s name. The name may have spaces. This is followed by data for each player.

- For each player:
- First Line: contains the Number of Games played and the player’s name. The name may have spaces.

This is followed by data for each game played: - For each Game:

- First Line: contains the Number of Holes played, the Date (no spaces) and the Course Name (spaces) - Second Line: a sequence of numbers with spaces between. These are the scores for each hole

played. So if there were 9 holes played, there will be 9 values.

GUI is not required. Here is the outline I've been given.

Explanation / Answer

//----------------------------------------------------------------------------
// Golf Club App
//----------------------------------------------------------------------------
// Name: YOUR NAME HERE
// Section:
// Date:
// An application to manage data for a Golf Club, including:
// - individual scores on a single hole of golf
// - total scores for a game
// - a list of games for one player
// - a list of players for the whole club
//----------------------------------------------------------------------------
#include <bits/stdc++.h>
using namespace std;

const int MAX_PLAYERS = 10; // max number of players in a club
const int MAX_HOLES = 18; // max number of holes in a game
const int MAX_GAMES = 10; // max number of games for a player
const int NOT_FOUND = -1; // return code for search
const int EXIT_OPTION = 7; // main menu option for EXIT


int fl;

// WRITE a structure for aGame. Members:
// course - string - name of golf course
// date - string - date game was played
// totalScore - integer - total score for the game
// hole - array of ints - list of scores for each hole
// numHoles - integers - number of holes played

struct aGame {
string course;
string date;
int totalScore;
int hole[MAX_HOLES];
int numHoles;
};

struct aPlayer {
string name; // name of player
float avgScore; // player's average score on all games
int numGames; // number of games this player has played
aGame game[MAX_GAMES]; // list of games played by this player
};

struct aClub {
string name; // name of the club
int numPlayers; // number of players in the club
int holeAvg; // used to calculate total score for players
aPlayer player[MAX_PLAYERS];// list of players in this club
};

//----------------------------------------------------------------
// printLogo
//----------------------------------------------------------------
// design a new logo if you wish (include your Name and Section)
void printLogo() {
system("cls");
cout << "+------------------------------------------------+ ";
cout << "| GOLB CLUB EXPRESS PRO | ";
cout << "| by | ";
cout << "+------------------------------------------------+ ";
} // printLogo()


//-----------------------------------------------------------------------------
// initGame
//-----------------------------------------------------------------------------
void initGame(aGame & g) {
string name,date;
int total,val,hole;
int i;

getline(cin,name);
getline(cin,date);

cin >> hole;
if (!(hole >= 1 && hole <= 18)) {
cout << "The number of holes exceed the Maximum limit. The game can't be added ";
fl = 1;
return;
}

g.course = name;
g.date = date;
g.numHoles = hole;

total = 0;
for (i = 0; i < hole; i++) {
cin >> val;
g.hole[i] = val;
total = total + val;
}

g.totalScore = total;
} // initGame()

//-----------------------------------------------------------------------------
// initPlayer
//-----------------------------------------------------------------------------
void initPlayer(aPlayer & p) {
string name;
fflush(stdin);
getline(cin,name);

p.name = name;
p.avgScore = 0;
p.numGames = 0;
} // initPlayer()

//-----------------------------------------------------------------------------
// initClub
//-----------------------------------------------------------------------------
void initClub(aClub & c) {
string cName;
int cHoleAvg;
fflush(stdin);
cout << "Enter the Club's Name:-";

getline(cin,cName);
cout << "Enter the Club's Hole Average ";
cin >> cHoleAvg;
c.holeAvg = cHoleAvg;
c.name = cName;
return;
} // initClub()

//-----------------------------------------------------------------------------
// startUp
//-----------------------------------------------------------------------------
// Modifies: a Club structure
// Initializes the the club to "empty", then reads data from a file or
// starts a new club.
//-----------------------------------------------------------------------------
void startUp(aClub & c) {
string file,cName;
int cHoleAvg;
FILE *fp;

cout << "Enter the file name to load with the file format or enter 'none' without quotes if you want to start a new club. ";
cin >> file;


ifstream ifs(file.c_str());

   int numPlayer, holeAvg, numGames, numHoles, val;
   string name, date, course;

   if(ifs.is_open()) {
cout << "fdlflf ";
       ifs >> numPlayer >> holeAvg;
       getline(ifs, name);

c.holeAvg = holeAvg;
c.numPlayers = numPlayer;
c.name = name;

       for(int i = 0; i < numPlayer; i++) {
       ifs >> numGames;
           getline(ifs, name);
           c.player[i].numGames = numGames;
           c.player[i].name = name;
       for(int j = 0; j < numGames; j++) {
           ifs >> numHoles >> date;
           getline(ifs, course);
           c.player[i].game[j].course = course;
           c.player[i].game[j].numHoles = numHoles;
           c.player[i].game[j].date = date;
           for(int k = 0; k < numHoles; k++) {
               ifs >> val;
               c.player[i].game[j].hole[k] = val;
           }
       }
       }
   } else {
initClub(c);
   }

return;
} // startUp()


//-----------------------------------------------------------------------------
// printClub
//-----------------------------------------------------------------------------
void printClub(aClub & c) {
cout << c.name << endl;
cout << "Number of Players:- " << c.numPlayers << endl;
cout << "Club Hole Avg" << c.holeAvg << endl;
} // printClub()

//-----------------------------------------------------------------------------
// addPlayer
//-----------------------------------------------------------------------------
// Modifies: a club structure
// If the list of players is not full, initializes the next available element
// in the club's player list and asks the user for the new player's name.
//-----------------------------------------------------------------------------
void addPlayer(aClub & c) {
cout << "ffdfd " << c.numPlayers << endl;
if (c.numPlayers == MAX_PLAYERS) {
cout << "The Club is full.";
return;
}
aPlayer player;
initPlayer(player);
c.player[c.numPlayers] = player;
c.numPlayers++;
} // addPlayer()

//-----------------------------------------------------------------------------
// dropPlayer
//-----------------------------------------------------------------------------
// Modifies: a club structure
// Allows the user to select a player for deletion from the club's Player list
// and deletes it.
//-----------------------------------------------------------------------------
void dropPlayer(aClub & c) {
string name;
int i,j;
int flag;

for (i = 0; i < c.numPlayers; i++) {
cout << c.player[i].name << endl;
}

cout << "Enter the player's name to be dropped. ";
getline(cin,name);

flag = 0;
for (i = 0; i < c.numPlayers; i++) {
if (c.player[i].name == name) {
flag = 1;
int temp_i;
temp_i = i;
for (j = i+1; j < c.numPlayers; j++) {
c.player[temp_i] = c.player[j];
temp_i++;
}
break;
}
}

if (!flag) {
cout << "Player not found. ";
} else {
cout << "Player dropped. ";
}
} // dropPlayer()

//-----------------------------------------------------------------------------
// addGame
//-----------------------------------------------------------------------------
// Modifies: a club structure
// Adds game data for a new game to a player in the club
//-----------------------------------------------------------------------------
void addGame(aClub & c) {
int i,flag,flag1,j;
int gamescore;
string name;
float avg;

for (i = 0; i < c.numPlayers; i++) {
cout << c.player[i].name << endl;
}
getline(cin,name);

flag = 0;
flag1 = 0;
for (i = 0; i < c.numPlayers; i++) {
if (c.player[i].name == name) {
flag = 1;
if (c.player[i].numGames != MAX_GAMES) {
flag1 = 1;
aGame game;
fl = 0;
initGame(game);
if (fl == 1) {
break;
}
game.totalScore = game.totalScore + ((18-game.numHoles)*c.holeAvg);
c.player[i].game[c.player[i].numGames] = game;

avg = 0.0;
for (j = 0; j < c.player[i].numGames; j++) {
avg = avg + c.player[i].game[j].totalScore;
}
avg = avg/(1.0*c.player[i].numGames);
c.player[i].avgScore = avg;
///avg score
gamescore = game.totalScore;
c.player[i].numGames++;
}
}
}

if (!flag) {
cout << "Player not found ";
} else {
if (!flag1) {
cout << "A new game can't be added for the player ";
} else {
if (fl == 0)
cout << "Total score of the game is:- " << gamescore << endl;
}
}
} // addGame()

//-----------------------------------------------------------------------------
// sortByName
//-----------------------------------------------------------------------------
// Modifies: a club structure
// Re-orders the club's list of players alphabetically by the players' names
//-----------------------------------------------------------------------------

int cmpname(aPlayer a, aPlayer b)
{
return a.name < b.name;
}

void sortByName(aClub & c) {
sort(c.player,c.player+c.numPlayers);
cout << "Players sorted by Name. ";
} // sortByName()

//-----------------------------------------------------------------------------
// sortByAvg
//-----------------------------------------------------------------------------
// Modifies: a club structure
// Re-orders the club's list of players by their Average Score, high to low
//-----------------------------------------------------------------------------

int cmpavg(aPlayer a, aPlayer b)
{
if (a.avgScore == b.avgScore) {
return a.name < b.name;
}
return a.avgScore < b.avgScore;
}

void sortByAvg(aClub & c) {
sort(c.player,c.player+c.numPlayers,cmpavg);
cout << "Players sorted by Average. ";
} // sortByAvg()

//-----------------------------------------------------------------------------
// saveData
//-----------------------------------------------------------------------------
// Given: a club structure
// Asks the user for an output file name and, if successfully opened, writes
// all club data to the file.
//-----------------------------------------------------------------------------
void saveData(aClub & c) {
string req;

cout << "Enter a filename with extension to save data into file or 'none' without quotes if you don't want to save data to file ";
cin >> req;

if (req == "none") {
return;
}
freopen(req.c_str(),"w",stdout);
cout << c.numPlayers << " " << c.holeAvg << " " << c.name << endl;
for (int i = 0; i < c.numPlayers; i++) {
cout << c.player[i].numGames << " " << c.player[i].name << endl;
for (int j = 0; j < c.player[i].numGames; j++) {
cout << c.player[i].game[j].numHoles << " " << c.player[i].game[j].date << " " << c.player[i].game[j].course << endl;
for (int k = 0; k < c.player[i].game[j].numHoles; k++) {
cout << c.player[i].game[j].hole[k] << " ";
} cout << endl;
}
}
} // saveData()

//-----------------------------------------------------------------------------
// getMenuOption
//-----------------------------------------------------------------------------
// Returns: the valid menu option chosen by the user
// Displays the main menu and gets the user's option
//----------------------------------------------------------------------------
int getMenuOption() {
int option = EXIT_OPTION;
printLogo();
cout << "1. Add Player ";
cout << "2. Drop Player ";
cout << "3. Add Game Data for Player ";
cout << "4. Print Club Report ";
cout << "5. Sort Players by Average ";
cout << "6. Sort Players by Name ";
cout << EXIT_OPTION << ". Exit ";

// ADD CODE: to ask and validate the option

return option;
} // getMenuOption()

//-----------------------------------------------------------------------------
// main
//-----------------------------------------------------------------------------
void main() {
aClub club;
startUp(club);

int option = 0;
while (option != EXIT_OPTION) {
system("cls");
option = getMenuOption();
switch (option) {
case 1: addPlayer(club); break;
case 2: dropPlayer(club); break;
case 3: addGame(club); break;
case 4: printClub(club); break;
// case 5: sortByAvg(club); break;
// case 6: sortByName(club); break;
case EXIT_OPTION: saveData(club);
}
system("pause");
}

} // main()