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.
// Min and Max for range validation.
constint MIN = 0;
constint MAX = 300;
// Amount of players and scores each player can have (for array).
constint PLAYERS = 3;
constint 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);
}
}
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.
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).
constint PLAYERS = 3;
constint SCORES = 2;
...
int scoreSheet[PLAYERS][SCORES];
I'd suggest you provide a small complete program that illustrates the problem.
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
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.