Crazy values! Please help!

Im working on a generic text-based RPG. I have a function (equipCheck) that checks if my items are equivalent to items in the item list. If so, the stats of my items become the stats that correspond to the item number. Then, after the for loops, it adds up all the stats for all 7 equipment types and puts them into my playerStats.
For some reason, after I do equipCheck, instead of the resulting stat being normal, or around 2, its like in the millions, and it changes each time.I'm very confused about why this is happening.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
    //CODE FOR PRINTING THE VALUES
    cout << playerStats[3] << endl; //0
    equipCheck();
    cout << playerStats[3] << endl; //in the millions

    ///ACTUAL EQUIPCHECK FUNCTION
void equipCheck(){
    for(int x; x < 7;x++){
        for(int y; y< itemCount;y++){
            if(playerEquips[x] == item[y]){
                playerEquipsVal[x][0] = itemVal[y][0];
                playerEquipsVal[x][1] = itemVal[y][1];
                playerEquipsVal[x][2] = itemVal[y][2];
                playerEquipsVal[x][3] = itemVal[y][3];
            }
        }
    }
    for(int q; q < 4; q++){
            playerStats[q+3] = playerEquipsVal[0][q] + playerEquipsVal[1][q] + playerEquipsVal[2][q] + playerEquipsVal[3][q] + playerEquipsVal[4][q] + playerEquipsVal[5][q] + playerEquipsVal[6][q]; //Adding stats then putting them into playerStats
    }

}


Thanks for reading!
You haven't specified an initial value in any of your for loops.
 
    for(int x; x < 7;x++) // x is not initialised 

perhaps you intended this:
 
    for (int x=0; x < 7; x++)


... and the same with y and q
Last edited on
I added the = 0, but it did not change anything. The values are still in the millions and are random.
Last edited on
There might be other issues. Right now I don't understand why you have q+3 here in function equipCheck()
 
    playerStats[q+3]


By the way, it's a good idea to start a new line occasionally rather than having very long horizontal lines for example,
1
2
3
4
5
6
7
8
9
10
11
    for (int q=0; q < 4; q++)
    {
        playerStats[q] = //Adding stats then putting them into playerStats 
            playerEquipsVal[0][q] + 
            playerEquipsVal[1][q] + 
            playerEquipsVal[2][q] + 
            playerEquipsVal[3][q] + 
            playerEquipsVal[4][q] + 
            playerEquipsVal[5][q] + 
            playerEquipsVal[6][q]; 
    }
Last edited on
The +3 is because I set up the playerStats oddly. The first three stats, 0,1,2, which are Strength, Intelligence, and Dexterity, are not currently stats for items, so to get the 3rd, 4th,5th, and 6th stats, I need to do a +3.
I think you need to show more of the code. in particular the part where you declare the arrays playerStats and playerEquipsVal, and also where you assign initial values to those arrays.

I'm guessing either the arrays themselves contain invalid values, or considering the sample of code seen thus far, possible there are invalid subscripts or some incorrect usage elsewhere in the code. I dare say the complete code might be very long, and if so asking someone else to comb through it might be asking a lot.

What I might suggest instead is that you use a debugger to step through the relevant parts of the code line by line, watching the values of important variables as you do so.
Last edited on
I did a lot of debugging on my own (my program wont let me debug. I figured out it was because when I initialized the playerEquipsVal array it was playerEquipsVal[][4]; when it should have been playerEquipsVal[7][4]. It works great, thanks for taking the time to help me :D
Topic archived. No new replies allowed.