Creating a Soccer Roster using vectors and loops

Hello,
I have spent copious amount of time on this code, and I don't know if I really don't understand the issue, or if my brain is just fried at this point, but I just cannot figure out what to change:) The prompt is below.

The error messages are mostly about my for loops, saying that I have comparison between signed and unsigned integer expressions.
~~~~~~~~~~~~~~~~~
main.cpp: In function ‘int main()’:
main.cpp:53:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (i = 0; i < jersey.size(); i++) {
~~^~~~~~~~~~~~~~~
main.cpp:68:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < jersey.size(); i++) {
~~^~~~~~~~~~~~~~~
main.cpp:84:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < jersey.size(); i++)
~~^~~~~~~~~~~~~~~
main.cpp:92:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < jersey.size(); i++)
~~^~~~~~~~~~~~~~~
main.cpp: At global scope:
main.cpp:101:1: error: expected declaration before ‘}’ token
}
~~~~~~~~~~~~~~~~~~~~~
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 vector and the ratings in another int vector. Output these vectors (i.e., output the roster). (3 pts)

Ex:

Enter player 1's jersey number:
84
Enter player 1's rating:
7

Enter player 2's jersey number:
23
Enter player 2's rating:
4

Enter player 3's jersey number:
4
Enter player 3's rating:
5

Enter player 4's jersey number:
30
Enter player 4's rating:
2

Enter player 5's jersey number:
66
Enter player 5's rating:
9

ROSTER
Player 1 -- Jersey number: 84, Rating: 7
Player 2 -- Jersey number: 23, Rating: 4
...
(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 pts)

Ex:

MENU
a - Add player
d - Remove player
u - Update player rating
r - Output players above a rating
o - Output roster
q - Quit

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

Ex:

ROSTER
Player 1 -- Jersey number: 84, Rating: 7
Player 2 -- Jersey number: 23, Rating: 4
...
(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 vectors. (1 pt)

Ex:

Enter a new player's jersey number:
49
Enter the player's rating:
8
(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:

Enter a jersey number:
4
(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:

Enter a jersey number:
23
Enter a new rating for player:
6
(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:

Enter a rating:
5

ABOVE 5
Player 1 -- Jersey number: 84, Rating: 7
...
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
#include <iostream>
#include <vector>
using namespace std;

int main() {
   int temp;
   vector <int> jersey;
   vector <int> rating;
  
int i,n;
for(i=0;i<5;i++)
{
cout<<"Enter player "<<i+1<<"\'s jersey number:" << endl;
cin>>n;jersey.push_back(n);
cout<<"Enter player "<<i+1<<"\'s rating:" << endl;
cin>>n;rating.push_back(n);
cout << endl;
}

cout<<"ROSTER \n";
for(i=0;i<5;i++)
{
cout<<"Player "<<i+1<<"-- Jersey number:"<<jersey.at(i)<<" Rating:"<<rating.at(i)<<"\n";
}

char option;

while (true) 
cout << "MENU" << endl << "a - Add player" << endl << "d - Remove player" << endl << "u - Update player rating" << endl;
cout << "r - Output players above a rating" << endl << "o - Output roster" << endl << "q - Quit" << endl << endl;

cout << "Choose an option: ";
cin >> option;

switch (option) {
   case 'a':
   case 'A':
   cout << "Enter a new player's jersey number: ";
   cin >> temp;
   jersey.push_back(temp);
   
   cout << "Enter the player's rating: ";
   cin >> temp;
   rating.push_back(temp);
   break;
   
   case 'd':
   case 'D':
   cout << "Enter a jersey number: ";
   cin >> temp;
   int i;
   
   for (i = 0; i < jersey.size(); i++) {
      if (jersey.at(i) == temp) {
         jersey.erase(jersey.begin() + i);
         rating.erase(rating.begin() + i);
         break;
      }
   }
   
break;

case 'u':
case 'U':
cout << "Enter a jersey number: ";
cin >> temp;

for (int i = 0; i < jersey.size(); i++) {
if (jersey.at(i) == temp) {
   cout << "Enter a new rating for player: ";
   cin >> temp;
   rating.at(i) = temp;
   break;
}
}
break;

case 'r':
case 'R':
cout << "Enter a rating: ";
cin >> temp;
cout << endl << "ABOVE " << temp << endl;

for (int i = 0; i < jersey.size(); i++)
if (rating.at(i) > temp)
cout << "Player " << i + 1 << " -- Jersey number: " << jersey.at(i) << ", Rating: " << rating.at(i) << endl;
break;

case 'o':
case 'O':
cout << "ROSTER" << endl;
for (int i = 0; i < jersey.size(); i++)
cout << "Player " << i + 1 << " -- " << "Jersey number: " << rating.at(i) << endl;
break;

case 'q':
   return 0;
   default: cout << "Invalid menu option. Try again." << endl;
}
}
}

   return 0;
}
Tidy up your code: there is a couple of extra closing curly parentheses, but they are difficult to spot in a not properly indented code.

One of the above two should pair off with a missing open curly parentheses where your while-loop starts:
while (true)
(The most of the IDEs takes an auto-indent / beautifier or similar option along: it’s usually a good idea to take advantage of it.)

The quoted warnings say you’re comparing a signed to an unsigned value. It can often introduce subtle logic errors, so the compiler alerts you.

What does std::vector::size() return?
http://www.cplusplus.com/reference/vector/vector/size/
size_type size() const noexcept;
Return Value
The number of elements in the container.
Member type size_type is an unsigned integral type.

Inside many for-loops you try to determine if an int (=signed) “i” is less than a std::vector::size() (which returns an unsigned).

Other issues are:
a) your ‘o’ option: it displays the “rating” but not the “jersey”
b) you don’t check if the user inputs the same ‘jersey’ value for more than one player, so the loop inside your ‘u’ option should not break after having updated only the first player found.

