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;
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.
2) You can create a pointer to the appropriate team structure and then use the pointer to display the elements of the team structure.
1 2 3 4 5
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.
1 2 3 4 5
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 << player.name 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;
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.
1 2 3 4 5 6 7 8 9 10 11
constint 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.
1 2
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:
1 2 3
cout "Enter team # to display number of players: ";
cin >> team_num;
cout << teams[team_num].name << " has " << teams[team_num].player_count << endl;