C++ code help Question: This program will store roster and rating information fo
ID: 3860091 • Letter: C
Question
C++ code help
Question:
This program will store roster and rating information for a soccer team. Coaches rate players during tryouts to ensure a balanced team.
(1) Prompt the user to input five pairs of numbers: A player's jersey number (0 - 99) and the player's rating (1 - 9). Store the jersey numbers in one int array and the ratings in another int array. Output these array(i.e., output the roster). (3 pts)
Ex:
(2) 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. (2 pt)
Ex:
(3) Implement the "Output roster" menu option. (1 pt)
Ex:
(4) Implement the "Add player" menu option. Prompt the user for a new player's jersey number and rating. Append the values to the two arrays. (1 pt)
Ex:
(5) 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). (2 pts)
Ex:
(6) 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:
(7) 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. (2 pts)
Ex:
Default Code:
#include <iostream>
// FIXME include any header files if you need them
using namespace std;
int main() {
/* Type your code here. */
return 0;
}
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;
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.