Soccer Roster with Functions

Hello, I am very new at programming and a few of my functions aren't doing what they should be. I put the problem I'm having right before the function. I'm very lost at this point. We aren't allowed to use vectors or pointer, this is a very basic, beginner class. Any amount of help would be great!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#include <iostream>
#include <string>
#include <iomanip>   // so support the use of std::setw() and std::left

using namespace std;


int setInitialRoster(string[], int[], int[]);
void printRoster(string[], int[], int[], int);
void addPlayer(string[], int[], int[], int);
void deletePlayer(string[], int[], int[], int);
void updateRating(int[], int[], int);
void showHigherRatings(string[], int[], int[], int);


int main() {
   int MAX_PLAYERS = 10;
   int numPlayers = 0;
   
   string playerName[MAX_PLAYERS];
   int playerNum[MAX_PLAYERS];
   int playerRating[MAX_PLAYERS];
   
   numPlayers = setInitialRoster(playerName, playerNum, playerRating);
  

   char menuOption = ' ';
   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" << std::endl << endl;
   cout << "Choose an option:" << endl << endl;
   cin >> menuOption;
   
   do {
      
      if(menuOption == 'a') {
         addPlayer(playerName, playerNum, playerRating, numPlayers);
      }
      else if(menuOption == 'd')  {
         deletePlayer(playerName, playerNum, playerRating, numPlayers);
         }
      else if(menuOption == 'u')  {
         updateRating(playerNum, playerRating, numPlayers);
         }
      else if(menuOption == 'r')  {
         showHigherRatings(playerName, playerNum, playerRating, numPlayers);
         }
      else if(menuOption == 'o')  {
          printRoster(playerName, playerNum, playerRating, numPlayers);
         }
   cin >> menuOption;
   } while(menuOption != 'q');
   

   return 0;
}

//Set initial roster
int setInitialRoster(string names[], int numbers[], int ratings[])  {
   int numToAdd = 0;
   cout << "Enter number of players to add: " << endl <<endl;
   cin >> numToAdd;
   
   for(int i = 0; i < numToAdd; i++)   {
      cout << "Enter player's name: " <<endl;
      cin >> names[i];
      cout << "Enter " << names[i] << "'s jersey number: " << endl;
      cin >> numbers[i];
      cout << "Enter " << names[i] << "'s rating: " << endl << endl;
      cin >> ratings[i];
   }
   cout << endl;
   printRoster(names, numbers, ratings, numToAdd);
   return numToAdd;
}

//Print roster
void printRoster(string names[], int numbers[], int ratings[], int numToAdd) {
   
   cout << "ROSTER:" << endl;
   cout << left << setfill(' ')
        << setw(10) << "Player"
        << setw(10) << "Jersey #"
        << setw(10) << "Rating" << endl;
   for(int i = 0; i < numToAdd; i++)   {
      cout << left << setfill(' ')
           << setw(10) << names[i]
           << setw(10) << numbers[i]
           << setw(10) << ratings[i] << endl; 
   }
   cout << endl;
}

//Add new player - does not add player
void addPlayer(string names[], int numbers[], int ratings[], int numToAdd)  {
   string newName;
   int newNum;
   int newRating;
   numToAdd++;
   cout << "Enter a new player's name:" << endl;
   cin >> newName;
   names[numToAdd] += newName;
   cout << "Enter another player's jersey number:" << endl;
   cin >> newNum;
   numbers[numToAdd] += numbers[newNum];
   cout << "Enter another player's rating:" << endl << endl;
   cin >> newRating;
   ratings[numToAdd] += ratings[newRating];
}

//Delete Player - does not delete player
void deletePlayer(string names[], int numbers[], int ratings[], int numToAdd) {
   int jerseyNum, i, j, playerDelete;
   int tempj[numToAdd];
   int tempr[numToAdd];
   cout << "Enter a jersey number:" << std::endl;
            cin >> jerseyNum;
            for(i = 0; i < numToAdd; i++) {
               if(numbers[i] == jerseyNum) {
                  playerDelete = i;
               }
            }
            for(i = 0; i < numToAdd; i++)   {
                  tempj[i] = numbers[i];
                  tempr[i] = ratings[i];
            }
            for(i = 0; i < numToAdd; i++)  {
               if(numbers[i] != numbers[playerDelete]) {
                  numbers[j] = tempj[i];
                  ratings[j] = tempr[i];
                  j++;
               }
            }
            --numToAdd;      
   }

