Referencing an object within a structure using a string

Hi, newbie here with what I believe is a pretty basic question. I'm trying to have the following code take the team selection input from a user and index it with one of the team names (objects) in my team structure so that I can get the currentPlayerCount associated with that team name.

I'm getting an error associated with my last line of code. Can someone tell me what I'm doing wrong?

#include <iostream>
#include <string>
#include <sstream>
using namespace std;

int totalDays = 365;

struct team {
string name;
int currentPlayerCount;

} UniversityofAlabama, AuburnUniversity, UniversityofSouthAlabama, TroyUniversity, UniversityofAlabamaBirmingham;

string teamSelection;

int main()

UniversityofAlabama.currentPlayerCount = 80;

cout << "Select A Team:\n";
getline(cin, teamSelection);
cout << "You Have Selected " << teamSelection;
cout << "Overview of " << teamSelection;
cout << "Current Roster Size: " << teamSelection.currentPlayerCount;
You don't have a } terminating main.

You're also missing endl on several of your couts.

teamSelection is a string. It doesn't have a currentlyPlayerCount member.

PLEASE USE CODE TAGS (the <> formatting button) when posting code. It makes your code easier to read and it makes it easier to respond to your post.
Thanks AbstractionAnon. I'll use code tags for subsequent posts. I also have a terminating main but failed to paste it into my original post. My bad.

Following up on my initial question, what I'm attempting to do is have the the player's input from the "Select A Team" prompt be used to match up with the identity of a specific team in the team structure. For example, if someone inputs "UniversityofAlabama", then one of the the outputs would read Current Roster Size: 80. I tried to do this by having their input be called "teamSelection" but where my code is messed up is I don't know how to match "teamSelection" up with one of the teams listed in my team structure. I think this might be a really basic concept that I'm not familiar with yet, but I'm not sure.

Eventually I want to set this up as a drop down menu, but I think I need to walk before I can run.

You have a few ways to handle this (in order of knowledge required):

1) Compare the teamSelection string against a series of literals.
  if (teamSelection == "UniversityofAlabama") 
    cout << "Current Roster Size: " << UniversityofAlabama.currentPlayerCount << endl;
  if (teamSelection == "AuburnUniversity") 
    cout << "Current Roster Size: " <<  AuburnUniversity.currentPlayerCount << endl;
  ... etc 

2) You can create a pointer to the appropriate team structure and then use the pointer to display the elements of the team structure.
  team * pteam;
  if (teamSelection == "UniversityofAlabama") 
   pteam = &UniversityofAlabama;
  // repeat for the other teams
  cout << "Current Roster Size: " <<  pteam->currentPlayerCount   << endl;

3) You can use an associative continer such as a map.
  map<string,team> teams;
  pair<"UniversityofAlabama", UniversityofAlabama) pr;
  teams.insert (pr);
  //  repeat for the other teams
   cout << "Current Roster Size: " <<  teams[teamSelection].currentPlayerCount   << endl;

Following up on this topic, say I have a long list of objects of a certain structure. In the example below, I only have 3, but imagine this list is in the hundreds. Is there a way to have user input enable a specific object so that I don't have to do hundreds of "if" statements as I have started to do below? For example, could you do something like cin >> player.rank and then use that input so that if you typed cout << the console would return the player's name whose rank was input by the user? Just thinking there must be a more elegant solution than creating hundreds of if's.

struct player {
int rank;
string name;
string position;
string state;
string height;
int weight;
string school;
float forty_time;
int bench;


int RecruitSelection;
cin >> RecruitSelection;
player * pplayer;
if (RecruitSelection == 1)
pplayer = &Robert_Nkemdiche;

if (RecruitSelection == 2)
pplayer = &Vernon_Hargreaves_III;

if (RecruitSelection == 3)
pplayer = &Jaylon_Smith;

cout << pplayer->name;
Last edited on
Since you have many players, it's really not the best idea to store each player as an individual instance. Use an array or a vector.

  vector<player>  players;
  player                p;

  //  initialize one player somehow 
  players.push_back (p);  // add player to vector 

vector<player> can be a member of team. You can apply the same principle to your original example of a collection of teams.
  vector<team>  teams;
  team                 t;

  //  initialize first team somehow
  teams.push_back (t);
  // etc... 

Please use code tags as previously requested and as you said you would do. I will not respond further posts without code tags.
Thanks, I thought I used code tags but I must have done something wrong. New here. Anyway, I didn't fully follow your response as I have not gotten to vectors yet.

I think what I'm trying to do is simple but maybe not.

All I'd like to do is take the input from "cin" that relates to one of the structure's members (for example, player rank) and, based on that input, look up which player is being referred to, and then output one of the other characteristics (members) of that same player.

As soon as you start dealing with multiples of something, arrays and vectors start making sense. This is also where loops come in.

Lets start simple using an array and your original example of teams.
Rather than declaring each team as a separate instance, we want to establish an array that will hold all our teams.
  const int MAX_TEAMS = 10;  // Just an example
  team[MAX_TEAMS] teams;  // 10 occurrances of team
  int num_teams = 0;             // count the number of teams

  //  Loading here from code.
  //  In practice, you'd probably want to load this info from a file.
  team[num_teams].name = "University of Alabama";
  team[num_teams].player_count = 20;
  num_teams++;                  // We've loaded the first team
  //  Continue loading the reamining teams 

Now we can do a couple of things. We can display a list of the teams.
  for (int i=0; i< num_teams; i++)
    cout << "Team # " << i << " " << teams[i].name << endl;

Given the team number, we can display the number of players:
  cout "Enter team # to display number of players: ";
  cin >> team_num;
  cout << teams[team_num].name << " has " << teams[team_num].player_count << endl;

Vectors are very mush like arrays, but they size themselves automatically and also keep track of how many entries they have.
I gave you two examples of using a vector earlier.

Topic archived. No new replies allowed.