Unchanging variable

I have a struct with one value and one function,
1
2
3
4
5
6
7
8
struct item {
       int mod[2];
       void set (int kA, int kB, int kC) {
          mod[0]=kA;
          mod[1]=kB;
          mod[2]=kB;
       }
};

and an initializing function
1
2
3
void initItems(){
     itemInQuestion.set(1,2,3);
}

but no matter what value I put in the third paramater, the mod[2] value always becomes 10. When I initialize the itemInQuestion from the main, however, the mod[2] value becomes what I want it to.
Any ideas what might be causing this?

NOTE:
I changed some things, and noticed the mod[2] value is being determined by the next variable of the next function (lolwut?). IE:
1
2
3
4
5
void initItms(){
     oNe.set(1,2,3);
     tWo.set(10,0,0);
     thRee.set(0,0,0);
}
makes n[2] of oNe equal ten, while

1
2
3
4
5
void initItms(){
     oNe.set(1,2,3);
     tWo.set(0,0,0);
     thRee.set(0,0,0);
}
makes n[2] of oNe equal 0
Last edited on
int mod[2];
is only two elements (mod[0] and mod[1]).
You're kinda lucky your program didn't even crash when you tried to access mod[2]....

...in other words, it should be
int mod[3];
Your array mod[] is an array of only two elements, accessed by mod[0] and mod[1]. Accessing mod[2] is a memory access violation. If you want an array of three elements, you have to do int mod[3];.

Also note that line 6 in your first code snippet is wrong. The right-hand side of the equal sign has to be kC, not kB.
Thanks, I just noticed the wrong array size, and now it works like a charm.
I need to stop changing my code halfway through . . .
also, is there any way to integrate cin into a function, as in
1
2
3
void function(int i) {...}

function (cin.get());

Topic archived. No new replies allowed.