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

C++ Program include only 2 headers: #include <iostream> #include <vector> This p

ID: 3605683 • Letter: C

Question

C++ Program

include only 2 headers:

#include <iostream>

#include <vector>

This program will store roster and rating information for a soccer team. Coaches rate players during tryouts to ensure a balanced team. Create a struct to store the information on one player: an int for the player's jersey number (0 - 99) and an int for the player's rating (1 - 9). Create a vector of these structs to hold all of the players. You can initialize it with size 0.

(1) Implement a menu of options for a user to modify the roster. Each option is represented by a single character. The program initially outputs the menu, and outputs the menu after a user chooses an option. The program ends when the user chooses the option to Quit. For this step, the other options do nothing. (1 pt)

Ex:

(2) Implement the "Add player" menu option. Prompt the user for a new player's jersey number and rating. Declare a struct for this player and append it to the end of the roster vector. Use push_back() (section 8.6). (2 pt)

Ex:

(3) Implement the "Output roster" menu option. (1 pt)

Ex:

(4) Implement the "Delete player" menu option. Prompt the user for a player's jersey number. Remove the player from the roster (delete the jersey number and rating). There are a couple of ways you can delete an element from a vector. You can use a loop, starting at the index where you want to delete, copy every element after that to the left by one, and then use pop_back() to remove the last element. An easier way to do this is to call the erase function. To delete an element at index i: myvector.erase (myvector.begin()+i);. (1 pts)

Ex:

(5) Implement the "Update player rating" menu option. Prompt the user for a player's jersey number. Prompt again for a new rating for the player, and then change that player's rating. (1 pt)

Ex:

(6) Implement the "Output players above a rating" menu option. Prompt the user for a rating. Print the jersey number and rating for all players with ratings above the entered value. (1 pts)

Ex:

(6) Implement the "Sort by player rating" menu option. You do not have to output anything after sorting, the tester will verify successful sorting by outputting the roster. (1 pt)

(7) Implement the "Sort by jersey number" menu option. You do not have to output anything after sorting, the tester will verify successful sorting by outputting the roster. (1 pt)

There are many different ways you can implement the sorting. You can modify either of the sorting algorithms shown in chapter 7 to compare an element of your struct when it does its comparison, and make sure the data types of any swapping variables are structs, so it swaps the entire struct. I will show you how to do this in lecture.

There is an easier and cooler way to sort your vector using code that exists in standard library files. I will leave it up to you as a challenge if you want to pursue this.

Explanation / Answer

// Header file section

#include <iostream>

#include <vector>

using namespace std;

// Program starts with the main function

int main()

{

     // Declare vectors

     vector<int> jerseyNumber;

     vector<int> rating;

     // Declare variable

     int temp;

     // This loop reads the jersey number

     // and player rating until

     // end of the loop

     for (int i = 1; i <= 5; i++)

     {

          // Prompt and read the jersey number

          // from the user

          cout << "Enter player " << i

              << "'s jersey number: ";

          cin >> temp;

          // push_back() method is used to

          // put elements into vector

          jerseyNumber.push_back(temp);

          // Prompt and read the player rating

          // from the user

          cout << "Enter player " << i

              << "'s rating: ";

          cin >> temp;

          // push_back() method is used to

          // put elements into vector

          rating.push_back(temp);

          cout << endl;

     }

     // Print roster

     cout << "ROSTER" << endl;

     // This loop display all elements

     // in the vector

     for (int i = 0; i < 5; i++)

          // Display jersey number and player rating

          cout << "Player " << i + 1 << " -- "

          << "Jersey number: " << jerseyNumber.at(i)

          << ", Rating: " << rating.at(i) << endl;

     // Declare variable

     char option;

     // This loop executes all the

     // implementations until user

     // press the 'q'

     while (true)

     {

          // Display MENU

          cout << "MENU" << endl;

          cout << "a - Add player" << endl;

          cout << "d - Remove player" << endl;

          cout << "u - Update player rating" << endl;

          cout << "r - Output players above a rating"

              << endl;

          cout << "o - Output roster" << endl;

          cout << "q - Quit" << endl << endl;

          // Prompt and read option from

          // the user

          cout << "Choose an option: ";

          cin >> option;

          // Switch statement matches all

          // the case labels

          switch (option)

          {

              // Check option is equal to

              // 'a' or 'A'

          case 'a':

          case 'A':

              // Prompt and read the jersey number

              // from the user

              cout << "Enter a new player's"

                   << "jersey number: ";

              cin >> temp;

              // push_back() method is used to

              // put elements into vector

              jerseyNumber.push_back(temp);

              // Prompt and read the player rating

              // from the user

              cout << "Enter the player's rating: ";

              cin >> temp;

              // push_back() method is used to

              // put elements into vector

              rating.push_back(temp);

              break;

              // Check option is equal to

              // 'd' or 'D'

          case 'd':

          case 'D':

              // Prompt and read the jersey number

              // from the user

              cout << "Enter a jersey number: ";

              cin >> temp;

              int i;

              // This loop checks the jersey number

              // with user element and remove the

              // corresponding jersey number in the

              // the vector

              for (i = 0; i < jerseyNumber.size();

              i++)

              {

                   // Check jersey number with

                   // user input

                   if (jerseyNumber.at(i) == temp)

                   {

                        jerseyNumber.erase(

                             jerseyNumber.begin() + i);

                        // erase() method is used to

                        // remove element from the

                        // vector

                        rating.erase(rating.begin() + i);

                        break;

                   }

              }

              break;

              // Check option is equal to

              // 'u' or 'U'

          case 'u':

          case 'U':

              // Prompt and read the jersey number

              // from the user

              cout << "Enter a jersey number: ";

              cin >> temp;

              // This loop checks the jersey number

              // with user element and update the

              // corresponding jersey number in the

              // vector

              for (int i = 0; i < jerseyNumber.size();

              i++)

              {

                   // Check jersey number with

                   // user input

                   if (jerseyNumber.at(i) == temp)

                   {

                        cout << "Enter a new rating "

                             << "for player: ";

                        cin >> temp;

                        rating.at(i) = temp;

                        break;

                   }

              }

              break;

              // Check option is equal to

              // 'r' or 'R'

          case 'r':

          case 'R':

              // Prompt and read the player rating

              // from the user

              cout << "Enter a rating: ";

              cin >> temp;

              cout << " ABOVE " << temp << endl;

              // This loop displays the jersey numbers

              // and player ratings above the user

              // input rating

              for (int i = 0; i < jerseyNumber.size();

              i++)

                   if (rating.at(i) > temp)

                        cout << "Player " << i + 1

                        << " -- "

                        << "Jersey number: "

                        << jerseyNumber.at(i)

                        << ", Rating: "

                        << rating.at(i) << endl;

              break;

              // Check option is equal to

              // 'o' or 'O'

          case 'o':

          case 'O':

              // This loop display all elements

              // in the vector

              cout << "ROSTER" << endl;

              for (int i = 0; i < jerseyNumber.size();

              i++)

                   // Display jersey number and

                   // player rating

                   cout << "Player " << i + 1 << " -- "

                   << "Jersey number: "

                   << jerseyNumber.at(i) << ", Rating: "

                   << rating.at(i) << endl;

              break;

              // Check option is equal to

              // 'q' or 'Q'

          case 'q':

              return 0;

              // Display a default message

          default: cout << "Invalid menu option."

              << " Try again." << endl;

          }

     }

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote