Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Deven Pace Project 4
- // start
- // Create structure for players name, jersey number, and points Scored
- // declare methods used in the program.
- // the program must hold 12 players, so the int size should be 12.
- // create array that holds player information.
- // create headings for program.
- // needs to show total points scored.
- // create funciton that calls for players information
- // if jersey or points scored is less than 0 make a loop to have user input a positive number
- // create display of players name, jersey number, and points scored (adjust the setw to look right)
- // use function to calculate total points and store in array to determine who scores the highest pointsScored
- // each player starts at 0 points, so make a algortithm to calculate total points
- // use funciton to show the highest points scored by a player. Needs to be stored into pointsScored playerInfoArr
- // print player who scored the most points.
- // end
- //notes
- // use a smaller number of players to test program. Remember to set size to 12 when completed.
- // must not accept negative values so write the for statement for the jersey number nand player score
- // remember to see why playerInfoArr[size] is not being called.
- // find the correct number for setw so program looks correct.
- // it doesnt show showHighest function. see why.
- #include <string>
- #include <iomanip>
- #include <iostream>
- using namespace std;
- //----------------------------------------------------------------------------//
- //player st ruct
- struct Player
- {
- string playerName; //String used for storing player name
- int playerJerseyNumber; //int to keep player's jersey number
- int pointsScored; //int to keep player's points scored
- };
- // dec methods
- void GetPlayerInfo(Player &); // Call function to gather player information
- void ShowPlayerInfo(const Player); // Call function to show player information
- int GetTotalPoints(const Player[], int); // Call function to find total points
- void ShowHighest(Player[], int); // Call function to show highest points
- //----------------------------------------------------------------------------//
- int main()
- {
- int size = 12; //Size is the int that will store how many players there will be in the array.
- Player playerInfoArr[size]; //creating array that will store player info
- for (int x = 0; x < size; x++)
- {
- GetPlayerInfo(playerInfoArr[x]); //call function to gather player information
- }
- cout << left << setw(20) << "Name" << setw(20) << "Jersey Number" << setw(20) << "Points Scored" << endl; //heading to show player name, jersey number, and points scored (mess with the size, it isnt right.)
- for (int x = 0; x < size; x++) //loop to show details about each player.
- {
- ShowPlayerInfo(playerInfoArr[x]); //calling the function to show the information of each player
- }
- int total = GetTotalPoints(playerInfoArr, size); //calling function to gather total points scored
- cout << "Total Points Scored: " << total << endl; //printing points scored.
- ShowHighest(playerInfoArr,size); //will call function to show which player had the highest score.
- system("pause");
- return 0;
- }
- //----------------------------------------------------------------------------//
- void GetPlayerInfo(Player &player) //Function to obtain player information
- {
- int number, points; //variables for jersey number and points scored
- // Entering player name
- cout << "Enter the players name: "; //Asking user for player name
- cin >> player.playerName; //Assigns a name into the string
- //Entering players jersey number.
- cout << "Enter players number: "; //Asking user for players number
- cin >> number; //input of players number
- while (number < 0) //User can't type a number lower than 0. This while loop assures that.
- {
- cout << "Must use a number equal or higher than zero!"; //Message if user inputs number less than 0
- cin >> number; //Users input
- }
- player.playerJerseyNumber = number; //Assigns player with jersey number
- //Entering player pointsScored
- cout << "Enter players points scored: "; //Asking user for player point scored
- cin >> points; //User input of points scored
- while (points < 0) //User can't type a number lower than 0.
- {
- cout << "Must use a number equal or higher than zero!";//Message if user inputs number less than 0
- cin >> points;// User input
- }
- player.pointsScored = points; //Assigns players points scored.
- }
- //----------------------------------------------------------------------------//
- void ShowPlayerInfo(const Player player) //Function to show player details.
- {
- cout << setw(20) << player.playerName << setw(20) << player.playerJerseyNumber << setw(20) << player.pointsScored << endl; //output of each players information //used setw to assure the display was easy to read
- }
- //----------------------------------------------------------------------------//
- int GetTotalPoints(const Player playerInfoArr[], int size) //function to calculate total points
- {
- int sum = 0; //each player will start out at 0, the integer will increase per points scored
- for(int x = 0; x < size; x++) //loop to add points to the total points scored
- {
- sum += playerInfoArr[x].pointsScored; //algorithm to store points into array
- }
- return sum; //returns total points scored
- }
- //----------------------------------------------------------------------------//
- void ShowHighest(Player playerInfoArr[], int size) //showing highest points scored
- {
- int playerPoints = playerInfoArr[0].pointsScored; //I set the points per player at 0, will increase if the player scores more than 0
- int playerPointsMax = 0; //will store player with highest points
- for(int x = 1; x < size; x++) //loop that determines who scores the most points
- {
- if(playerPoints < playerInfoArr[x].pointsScored)
- {
- playerPoints = playerInfoArr[x].pointsScored;
- playerPointsMax = x;
- }
- }
- Player highPoint = playerInfoArr[playerPointsMax]; //gets players points from maxpointint
- cout << "The player who scored the most points is: " << highPoint.playerName << endl; //printing the player with highest points
- cout << "They scored: " << highPoint.pointsScored << " total points." << endl; // printing total points scored
- }
- //----------------------------------------------------------------------------//
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement