How can i get certain parts of an array

Hi Guys, Now I have an array where I enter the data and then once i type "finish" the data in the array goes to another class and is stored as private data, the problem im having with this code though is I'm not able to get parts of the array back? eg, team 2 or 3.


Any help would be most appreciated, I'm currently just experimenting with ways to store teams. Thanks
Last edited on
It is generally a bad idea to use a 'system()...' statement in your code. This can cause system dependency issues as well as other security problems.

An alternative method may be to use cin.getline(); or some other form of getting input such that the program does not exit immediately. Another option is to use a while loop in your program.

I believe you will need to use an array for this, to store more than one team, and then to be able to access the team you want (either through a loop or by user input).



It also seems as though you are trying to access the wrong variable in your get_team_name function. Check the get_team_name function to make sure you are accessing the proper variable, and that the function return type matches the variable type you want to return for the team's name.

Hope this is of some help.
Last edited on
It also seems as though you are trying to access the wrong variable in your get_team_name function. Check the get_team_name function to make sure you are accessing the proper variable for the team's name.

I am under the impression I would be trying to access set_team_name function as this is where the array is being stored?

and I didn't mean to put (string teamName)
in the get_team_name function was me being silly

and I am only using system ("pause") as a temporary measure at the moment But will change it. Thanks for making me aware though.
Perhaps it is when you call the get_team_name function, it returns 1 instead of 0. This will cause the function to return the second element of teamName array, as arrays start at 0. Also, I am not seeing where teamName is an array of teams. Such an array can be created using string teamName[LIM] where LIM is the number of elements (teams in this case) that you want to store. Accesses to the individual teams could then be made:

returns.get_team_name(param) where param is of type int, and used inside the get_team_name function to access the teamName[param] element.

The get_team_name function (I would think) should access the cteams class' member variable, fteamName, since that is what is set with the call to leagueTeams[i].set_team_name(teamName).

In this case, it should probably be string fteamName[LIM], since this is the variable that stores the teams for each league, and since teamName is created inside the member function void add_league_teams() and is therefore only a temp variable.

EDIT
It might also be a good idea to check the bounds of leagueTeams[i] before adding teams, otherwise the program will throw an exception/crash (like it did when I compiled it) when 'i' becomes greater than the bounds of your array (10).

In the cleage class, you instantiate 10 league teams (cteams leagueTeams[10];), but in your main program, you only access one team (returns). This may be why you only get one team printed back as a result. To be honest, I'm surprised you get anything printed back. Check to make sure your accessing all the teams you want to in your main program.

There may be better ways to approach this altogether, I am a beginner myself - but I believe these steps should help in solving your problem. If I have not been clear or you are still having problems, post back as I will be checking often, and I'm sure someone who has a much better understanding of C++ then I will see your problem.

Sorry for the late edits - I keep rereading this program and notice something else that either modifies what I need to say for advice or that may cause bad/unexpected behavior.
//EDIT
Last edited on
I am not seeing where teamName is an array of teams. Such an array can be created using string teamName[LIM] where LIM is the number of elements (teams in this case) that you want to store. Accesses to the individual teams could then be made:

I was under the impression I created leagueTeams as the array which then stores it in cteams??
Last edited on
You are correct about leagueTeams being responsible for storing the team names. However, the call that leagueTeams makes, set_team_name(teamName), stores the name in the variable fteamName:

1
2
3
4
	void set_team_name(string teamName)
	{
		fteamName = teamName; //the team name assigned to fteamName
	}


If you want to access the team's name, the get_team_name function should access fteamName. This will be accessing the data that is stored in the cteams object.

To get back all of the teams, (print to screen or whatnot), you will need to access each cteam object that you stored data to with the call to add_league_teams. Currently, 'cteams returns' is only one of those teams, in your cleague class, you iterate through 10 league teams (leagueTeams[0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10]) and add data (team name) to them. The statement cout << returns.get_team_name(0) << endl; only accesses the team's name stored in the returns object.

It might make more sense to do the adding of team names outside of the cleague class, instead of creating an array of objects leagueTeams in that class.
Last edited on
The problem I'm having though Is when I try and call the get_team_name function to return teamName [0] it compiles ok but will not reurn the first name I entered in to the array, this is ultra confusing!!



This is the revised version of the code and all it says when i type finished and try and call back the first team in the array is "Team 1 is" and then blank??

Thanks for your time and patients, I shall get their eventually
Last edited on
When you call add_league_teams() you put a team name into the league's list. Then you try to get the name out of the teamss class, which is completely separate, and thus has nothing inside of its array.


I was assuming that this part of the code would place the array inside the cteams class?? and thus I was trying to call it from there? if this is not the case then how would I go about calling cleague so i can enter team names and then store those names in an array in cteams??

