This program will allow the user to see a list of teams, add a team to the list,
ID: 3885271 • Letter: T
Question
This program will allow the user to see a list of teams, add a team to the list, or search for a team using an ID.
1. Structure Definition (5 points)
First define struct TeamS with an integer field to hold an ID, and an array of strings with three elements to hold the names of the team members. In the main function, declare an empty vector of TeamV and also the following arrays:
const int ID [NUM_TEAMS] ={ 123, 321, 456, 789};
const string MEMBERS [NUM_TEAMS] [NUM_MEMBERS ] = { {"Sarah", "Joe", "John"}, {"Chris", "Kevin", "James"}, {"Tom", "Kim", "Emily"}, {"Jill", "Jason", "Jim"} };
2. Creating the vector (9 points)
Call a function with the following prototype and pass in the empty vector and the arrays:
void Initialize (vector <TeamS> & TeamV, const int id[], const string m[][NUM_MEMBERS], int arraySize);
The function should place into the vector the elements of the arrays.
For example, the first element of the vector will have team id: 123 and members: Sarah, Joe, and John. The second element of the vector will have team id: 321 and members: Chris, Kevin, and James, and so on.
3. The print function (10 points)
Back in main, call a function with the following prototype to show that your vector has been populated with the team information:
void printList (const vector <TeamS> & TeamV);
The function simply prints out the content of the vector.
4. The menu Function (5 points)
Next, place a loop in your main function such that in each iteration the user sees a menu and has the chance to enter a choice.
Call a function with the following prototype to display the menu:
void menu();
If the users selects option 1, call the printList function to display the list.
5. Adding to a vector If the user selects option 2, call a function with the following protoype which will allow the user to enter the information for a team which will be added to your team vector: (10 points)
void add (vector <TeamS> & TeamV);
6. Searching a Vector If the user selects option 3, ask for the ID the user wants to search for and call a function with the following protoype which will perform a search on your vector and return the index of the element with the matching ID or -1 if no match is found: (5 points)
int search(const vector <TeamS> & TeamV, int id );
Make sure that this function performs a linear search.
7. Displaying one vector entry
In the search function, printout the information of the team with the matching ID In main, use the return value of the function to determine if an error message is to be displayed if no matching ID is found. (5 points)
8. Ending the program
If the user selects option 4, stop the program.(1 points) Display the following message: Lab 2 written by XXXXXX has ended. Where XXXXXX is your First and Last name
Help, I am stuck! This is what I have so far.
#include <iostream>
#include <iomanip>
#include <string>
#include <vector>
using namespace std;
const int NUM_TEAMS = 4;
const int NUM_MEMBERS = 3;
struct TeamS
{
int ID;
string team_Mates[3];
};
void Initialize(vector <TeamS> & TeamV, const int id[], const string m[][NUM_MEMBERS], int arraySize)
{
cout << "Starting Initialization" << endl;
TeamS team;
for (int z = 0; z < sizeof(id); z++)
{
team.ID = id[z];
for (int i = 0; i < arraySize; i++)
{
for (int j = 0; j < arraySize; j++)
team.team_Mates[z] = m[i][j];
}
TeamV.push_back(team);
}
cout << "Ending Initialization" << endl;
}
void printList(const vector <TeamS> & TeamV)
{
cout << "----Team List----" << endl << endl;
for (int x = 0; x < NUM_TEAMS; x++)
{
cout << "Information for team " << x+1 << endl;
cout << "ID: " << TeamV[x].ID << endl;
for (int y = 0; y < NUM_MEMBERS; y++)
{
cout << " " << TeamV[y].team_Mates << endl;
}
}
cout << " -----------------" << endl;
}
void menu()
{
cout << "Enter 1 to see a list of teams." << endl;
cout << "Enter 2 to add a team to the list." << endl;
cout << "Enter 3 to search for a team using an ID." << endl;
cout << "Enter 4 to end the program." << endl;
}
void add(vector <TeamS> & TeamV)
{
TeamS team;
for (int a = 0; a < TeamV.size(); a++)
{
cout << "Enter ID." << endl;
cin >> TeamV[a].ID;
for (int b = 0; b < 4; b++)
{
cout << " Enter member name " << b + 1 << endl;
TeamV[b].team_Mates[a][b];
}
TeamV.push_back(team);
break;
}
}
int search(const vector <TeamS> & TeamV)
{
int idNumber;
cout << "Enter an ID Number." << endl;
cin >> idNumber;
}
int main()
{
int arraySize = 4;
int user_Input;
vector<TeamS> TeamV;
const int ID[NUM_TEAMS] = { 123, 321, 456, 789 };
const string MEMBERS[NUM_TEAMS][NUM_MEMBERS] = { { "Sarah", "Joe","John", },
{ "Chris", "Kevin", "James" },
{ "Tom", "Kim", "Emily" },
{ "Jill", "Jason", "Jim" } };
Initialize(TeamV, ID, MEMBERS, arraySize);
do
{
menu();
cin >> user_Input;
switch (user_Input)
{
case 1:
printList(TeamV);
break;
case 2:
add(TeamV);
break;
case 3:
search(TeamV);
}
} while (user_Input != 4);
system("pause");
return 0;
}
Explanation / Answer
Hi,
I haved added the search function and the last part of handling the print of search, here is the fully executable code,
#include <iostream>
#include <iomanip>
#include <string>
#include <vector>
using namespace std;
const int NUM_TEAMS = 4;
const int NUM_MEMBERS = 3;
struct TeamS
{
int ID;
string team_Mates[3];
};
void Initialize(vector <TeamS> & TeamV, const int id[], const string m[][NUM_MEMBERS], int arraySize)
{
cout << "Starting Initialization" << endl;
TeamS team;
for (int z = 0; z < sizeof(id); z++)
{
team.ID = id[z];
for (int i = 0; i < arraySize; i++)
{
for (int j = 0; j < arraySize; j++)
team.team_Mates[z] = m[i][j];
}
TeamV.push_back(team);
}
cout << "Ending Initialization" << endl;
}
void printList(const vector <TeamS> & TeamV)
{
cout << "----Team List----" << endl << endl;
for (int x = 0; x < NUM_TEAMS; x++)
{
cout << "Information for team " << x+1 << endl;
cout << "ID: " << TeamV[x].ID << endl;
for (int y = 0; y < NUM_MEMBERS; y++)
{
cout << " " << TeamV[y].team_Mates << endl;
}
}
cout << " -----------------" << endl;
}
void menu()
{
cout << "Enter 1 to see a list of teams." << endl;
cout << "Enter 2 to add a team to the list." << endl;
cout << "Enter 3 to search for a team using an ID." << endl;
cout << "Enter 4 to end the program." << endl;
}
void add(vector <TeamS> & TeamV)
{
TeamS team;
for (int a = 0; a < TeamV.size(); a++)
{
cout << "Enter ID." << endl;
cin >> TeamV[a].ID;
for (int b = 0; b < 4; b++)
{
cout << " Enter member name " << b + 1 << endl;
TeamV[b].team_Mates[a][b];
}
TeamV.push_back(team);
break;
}
}
int search(const vector <TeamS> & TeamV,int id)
{
int idNumber;
cout << "Enter an ID Number." << endl;
cin >> idNumber;
for (int a = 0; a < TeamV.size(); a++)
{
if(TeamV[a].ID==id)
{
return a;
}
}
return -1;
}
int main()
{
int arraySize = 4;
int user_Input;
vector<TeamS> TeamV;
const int ID[NUM_TEAMS] = { 123, 321, 456, 789 };
const string MEMBERS[NUM_TEAMS][NUM_MEMBERS] = { { "Sarah", "Joe","John", },
{ "Chris", "Kevin", "James" },
{ "Tom", "Kim", "Emily" },
{ "Jill", "Jason", "Jim" } };
Initialize(TeamV, ID, MEMBERS, arraySize);
do
{
menu();
cin >> user_Input;
switch (user_Input)
{
case 1:
printList(TeamV);
break;
case 2:
add(TeamV);
break;
case 3:
int index=search(TeamV);
if(index==-1)
cout<<"no team found with given id"<<endl;
else
cout<<"you searched for vector:"<<TeamV[index]<<endl;
break;
}
} while (user_Input != 4);
system("pause");
return 0;
}
Thumbs up if this was helpful, otherwise let me know in comments.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.