-6

I have the following struct

struct statistics {
    int result[100],steps[100],gamecounter;
    float time[100];
};
typedef struct statistics GAMEST; 

In my functions I call it like this

GAMEST gamesStruct;
gamesStruct.result[2]=res;
gamesStruct.steps[2]=step;
gamesStruct.time[2]=tim;

At the moment I assign values to the struct its all good and the data are fine. But when I try to print value that I assigned previously for example gamesStruct.result[0] what I get is a big number which looks like a memory address like if the gamesStruct.result[0] is empty.

Any ideas ?

Here is some more code

 int stats(int res,int gamenum,int step,float tim,int counter){
GAMEST gamesStruct;
char vic[3][7];
if(gamenum>=0){
    strcpy(vic[0],"Draw");
    strcpy(vic[1],"Victory");
    strcpy(vic[2],"Loss");
    gamesStruct.result[gamenum]=res;
    gamesStruct.steps[gamenum]=step;
    gamesStruct.time[gamenum]=tim;
}else{
    printf("Stats per game :\n\n");
    system("pause");
    for(int i=0;i<=counter;i++){
        int tmp=gamesStruct.result[0];
        printf("%d",gamesStruct.result[0]);
    system("pause");
    system("pause");
        printf("Game %d : %s in %d moves and %f seconds\n\n",i+1,vic[tmp],gamesStruct.steps[i],gamesStruct.time[i]);
    }   
    printf("out");
    system("pause");
    int ressum[3]={0,0,0};
    int stepsum=0;
    int timesum=0;
    printf("2");
    system("pause");
    for(int i=0;i<=counter;i++){
        ressum[gamesStruct.result[i]]=ressum[gamesStruct.result[i]]+gamesStruct.result[i];
        stepsum=stepsum+gamesStruct.steps[i];
        timesum=timesum+gamesStruct.time[i];
    }
    float resmid[3],stepmid,timemid;
    resmid[0]=resmid[0]/(counter+1);
    resmid[1]=resmid[1]/(counter+1);
    resmid[2]=resmid[2]/(counter+1);
    stepmid=stepmid/(counter+1);
    timemid=timemid/(counter+1);
    printf("\n\nAverage stats for %d games:\n\n",counter+1);
    printf("\nVictories :%f  Losses :%f  Draws :%f  \n",resmid[1],resmid[2],resmid[0]);
    printf("\nAverage moves :%f and Time :%f\n\n",stepmid,timemid);
}


return(0);

}

If gamenum is bigger or equal to 0 my function assigns values properly to the struct. But when i try to print them my program crashes because the data is wrong

I want to point out that I HAVE assigned values before I try to access them, thats is my problem

mremremre1
  • 1,036
  • 3
  • 16
  • 34
  • [learn how to ask](http://stackoverflow.com/help/how-to-ask). [Post a Minimal, Complete, Valid Example](http://stackoverflow.com/help/mcve). – Rakib May 25 '14 at 11:17
  • 2
    Possible duplicate of: http://stackoverflow.com/questions/1597405/what-happens-to-a-declared-uninitialized-variable-in-c-does-it-have-a-value – vines May 25 '14 at 11:19
  • @vines my problem is that i assign a value in position 0 and when I assign in the next position of the array the previous is gone . My question is not about empty variables is about variables not maintaining their value – mremremre1 May 25 '14 at 11:20
  • 2
    @mremremre1, compilers don't do any magic, unless you do. Try to debug, or post relevant, reproducible code. – Rakib May 25 '14 at 11:22
  • @mremremre1 - if you want our help, you need to show us more information - like others told you - show us a Minimal, Complete and Valid example... – NirMH May 25 '14 at 11:23
  • @NirMH I added more code – mremremre1 May 25 '14 at 11:38
  • @mremremre1 which is nowhere near *minimal* still... – vines May 25 '14 at 20:10

1 Answers1

1

From your code - assuming the function stats is being executed one per game iteration. the gamesStruct local variable is being destroyed at end of stats and being declared (and initialized) again and again.

this is why the previous result is cleared.

you should declare the gamesStruct at tge calling API and pass it by reference to the stats method.

int stats(GAMEST& gamesStruct, int res,int gamenum,int step,float tim,int counter)
{
   ...  // same as before
}

(pay attention to create the gamesStruct before calling the iteration and destroy it properly if dynamically allocated.

NirMH
  • 4,769
  • 3
  • 44
  • 69
  • You mean that i should pass it as a parameter of the stats function ? The struct is being called by many other functions , should i declare it as global ? – mremremre1 May 25 '14 at 11:42
  • yes, exactly - if you declared it locally to the method, its memory is being returned to the OS at end of the method – NirMH May 25 '14 at 11:44
  • i want the struct to keep the data as long as the program is running. Should i declare it as extern or inside main ? – mremremre1 May 25 '14 at 11:48
  • yes. declare it on main, and pass it by reference – NirMH May 25 '14 at 11:49
  • in main i call stats like this ? stats(gamesStruct,0,-1,0,0,gamecounter); – mremremre1 May 25 '14 at 11:51