HELP: My 2d array is reporting incorrect inputs. I am not sure why

I am having a problem where if I input 1, 2, 3, 4 for each game and i am receiving a different output from the array (scoreSheet[][]). I would get . .
1 1 2 3
1 2 3 4
1 2 3 4
Would anyone know why?
The code below is just a snippet of my application so some of my info may be relevant to the question.

All help would be appreciated!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
  	// Min and Max for range validation.
	const int MIN = 0;
	const int MAX = 300;
	
	// Amount of players and scores each player can have (for array).
	const int PLAYERS = 3;
	const int SCORES = 2;
	
	// A sum of the averages across all players.
	double averagesSumed;	
	
	// Array that holds each score for player for every game.
    int scoreSheet[PLAYERS][SCORES];
    // Array that holds each players score average.
    double averageScores[PLAYERS];
    
    // Header for the page
    cout << "Bowling Score Sheet" << endl;
    cout << "-------------------" << endl;
    
    // Nested For structure that displays a game header and gets each players score for that game.
    for (int gameNumber = 0; gameNumber <= SCORES;  gameNumber++)
	{
		cout << endl << "=======================" << endl;
		cout << "Results of game " << gameNumber + 1 << endl;
		cout << "=======================" << endl;
		
		for(int userIndex = 0; userIndex <= PLAYERS; userIndex++)
		{
			cout << userIndex<< "    " << gameNumber;	cout << "Score from " << userName(userIndex) << ": "; 

		    scoreSheet[userIndex][gameNumber] = GetValidInteger(MIN, MAX);		    
		}
		
	} 
Last edited on
Well this is only a guess because I don't see where you're entering anything, but you do appear to be accessing your arrays out of bounds. Remember arrays start at zero and stop at SIZE - 1, in your loops you're stopping at SIZE not SIZE - 1.

My cin is in my GetValidInteger() function. If i subtract 1 i dont get my full size of 4 PLAYERS and 3 SCORES.
Normally instead of subtracting 1 you should just use the operator< instead of the operator<= in your loops.
for (int gameNumber = 0; gameNumber < SCORES; gameNumber++)


Also your arrays are not 4 players and 3 scores, they are 3 players and 2 scores.

1
2
3
4
5
	// Amount of players and scores each player can have (for array).
	const int PLAYERS = 3;
	const int SCORES = 2;
...
    int scoreSheet[PLAYERS][SCORES];



I'd suggest you provide a small complete program that illustrates the problem.
Heres an image of the output. The very bottom is just showing the array values:
http://tinypic.com/r/nqogsm/9
and when i change to the same for statement you have i have one less game. Which is not what i want.
and what i meant as was PLAYERS = 3. 0, 1, 2, 3 and SCORES = 2. 0, 1, 2.
i incremented PLAYERS and SCORES by one and it added another set of number.s but worked correctly I dont want this but it works. I only output the first 3 sets and not the last.
http://tinypic.com/r/2f0emgl/9
Post your current code.

and when i change to the same for statement you have i have one less game. Which is not what i want.

If you want an array of 4 players then you need a SIZE of 4 not three. The valid array indexes of an array with a size of 3 is 0, 1, 2 trying to access index 3 would be accessing your array out of bounds.
Topic archived. No new replies allowed.