the problem is with your curly braces you have too many,if you a curly brace your IDE depending which one you are using will match the opening with the closing


as for the others they are just warnings which shouldn't be ignored but the code will still run


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


#include <iostream>
#include <vector>
using namespace std;

int main()
{
    int temp;
    vector <int> jersey;
    vector <int> rating;

    int i,n;
    for(i=0; i<5; i++)
    {
        cout<<"Enter player "<<i+1<<"\'s jersey number:" << endl;
        cin>>n;
        jersey.push_back(n);
        cout<<"Enter player "<<i+1<<"\'s rating:" << endl;
        cin>>n;
        rating.push_back(n);
        cout << endl;
    }

    cout<<"ROSTER \n";
    for(i=0; i<5; i++)
    {
        cout<<"Player "<<i+1<<"-- Jersey number:"<<jersey.at(i)<<" Rating:"<<rating.at(i)<<"\n";
    }

    char option;

    while (true)
        cout << "MENU" << endl << "a - Add player" << endl << "d - Remove player" << endl << "u - Update player rating" << endl;
    cout << "r - Output players above a rating" << endl << "o - Output roster" << endl << "q - Quit" << endl << endl;

    cout << "Choose an option: ";
    cin >> option;

    switch (option)
    {
    case 'a':
    case 'A':
        cout << "Enter a new player's jersey number: ";
        cin >> temp;
        jersey.push_back(temp);

        cout << "Enter the player's rating: ";
        cin >> temp;
        rating.push_back(temp);
        break;

    case 'd':
    case 'D':
        cout << "Enter a jersey number: ";
        cin >> temp;
        int i;

        for (i = 0; i < jersey.size(); i++)
        {
            if (jersey.at(i) == temp)
            {
                jersey.erase(jersey.begin() + i);
                rating.erase(rating.begin() + i);
                break;
            }
        }

        break;

    case 'u':
    case 'U':
        cout << "Enter a jersey number: ";
        cin >> temp;

        for (int i = 0; i < jersey.size(); i++)
        {
            if (jersey.at(i) == temp)
            {
                cout << "Enter a new rating for player: ";
                cin >> temp;
                rating.at(i) = temp;
                break;
            }
        }
        break;

    case 'r':
    case 'R':
        cout << "Enter a rating: ";
        cin >> temp;
        cout << endl << "ABOVE " << temp << endl;

        for (int i = 0; i < jersey.size(); i++)
            if (rating.at(i) > temp)
                cout << "Player " << i + 1 << " -- Jersey number: " << jersey.at(i) << ", Rating: " << rating.at(i) << endl;
        break;

    case 'o':
    case 'O':
        cout << "ROSTER" << endl;
        for (int i = 0; i < jersey.size(); i++)
            cout << "Player " << i + 1 << " -- " << "Jersey number: " << rating.at(i) << endl;
        break;

    case 'q':
        return 0;
    default:
        cout << "Invalid menu option. Try again." << endl;
    }


return 0;
}

Topic archived. No new replies allowed.