cleague is a manager class and cteams is where I want to store all the data. thanks
Last edited on
You first need to instantiate the class, cteams, then add data (team names) to that object which you instantiate. You cannot (to my knowledge) in this way, directly add data to a class itself.
1
2
3
4
	void set_team_name(string teamName, int pos)
	{
		fteamName[pos] = teamName;
	}


then in your call to set_team_name, add an int parameter to specify which position you want to put the team's name in the fteamName[] array.

To get the team names stored in a team object:
1
2
3
4
	string get_team_name (int whichteamyouwant)
	{
		return fteamName[whichteamyouwant];
	}

where whichteamyouwant is the position in the array fteamName you want to access. This way, in your call to get_team_names(), you can specify (through a for loop if you want) which team to get back.

ie:
1
2
3
for (int i = 0; i <10; i++) {
cout << get_team_names(i);
}


What you are currently doing is adding a bunch of team names to a bunch of different teams with leagueTeams[i].set_team_name(teamName);. Every time this loop is executed, variable i is increased, and the team object you are adding teams to changes. This will cause each team object to only have one team name.



Last edited on
right so what your saying is perhaps something like this would be better??

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26

void set_team_names(char team_name[MAX][LENGH])
{
	int loop;

	for (loop = 0; loop < MAX; loop++)
	{
		cout << "Enter Team  Names: " << endl;
		cin.getline(team_name[loop],25);
	}
}


int main()
{
cteams teamlist (0);

char teams[MAX][LENGH];

teamlist.set_team_names(teams);
cout << endl;
teamlist.show(teams);
cout<< endl;

cout << teams [1] << endl;

As this way I can get back contents from the array. I still would love to be able to have this loop in cleague but be able to store the data as private data in in class cteams.
I'm not sure how a multidemensional array would solve this problem, but I am not all too familiar with them so I won't say it can't work that way.

I have rewritten part of your program as shown below and it accomplishes what I believe you want:

The add_league_teams function takes a cteams object reference as a parameter:

//code removed by topic owners request

I rewrote the get_team_name method to take an int parameter that specifies which team you want to return, and the set_team_name function to take a int parameter that specifies which team you want to set (which element in fteamName you want to set):

//code removed by topic owners request

In the main body of your program:


//code removed by topic owners request

The 'for' loop just above and the myTeam.get_team_name(i) function will print out each team stored in myTeam object.

Because add_league_teams() takes a cteams object as a parameter, you can have as many cteams objects as you want as long as you instantiate them in the main body of your program, or where they are accessible to the main body of your program.

The full source now looks like this:

//code removed by topic owners request

While this is not perfect and will not make it so that each "league" has a set of teams, it accomplishes this behavior, just with different variables. It can relatively easily be writtiten to store many teams in a league object using the principals outlined above. If you want anything else explained, or if it seems as though I have failed to understand what you want to accomplish, post back and I'll be happy to try and clarify.

(Sorry for the book of a post.. trying to be thorough :P )
Last edited on
A somewhat simplified way to do this (still, not perfect and in need of some better bounds checking among other things, I'm sure) is by just using one class, 'cleague', and adding a set of teams to that league. In the main body of your program, you can declare as many different leagues as you like, and then add as many teams to those leagues you want (assuming you make the fteamName array big enough to hold the number of teams you want).

Here is the modified version of the above (basicaly a new program from the ground up - note how one class can accomplish what I think your trying to accomplish).

//code removed by authors request

I realize this is a somewhat disorganized series of posts, and if any of the members/mods on this forum disapprove posting this much code, let me know and I'll make sure not to do it again.
Last edited on
Ahh excellent, I'm with you. I can deffinatly see where I have gone wrong and can no update/correct the code. I'm going to stick with using the 2 classes as I want cteams to hold the data and cleague to effectively manage it if your with me. But i do like the single class idea aswell.


How new to coding are you? as you seem rather compitent?

Thanks buddy
Last edited on
Glad to hear it helped. (It's usually frowned upon to post entire programs, regardless of how small they might be - as sometimes people who aren't interested in learning will just copy/paste, though you seem genuine in learning).

I've been reading tutorials on C++ for around two and a half months as well as trying to learn from people's inquiries - like yours, and writing some simple programs to practice, but with work and other activities I'm not able to spend as much time on learning/practicing as I'd like to, so I'm still new to the language, but thanks for the comments. I have recently started reading up on some Java tutorials as well, and I feel they help with understanding how to better use objects and classes.

I do not have msn (probably will someday as lots of people seem to prefer it?), although if you would like to contact me, my e-mail is negotiable9@gmail.com.

Cheers
Topic archived. No new replies allowed.