Can someone tell me how to fix this warning message in my my code of C++? main.c
ID: 3889081 • Letter: C
Question
Can someone tell me how to fix this warning message in my my code of C++?
main.cpp: In function 'void outputRoster(std::vector, std::vector)': main.cpp:114:14: warning: comparison between signed and unsigned integer expressions [-Wsign-compare] for(i=0; i
main.cpp: In function 'void deletePlayer(std::vector&, std::vector&)': main.cpp:132:14: warning: comparison between signed and unsigned integer expressions [-Wsign-compare] for(i=0; i
main.cpp: In function 'void updatePlayer(std::vector&, std::vector&)': main.cpp:168:14: warning: comparison between signed and unsigned integer expressions [-Wsign-compare] for(i=0; i
main.cpp: In function 'void aboveRating(std::vector, std::vector)': main.cpp:206:14: warning: comparison between signed and unsigned integer expressions [-Wsign-compare] for(i=0; i
here is my code in C++
#include
#include
#include
using namespace std;
//Function prototypes
void menu();
void addPlayer(vector& jerseyNumbers, vector& ratings);
void outputRoster(vector jerseyNumbers, vector ratings);
void deletePlayer(vector& jerseyNumbers, vector& ratings);
void updatePlayer(vector& jerseyNumbers, vector& ratings);
void aboveRating(vector jerseyNumbers, vector ratings);
//Main function
int main()
{
//Vectors to hold jersey numbers
vector jerseyNumbers;
//Vectors to hold ratings
vector ratings;
int temp;
int i ;
char option;
//Storing five player details
for(i=0; i<5; i++)
{
//Reading jersey numbers
cout << " Enter player " << (i+1) << "'s jersey number: ";
cin >> temp;
//Storing in vector
jerseyNumbers.push_back(temp);
//Reading rating
cout << " Enter player " << (i+1) << "'s rating: ";
cin >> temp;
//Storing in vector
ratings.push_back(temp);
}
//Loop till user wants to quit
while(1)
{
//Printing menu
menu();
//Reading option from user
cout << " Choose an option: ";
cin >> option;
//Calling appropriate function
switch(option)
{
case 'a': addPlayer(jerseyNumbers, ratings); break;
case 'd': deletePlayer(jerseyNumbers, ratings); break;
case 'u': updatePlayer(jerseyNumbers, ratings); break;
case 'r': aboveRating(jerseyNumbers, ratings); break;
case 'o': outputRoster(jerseyNumbers, ratings); break;
case 'q': exit(0);
default:
cout << " Invalid option.... "; break;
}
}
cout << endl;
return 0;
}
//Function that prints menu
void menu()
{
cout << " MENU a - Add player d - Remove player u - Update player rating r - Output players above a rating o - Output roster q - Quit ";
}
//Function that adds player to roster
void addPlayer(vector& jerseyNumbers, vector& ratings)
{
int temp;
//Reading jersey numbers
cout << " Enter player jersey number: ";
cin >> temp;
//Storing in vector
jerseyNumbers.push_back(temp);
//Reading rating
cout << " Enter player rating: ";
cin >> temp;
//Storing in vector
ratings.push_back(temp);
cout << " Player has been added successfully ";
}
//Function that prints ROSTER to console
void outputRoster(vector jerseyNumbers, vector ratings)
{
int i;
cout << " ROSTER ";
//Looping over each element in the vector and printing to console
for(i=0; i cout << " Player " << (i+1) << " --- Jersey Number: " << jerseyNumbers[i] << ", Rating: " << ratings[i];
cout << " ";
}
//Function that deletes an existing player from roster
void deletePlayer(vector& jerseyNumbers, vector& ratings)
{
int jerseyNumber;
int pos = -1, i;
//Reading jersey numbers
cout << " Enter player jersey number: ";
cin >> jerseyNumber;
//Looping over each element in the vector and searching for given jersey number
for(i=0; i {
//Comparing player jersey numbers
if(jerseyNumbers[i] == jerseyNumber)
pos = i;
}
//If player not found
if(pos == -1)
{
cout << " No player exists with given Jersey Number.... ";
}
else
{
//Deleting jersey number from vector
jerseyNumbers.erase(jerseyNumbers.begin() + pos);
//Deleting ratings from vector
ratings.erase(ratings.begin() + pos);
cout << " Player with jersey number " << jerseyNumber << " has been deleted.... ";
}
}
//Function that updates an existing player from roster
void updatePlayer(vector& jerseyNumbers, vector& ratings)
{
int jerseyNumber, rating;
int pos = -1,i;
//Reading jersey numbers
cout << " Enter player jersey number: ";
cin >> jerseyNumber;
//Looping over each element in the vector and searching for given jersey number
for(i=0; i {
//Comparing player jersey numbers
if(jerseyNumbers[i] == jerseyNumber)
pos = i;
}
//If player not found
if(pos == -1)
{
cout << " No player exists with given Jersey Number.... ";
}
else
{
//Reading new rating
cout << " Enter a new rating for player: ";
cin >> rating;
//Updating ratings vector
ratings[pos] = rating;
cout << " Player with jersey number " << jerseyNumber << " has been updated.... ";
}
}
//Function that displays the details of player whose rating is above the given rating
void aboveRating(vector jerseyNumbers, vector ratings)
{
int rating, i;
//Reading rating
cout << " Enter a rating: ";
cin >> rating;
cout << " ABOVE " << rating << " ";
//Looping over each element in the vector and printing to console
for(i=0; i {
if(ratings[i] > rating)
cout << " Player " << (i+1) << " --- Jersey Number: " << jerseyNumbers[i] << ", Rating: " << ratings[i];
}
cout << " ";
}
The original question ask for this:
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:
(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:
(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 vectors. (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:
Explanation / Answer
//Please see the updated code without warning message below:
#include <iostream>
#include <vector>
#include <string>
using namespace std;
//Function prototypes
void menu();
void addPlayer(vector<int>& jerseyNumbers, vector<int>& ratings);
void outputRoster(vector<int> jerseyNumbers, vector<int> ratings);
void deletePlayer(vector<int>& jerseyNumbers, vector<int>& ratings);
void updatePlayer(vector<int>& jerseyNumbers, vector<int>& ratings);
void aboveRating(vector<int> jerseyNumbers, vector<int> ratings);
//Main function
int main()
{
//Vectors to hold jersey numbers
vector<int> jerseyNumbers;
//Vectors to hold ratings
vector<int> ratings;
int temp;
int i ;
char option;
//Storing five player details
for(i=0; i<5; i++)
{
//Reading jersey numbers
cout << " Enter player " << (i+1) << "'s jersey number: ";
cin >> temp;
//Storing in vector
jerseyNumbers.push_back(temp);
//Reading rating
cout << " Enter player " << (i+1) << "'s rating: ";
cin >> temp;
//Storing in vector
ratings.push_back(temp);
}
//Loop till user wants to quit
while(1)
{
//Printing menu
menu();
//Reading option from user
cout << " Choose an option: ";
cin >> option;
//Calling appropriate function
switch(option)
{
case 'a': addPlayer(jerseyNumbers, ratings); break;
case 'd': deletePlayer(jerseyNumbers, ratings); break;
case 'u': updatePlayer(jerseyNumbers, ratings); break;
case 'r': aboveRating(jerseyNumbers, ratings); break;
case 'o': outputRoster(jerseyNumbers, ratings); break;
case 'q': exit(0);
default:
cout << " Invalid option.... "; break;
}
}
cout << endl;
return 0;
}
//Function that prints menu
void menu()
{
cout << " MENU a - Add player d - Remove player u - Update player rating r - Output players above a rating o - Output roster q - Quit ";
}
//Function that adds player to roster
void addPlayer(vector<int>& jerseyNumbers, vector<int>& ratings)
{
int temp;
//Reading jersey numbers
cout << " Enter player jersey number: ";
cin >> temp;
//Storing in vector
jerseyNumbers.push_back(temp);
//Reading rating
cout << " Enter player rating: ";
cin >> temp;
//Storing in vector
ratings.push_back(temp);
cout << " Player has been added successfully ";
}
//Function that prints ROSTER to console
void outputRoster(vector<int> jerseyNumbers, vector<int> ratings)
{
int i;
cout << " ROSTER ";
//Looping over each element in the vector and printing to console
for(i=0; i<static_cast<int>(jerseyNumbers.size()); i++)
cout << " Player " << (i+1) << " --- Jersey Number: " << jerseyNumbers[i] << ", Rating: " << ratings[i];
cout << " ";
}
//Function that deletes an existing player from roster
void deletePlayer(vector<int>& jerseyNumbers, vector<int>& ratings)
{
int jerseyNumber;
int pos = -1, i;
//Reading jersey numbers
cout << " Enter player jersey number: ";
cin >> jerseyNumber;
//Looping over each element in the vector and searching for given jersey number
for(i=0; i<static_cast<int>(jerseyNumbers.size()); i++)
{
//Comparing player jersey numbers
if(jerseyNumbers[i] == jerseyNumber)
pos = i;
}
//If player not found
if(pos == -1)
{
cout << " No player exists with given Jersey Number.... ";
}
else
{
//Deleting jersey number from vector
jerseyNumbers.erase(jerseyNumbers.begin() + pos);
//Deleting ratings from vector
ratings.erase(ratings.begin() + pos);
cout << " Player with jersey number " << jerseyNumber << " has been deleted.... ";
}
}
//Function that updates an existing player from roster
void updatePlayer(vector<int>& jerseyNumbers, vector<int>& ratings)
{
int jerseyNumber, rating;
int pos = -1,i;
//Reading jersey numbers
cout << " Enter player jersey number: ";
cin >> jerseyNumber;
//Looping over each element in the vector and searching for given jersey number
for(i=0; i<static_cast<int>(jerseyNumbers.size()); i++)
{
//Comparing player jersey numbers
if(jerseyNumbers[i] == jerseyNumber)
pos = i;
}
//If player not found
if(pos == -1)
{
cout << " No player exists with given Jersey Number.... ";
}
else
{
//Reading new rating
cout << " Enter a new rating for player: ";
cin >> rating;
//Updating ratings vector
ratings[pos] = rating;
cout << " Player with jersey number " << jerseyNumber << " has been updated.... ";
}
}
//Function that displays the details of player whose rating is above the given rating
void aboveRating(vector<int> jerseyNumbers, vector<int> ratings)
{
int rating, i;
//Reading rating
cout << " Enter a rating: ";
cin >> rating;
cout << " ABOVE " << rating << " ";
//Looping over each element in the vector and printing to console
for(i=0; i<static_cast<int>(jerseyNumbers.size()); i++)
{
if(ratings[i] > rating)
cout << " Player " << (i+1) << " --- Jersey Number: " << jerseyNumbers[i] << ", Rating: " << ratings[i];
}
cout << " ";
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.