passing data into structures

I’ve been trying to sort out my Players wins/lossess table and am not having much luck.
I have declared a data structure to store 10 winners statistics as follows:-
1
2
3
4
5
6
struct players
{
      char winners[21];
      int move;
      int wins;
} stats[10];

I have variables within my program for player1 & player2, moves taken, tally check and whether a win has occurred:-
1
2
3
4
5
char nameA[21];                                             // declare player1 & 2 variables
char nameB[21];
int tally=0,move=0;           // declare integers for check count and number of moves taken in game

bool win;                                             // win variable   
After the tally reaches 3 win becomes true and that players name becomes the winners variable
1
2
strcpy(winners, nameB);       // or nameA if player1 wins
wins++;

At this point I am trying to put those values into the 1st element of my stats array but it obviously doesn’t work like this
1
2
3
 winners=stats[0].winners;
                              stats[0].wins = wins;
                              stats[0].move=move;

Would really appreciate a few pointers on this if anyone has the time to be generous with their hard earned knowledge!!
I don't really get your idea of structure's design, which is by the way the first and most important thing you must consider when thinking and designing a solution. Your model is a little bit confusing. That "char winners[21];" member of "players" would be better renamed as "char name[21];". There actually is a problem with entire problem exposing. "winners" identifier from "strcpy(winners, nameB);" have been declared somewhere? I see it is not addressed as member of "players". And you're right, "it obviously doesn’t work like this" - wanting to put values in the 1st element of stats and writing this "winners=stats[0].winners;". I think you should remember a few things about assignment:
http://www.cplusplus.com/doc/tutorial/operators
Sorry I had just tried reversing it to see if it made a difference I should have switched it back before posting.
My program is for a game of Connect 4 I am at the stage where I want to make a leaderboard of the 10 players with most wins(stats[10]). I will use an insertion sorting algorithm on the stats.wins afterwards to rearrange the table before saving and i can do it if the user puts the data in themselves by using cin.getline but what I am asking is how to make the members of the structure represent variables that are already defined
You can make the members of a structure to represent variables that are already defined by declaring and using pointers to those variables. So, in the "players" structure, instead of char[] (which is in fact a constant pointer - const char*), you'll use pointer to pointer of char - char**. Instead of int variables you'll redeclare those player's members as pointers of int. The problem you should ask yourself is what will happen when your defined variables are destroyed because of their limited scope?

P.S.: It's almost funny! ...or not :(
Yes it sounds logical now you say it like that. I have heard of, but not learnt about pointers yet. I am heading over to the tutorial right now to do so!
I am intending to use fstream to create and re-create a text file each time the game is played. New winners will be appended to this file and then insertion sorted before being saved. So it will not matter about variable values as they are all null at the start of each game?? Does that sound ok?

Really appreciate the advice by the way. Thankyou.
You'l persevering fellow, aren't you?
Ok... Did you had the case when the compiler warned you about a certain variable being used without being initialized first? Well, that was in fact a warning about variable mismanagement. Before going to the pointers and files make sure you are prepared for a bigger scale resource management that you should perform. Remember the problem I mentioned above? When you'll use pointers, don't just point them to limited scope variables! Otherwise soon after your pointer assignment, and after automatically deallocation of your local and limited scope variables, your pointers would come to point to nowhere! ...nowhere useful at least. To avoid that, a manually dynamically allocated unlimited-scope memory (limited to your process) would be required, with additional management for their deallocation. Here is where you should be prepared and understand very well pointers. File management requires no less attention. When opening an existing file, take care if it exists, if so - then if it can be opened or not, ...logical things, you know?
But for now I think you should understand enough all the other things. You have an array of 10 "player" objects. That would do for what you need if that "stats" array is global. When you want to access a member from the "player" object, which in it's turn is just one of those 10 "player" array named "stats", you don't write

strcpy(winners, nameB);

For accessing a "player" object from those 10 in "stats" you write "stats[n]", for example "stats[2]" would mean the third object, "stats[9]" would mean the last object in array, "stats[10]" would mean the 11'th (invalid out of array's range) object. Then, to address a member from one of these objects, like you wanted (I suppose) for "winners", you write "stats[n].winners". So "stats[0].winners" is, like you guessed somehow, the "winners" member of the first "player" object in the "stats" array. Back to that string copy of "nameB", it should have been

strcpy(stats[0].winners, nameB); //write "nameB" in the 1st element of "stats" array

I'm writing this because although you're asking about files and pointers you didn't gave no signs of understanding the basics.
Topic archived. No new replies allowed.