
Concept explainers
The following is code for a disc golf program written in C++:
player.h:
#ifndef PLAYER_H
#define PLAYER_H
#include <string>
#include <iostream>
class Player {
private:
std::string courses[20]; // Array of course names
int scores[20]; // Array of scores
int gameCount; // Number of games played
public:
Player(); // Constructor
void CheckGame(const std::string& courseName, int gameScore);
void ReportPlayer(int playerId) const;
};
#endif // PLAYER_H
player.cpp:
#include "player.h"
#include <iomanip>
Player::Player() : gameCount(0) {}
void Player::CheckGame(const std::string& courseName, int gameScore) {
for (int i = 0; i < gameCount; ++i) {
if (courses[i] == courseName) {
// If course has been played, check for minimum score
if (gameScore < scores[i]) {
scores[i] = gameScore; // Update to new minimum score
}
return; // Exit after finding the course
}
}
// If course not found, add a new course and score
if (gameCount < 20) {
courses[gameCount] = courseName;
scores[gameCount] = gameScore;
++gameCount;
}
}
void Player::ReportPlayer(int playerId) const {
if (gameCount == 0) {
std::cout << "Player P" << playerId << " has no games\n";
} else {
std::cout << "Player P" << playerId << "\n";
for (int i = 0; i < gameCount; ++i) {
std::cout << "\t" << courses[i] << " " << scores[i] << "\n";
}
}
}
main.cpp:
#include <iostream>
#include <string>
#include <algorithm>
#include <limits>
#include "player.h"
int main() {
Player players[10]; // Array to hold players
std::string playerCode;
int inId, inScore;
std::string inCourse;
int maxPlayerIndex = -1; // Track the highest player ID
while (true) {
std::cout << "Enter player code or Q to quit: ";
std::cin >> playerCode;
// Check for quit input
if (playerCode == "q" || playerCode == "Q") break;
// Validate player code
if (playerCode.length() != 2 || playerCode[0] != 'P' || playerCode[1] < '0' || playerCode[1] > '9') {
std::cout << "Invalid player code. Please enter P0-P9.\n";
continue;
}
inId = playerCode[1] - '0'; // Extract player ID
if (inId < 0 || inId > 9) {
std::cerr << "Invalid player ID detected.\n";
continue;
}
maxPlayerIndex = std::max(maxPlayerIndex, inId); // Update the highest player ID
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
// Input course name
std::cout << "Enter course name: ";
std::getline(std::cin, inCourse);
// Input game score
std::cout << "Enter game score: ";
std::cin >> inScore;
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
std::cout << std::endl;
// Register the game for the player
players[inId].CheckGame(inCourse, inScore);
}
// Generate a report for all players
std::cout << "\nReport\n";
for (int i = 0; i <= maxPlayerIndex; ++i) {
players[i].ReportPlayer(i);
}
return 0;
}
Edit the program to allow both uppercase (as in P2) and lowercase (as in p2) inputs

Step by stepSolved in 2 steps

- In previous chapters, you have created programs for the Greenville Idol competition. Now create a Contestant class with the following characteristics: The Contestant class contains public static arrays that hold talent codes and descriptions. Recall that the talent categories are Singing Dancing, Musical instrument, and Other. The class contains an auto-implemented property that holds a contestants name. The class contains fields for a talent code and description. The set accessor for the code assigns a code only if it is valid. Otherwise, it assigns I for Invalid. The talent description is a read-only property that is assigned a value when the code is set. Modify the GreenvilleRevenue program so that it uses the Contestant class and performs the following tasks: The program prompts the user for the number of contestants in this years competition; the number must be between 0 and 30. The program continues to prompt the user until a valid value is entered. The expected revenue is calculated and displayed. The revenue is $25 per contestant. The program prompts the user for names and talent codes for each contestant entered. Along with the prompt for a talent code, display a list of the valid categories. After data entry is complete, the program displays the valid talent categories and then continuously prompts the user for talent codes and displays the names of all contestants in the category. Appropriate messages are displayed if the entered code is not a character or a valid code.arrow_forward(Data processing) A bank’s customer records are to be stored in a file and read into a set of arrays so that a customer’s record can be accessed randomly by account number. Create the file by entering five customer records, with each record consisting of an integer account number (starting with account number 1000), a first name (maximum of 10 characters), a last name (maximum of 15 characters), and a double-precision number for the account balance. After the file is created, write a C++ program that requests a user-input account number and displays the corresponding name and account balance from the file.arrow_forwardA(n) ____________ is one instance or variable of a class.arrow_forward
- Microsoft Visual C#Computer ScienceISBN:9781337102100Author:Joyce, Farrell.Publisher:Cengage Learning,C++ for Engineers and ScientistsComputer ScienceISBN:9781133187844Author:Bronson, Gary J.Publisher:Course Technology PtrEBK JAVA PROGRAMMINGComputer ScienceISBN:9781337671385Author:FARRELLPublisher:CENGAGE LEARNING - CONSIGNMENT
- Programming Logic & Design ComprehensiveComputer ScienceISBN:9781337669405Author:FARRELLPublisher:CengageC++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage LearningEBK JAVA PROGRAMMINGComputer ScienceISBN:9781305480537Author:FARRELLPublisher:CENGAGE LEARNING - CONSIGNMENT




