Can you help me with my code? C ++ Question: This program will store roster and
ID: 3857851 • Letter: C
Question
Can you help me with my code? C ++
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:
My current code:
#include <iostream>
// FIXME include any header files if you need them
using namespace std;
int main() {
/* Type your code here. */
return 0;
}
My Output vs Expected Output
Enter player 1's jersey number: Enter player 1's rating: Enter player 2"s jersey number: Enter player 2's rating: Enter player 3's jersey number: Enter player 3's rating: Enter player 4's jersey number: Enter player 4's rating: Enter player 5's jersey number: Enter player 5's rating: Your output starts with MENU a -Add player d -Delete player Update player rating r -Output players above a ating -Output roster QuitJ Choose an option Enter player 1's jersey number: Enter player 1's rating: Enter player 2"s jersey number: Enter player 2's rating: Enter player 3's jersey number: Enter player 3's rating: Enter player 4's jersey number: Enter player 4's rating: Expected output starts with Enter player 5's jersey number: Enter player 5's rating: ROSTER Rating: 4 ayer Gersey number: Player 2ersey number: 6, Rating: Player 3 -- Jersey number: 21, Rating: 5 Player 4ersey number:47, Rating: 8 Player S _ Jersey number: 83, Rating: 2Explanation / Answer
As per the question, the information are stored in two different arrays, thereby making the different operations more complex. You could use STL libraries and make use of pair and set to perform the operations efficiently.
Switch case has been used for the implementation of menu's. The code for the given problem is given below. I have tried to explain it using comments wherever possible.
CODE:
#include<bits/stdc++.h>
using namespace std;
int main(){
//jersey array to store jersey number and rating to store rating of player
int jersey[1000],rating[1000];
int array_size=5;//initialized size to 5
//Input of first 5 players information
for(int i=0;i<array_size;i++){
cout<<"Enter player "<< (i+1) <<"'s jersey number: ";
cin>>jersey[i];
cout<<"Enter player "<< (i+1) <<"'s rating: ";
cin>>rating[i];
}//for ends
//Output of the above array
cout<<"ROSTER"<<endl;
for(int i=0;i<array_size;i++){
cout<<"Player "<< (i+1) <<" -- Jersey number: "<<jersey[i]<<", Rating: "<<rating[i]<<endl;
}
char choice;
//while loop runs till choice 'q' is given as input
while(true){
cout<<"MENU a - Add player d - Delete player u - Update player rating r - Output players above a rating o - Output roster q - Quit Choose an option: "<<endl;
cin>>choice;//user choice input
if(choice=='q') //quit option in menu.
break;
int jno,rat,temp; //jno,rat is used to store jersey number,ratings temporarily for performing opertaions
switch(choice){
case 'o':
//Printing array
for(int i=0;i<array_size;i++){
cout<<"Player "<< (i+1) <<" -- Jersey number: "<<jersey[i]<<", Rating: "<<rating[i]<<endl;
}
break;
case 'a':
//adding a player at the end of array and increasing the size of array i.e array_size variable
cout<<"Enter another player's jersey number: ";
cin>>jersey[array_size];
cout<<"Enter another player's rating: ";
cin>>rating[array_size];
array_size++;
break;
case 'd':
//deleting the player and shifting the remaining to one position left
cout<<"Enter a jersey number: ";
cin>>jno;
for(int i=0;i<array_size;i++){//to find the position in array
if(jersey[i]==jno){
temp=i;
break;
}//if ends
}//for ends
for(int i=temp;i<array_size-1;i++){//shifting all the next values one position to the left
jersey[i]=jersey[i+1];
rating[i]=rating[i+1];
}
array_size--;// decrementing array size as one entry is deleted.
break;
case 'u':
//update operation
cout<<"Enter another player's jersey number: "<<endl;
cin>>jno;
cout<<"Enter another player's rating: "<<endl;
cin>>rat;
for(int i=0;i<array_size;i++){
if(jersey[i]==jno){ //checking for the jersey number position in array
rating[i]=rat;//updating rating as given
break;
}
}
break;
case 'r':
cout<<"Enter a rating: "<<endl;
cin>>rat;
cout<<"ABOVE "<<rat<<endl;
for(int i=0;i<array_size;i++){
if(rating[i]>rat){// printing wherever rating is above the given input
cout<<"Player "<<(i+1)<<" -- Jersey number: "<<jersey[i]<<", Rating: "<<rating[i]<<endl;
}
}
break;
}//switch ends
}//while ends
return 0;
}//main method ends