I keep having an error. Without chaning any code, could anyone fix the problem?
ID: 3809536 • Letter: I
Question
I keep having an error. Without chaning any code, could anyone fix the problem?
The output should be
Enter anoter player's jersey number:
Enter another player's rating:
But I have another line above them..
an option:
Enter anoter player's jersey number:
Enter another player's rating:
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<int> jerseyNumber;
vector<int> rating;
int temp;
for(int i = 1; i <= 5; i++)
{
cout<<"Enter player "<<i<<"'s jersey number: " << endl;
cin>>temp;
jerseyNumber.push_back(temp);
cout<<"Enter player "<<i<<"'s rating: " << endl;
cin>>temp;
rating.push_back(temp);
cout<<endl;
}
cout<<" ROSTER"<<endl;
for (int i = 0; i < 5; i++)
cout<<"Player "<<i+1<<" -- "<<"Jersey number: "<<jerseyNumber.at(i)<<", Rating: "<<rating.at(i)<<endl;
char option;
while(true)
{
cout<<" MENU"<<endl;
cout<<"a - Add player"<<endl;
cout<<"d - Delete 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;
cout<<"Choose an option: " << endl;
cin>>option;
switch(option)
{
case 'a':
case 'A':
cout<<"Enter anoter player's jersey number: ";
cin>>temp;
jerseyNumber.push_back(temp);
cout<<"Enter another 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 < jerseyNumber.size(); i++)
{
if(jerseyNumber.at(i) == temp)
{
jerseyNumber.erase (jerseyNumber.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 < jerseyNumber.size(); i++)
{
if(jerseyNumber.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<<" ABOVE "<<temp<<endl;
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;
case 'o':
case 'O':
cout<<"ROSTER"<<endl;
for (int i = 0; i < jerseyNumber.size(); i++)
cout<<"Player "<<i+1<<" -- "<<"Jersey number: "<<jerseyNumber.at(i)<<", Rating: "<<rating.at(i)<<endl;
break;
case 'q': return 0;
default : cout<<"Invalid menu option. Try again."<<endl;
}
}
}
Explanation / Answer
/*this code is ok but if you want to repeat menu items then it is ok and if you don't want to see the message "Choose an option" then replace it's place to another location within the while loop as I have done. as the code required you have to choose at least one menu option to varify your code. if you have still problem this code so explain it. I would solve it. */
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<int> jerseyNumber;
vector<int> rating;
int temp;
for(int i = 1; i <= 5; i++)
{
cout<<"Enter player "<<i<<"'s jersey number: " << endl;
cin>>temp;
jerseyNumber.push_back(temp);
cout<<"Enter player "<<i<<"'s rating: " << endl;
cin>>temp;
rating.push_back(temp);
cout<<endl;
}
cout<<" ROSTER"<<endl;
for (int i = 0; i < 5; i++)
cout<<"Player "<<i+1<<" -- "<<"Jersey number: "<<jerseyNumber.at(i)<<", Rating: "<<rating.at(i)<<endl<<endl;
char option;
while(true)
{
cout<<"Choose an option: " << endl;
cout<<" MENU"<<endl;
cout<<"a - Add player"<<endl;
cout<<"d - Delete 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;
cin>>option;
switch(option)
{
case 'a':
case 'A':
cout<<"Enter anoter player's jersey number: ";
cin>>temp;
jerseyNumber.push_back(temp);
cout<<"Enter another 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 < jerseyNumber.size(); i++)
{
if(jerseyNumber.at(i) == temp)
{
jerseyNumber.erase (jerseyNumber.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 < jerseyNumber.size(); i++)
{
if(jerseyNumber.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<<" ABOVE "<<temp<<endl;
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;
case 'o':
case 'O':
cout<<"ROSTER"<<endl;
for (int i = 0; i < jerseyNumber.size(); i++)
cout<<"Player "<<i+1<<" -- "<<"Jersey number: "<<jerseyNumber.at(i)<<", Rating: "<<rating.at(i)<<endl;
break;
case 'q': return 0;
default : cout<<"Invalid menu option. Try again."<<endl;
}
}
}