officerkrupke
officerkrupke

Reputation: 43

Overwriting values in a for loop

I have the following code, where book is an array of structures and alpha, beta . . are members of the struct:

 for(i = 0; i < sizeof(book)/sizeof(book[0]); ++i) {
    n    = book[i].alpha;
    m    = book[i].beta;
    gnm  = book[i].gamma;
    hnm  = book[i].delta;
    dgnm = book[i].epsilon;
    dhnm = book[i].zeta;        
}

I want to use this to store the data that I have in a structure array into the variables n,m,..etc. Right now this code only stores the last line of data into each variable. How can I change this code so that the value isn't overwritten each time? eg instead of just storing 12 it should be 123456789101112.

Here is the code:

int main()
{ 
int i,n,m,floot;
int aplha[90],beta[90], buf_size = 3500, offset = 0;
float gnm,hnm,dgnm,dhnm,gamma[90],delta[90],epsilon[90],zeta[90];
static char  c_str[90]; 

    struct wmm
       {
        int   alpha;
        int   beta;
        float gamma;
        float delta;
        float epsilon;
        float zeta;
       }book[]= {
  {1, 0, -29496.6,       0.0,       11.6,       0.0},
  {1, 1,  -1586.3,    4944.4,       16.5,     -25.9},
  {2, 0,  -2396.6,       0.0,      -12.1,       0.0},
  {2, 1,   3026.1,   -2707.7,       -4.4,     -22.5},
  {2, 2,   1668.6,    -576.1,        1.9,     -11.8},

[...50 or so similar lines omitted...]

 {12, 11,    -0.8,      -0.2,       -0.1,        0.0},
 {12, 12,     0.0,       0.9,        0.1,        0.0}
 };

Upvotes: 2

Views: 1029

Answers (2)

Greg
Greg

Reputation: 1500

You need to change the operation you are using to store data. Right now you are using the assignment ('=') operation which will overwrite whatever information was previously stored in your archiving variables.

Instead, you want to replace this with an appropriate append function. For string data this probably means concatenation, for integer data, you are looking at something complicated enough that I would suggest first converting it to a string, and then storing a concatenation of that data.

So your loop would look something like:

for(i = 0; i < sizeof(book)/sizeof(book[0]); ++i)
  {
     append(&n, book[i].alpha);
     append(&m, book[i].beta);     
     append(&gnm, book[i].gamma);
     append(&hnm, book[i].delta);
     append(&dgnm, book[i].epsilon);
     append(&dhnm, book[i].zeta);
  } 

Where append is a procedure you define to perform the appropriate work for your data type.

Alternately, I suppose you could turn your storage variables into arrays of the appropriate type and simply assign incoming data to the matching index into those arrays, e.g. n[i] = book[i].alpha, but that may be redundant given that you already have an array of structs storing that data for you.

Upvotes: 1

Dan F
Dan F

Reputation: 17732

You need to store them in such a way that it appends the data. This can either be done with some kind of string object, or an array. I'm not sure what your data type here is so I can't give an exact example, but the idea is to avoid repetitive assignment like that, as that will just override the previous value each iteration of the loop

Upvotes: 1

Related Questions