//Update player rating
void updateRating(int numbers[], int ratings[], int numToAdd)   {
   int jerseyNum, newRatingNum, i;
   cout << "Enter a jersey number:" << endl;
   cin >> jerseyNum;
   cout << "Enter a new rating for player:" << endl;
   for(i = 0; i < numToAdd; i++)  {
      if(numbers[i] == jerseyNum)  {
         cin >> newRatingNum;
         ratings[i] = newRatingNum;
         }
      }
   }

//Show players higher than certain rating - outputs whole roster
void showHigherRatings(string names[], int numbers[], int ratings[], int numToAdd)   {
   int ratingNum, i;
   cout << "Enter a rating:" << endl;
   cin >> ratingNum;
   cout << "ABOVE " << ratingNum << endl;
   for(i = 0; i < numToAdd; i++)  {
      if(ratings[i] > ratingNum)  {
         cout << "Player " << i + 1 << " -- Jersey number: " << numbers[i] << ", Rating: " << ratings[i] << endl;
            }
         }
   }
Last edited on
names[numToAdd] += newName;
You don't add newName to the array but to the last names element
Also numToAdd should be a reference otherwise the changes will be lost.
This is a beginner project. I find primitive, simple solutions work very well for this kind of stuff.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include<string>
using namespace std;

int main() {

	int a[5] = {NULL}; //start your array with NULL values

	//ADD useful values to some elements
	a[0] = 1;
	a[1] = 2;
	a[2] = 3;

	
	//PRINT THE ARRAY (only the elements with useful values)
	for (int i = 0; i < 5; i++) {
		if (a[i] != NULL) {
			cout << a[i] << " ";
		}
	}

	cout << endl;

	//delete a value by putting it back to NULL
	a[1] = NULL;

	//PRINT THE ARRAY (only the elements with useful values)
	for (int i = 0; i < 5; i++) {
		if (a[i] != NULL) {
			cout << a[i] << " ";
		}
	}

	//Add a new value to the array
	for (int i = 0; i < 5; i++) {
		if(a[i] == NULL) {
			a[i] = 7;
			break;
		}
	}

	cout << endl;

	//PRINT THE ARRAY (only the elements with useful values)
	for (int i = 0; i < 5; i++) {
		if (a[i] != NULL) {
			cout << a[i] << " ";
		}
	}
	cout << endl;

	system("pause");
	return 0;
}
I'm sure the menu and user input stuff works fine, but better to not distract yourself from checking the logic of the methods. I suggest globalizing all the repeated arrays until you can encapsulate it all in a class, and printing out the Index in the roster.

- Can add by keeping track of next available index
- Can delete from a specified index. Carefully shift necessary things to the left after deletion, and decrement available index:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include <iostream>
#include <iomanip>
#include <string>

using namespace std;

const int MAX_PLAYERS = 10;  // Max number allowed in roster
string names[MAX_PLAYERS]
{
    "----", "----", "----", "----", "----",
    "----", "----", "----", "----", "----"
};
int numbers[MAX_PLAYERS] { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 };
int ratings[MAX_PLAYERS] { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 };
int avail_index = 0;  // Next open index in roster, used in adding/removing

//Print roster
void PrintRoster() 
{
    cout << endl << endl;
    cout << left <<
            setw(7) << "Index" <<
            setw(10) << "Player" <<
            setw(10) << "Jersey#" <<
            setw(10) << "Rating" << endl;
    for(int i = 0; i < MAX_PLAYERS; i++)
    {
        cout << left << 
                setw(7) << i <<
                setw(10) << names[i] << 
                setw(10) << numbers[i] <<
                setw(10) << ratings[i] << endl; 
    }
    cout << endl;
}

// Add a player to the next open spot
void AddPlayer(string name, int number, int rating)
{
    if (avail_index == MAX_PLAYERS)
    {
        cout << "Sorry, unable to add " << name << 
                " -- roster is full!" << endl;
        return;
    }
    cout << "Adding player " << name << " to index "<< avail_index << endl;
    names[avail_index] = name;
    numbers[avail_index] = number;
    ratings[avail_index] = rating;
    avail_index += 1;
}

// Attempt to delete player from specified index
void DeletePlayer(int i)
{
    if (i<0 || i>=avail_index)
    {
        cout << "Sorry, unable to delete player from index " << i << endl;
        return;
    }
    cout << "Deleting player " << names[i] << " from index "<< i << endl;
    
    // Shift those right of i to the left
    for (int j=i; j<avail_index-1; ++j)
    {
        names[j] = names[j+1];
        numbers[j] = numbers[j+1];
        ratings[j] = ratings[j+1];
    }
    
    // Decrement and reset next available spot
    avail_index -= 1;
    names[avail_index] = "----";
    numbers[avail_index] = -1;
    ratings[avail_index] = -1;
}

int main() 
{
    PrintRoster();
    AddPlayer("Gilberto", 39, 85);
    AddPlayer("Abbigail", 81, 75);
    AddPlayer("Gregory", 73, 89);
    AddPlayer("Giovanna", 21, 90);
    AddPlayer("Kole", 57, 70);
    AddPlayer("Dennis", 5, 92);
    AddPlayer("Shannon", 87, 66);
    AddPlayer("Mekhi", 21, 61);
    AddPlayer("Damari", 76, 97);
    AddPlayer("Chelsea", 13, 88);
    AddPlayer("Joanna", 44, 97);
    AddPlayer("Kadyn", 58, 82);
    PrintRoster();
    
    DeletePlayer(10);
    DeletePlayer(7);
    PrintRoster();
    DeletePlayer(8);
    PrintRoster();
    AddPlayer("Joanna", 44, 97);
    AddPlayer("Kadyn", 58, 82);
    PrintRoster();
    
    return 0;
}


Can test at https://repl.it/repls/EachPertinentBookmarks

Output:
Index  Player    Jersey#   Rating    
0      ----      -1        -1        
1      ----      -1        -1        
2      ----      -1        -1        
3      ----      -1        -1        
4      ----      -1        -1        
5      ----      -1        -1        
6      ----      -1        -1        
7      ----      -1        -1        
8      ----      -1        -1        
9      ----      -1        -1        

Adding player Gilberto to index 0
Adding player Abbigail to index 1
Adding player Gregory to index 2
Adding player Giovanna to index 3
Adding player Kole to index 4
Adding player Dennis to index 5
Adding player Shannon to index 6
Adding player Mekhi to index 7
Adding player Damari to index 8
Adding player Chelsea to index 9
Sorry, unable to add Joanna -- roster is full!
Sorry, unable to add Kadyn -- roster is full!


Index  Player    Jersey#   Rating    
0      Gilberto  39        85        
1      Abbigail  81        75        
2      Gregory   73        89        
3      Giovanna  21        90        
4      Kole      57        70        
5      Dennis    5         92        
6      Shannon   87        66        
7      Mekhi     21        61        
8      Damari    76        97        
9      Chelsea   13        88        

Sorry, unable to delete player from index 10
Deleting player Mekhi from index 7


Index  Player    Jersey#   Rating    
0      Gilberto  39        85        
1      Abbigail  81        75        
2      Gregory   73        89        
3      Giovanna  21        90        
4      Kole      57        70        
5      Dennis    5         92        
6      Shannon   87        66        
7      Damari    76        97        
8      Chelsea   13        88        
9      ----      -1        -1        

Deleting player Chelsea from index 8


Index  Player    Jersey#   Rating    
0      Gilberto  39        85        
1      Abbigail  81        75        
2      Gregory   73        89        
3      Giovanna  21        90        
4      Kole      57        70        
5      Dennis    5         92        
6      Shannon   87        66        
7      Damari    76        97        
8      ----      -1        -1        
9      ----      -1        -1        

Adding player Joanna to index 8
Adding player Kadyn to index 9


Index  Player    Jersey#   Rating    
0      Gilberto  39        85        
1      Abbigail  81        75        
2      Gregory   73        89        
3      Giovanna  21        90        
4      Kole      57        70        
5      Dennis    5         92        
6      Shannon   87        66        
7      Damari    76        97        
8      Joanna    44        97        
9      Kadyn     58        82
Last edited on
Topic archived. No new replies allowed.