When I initialize my grid array I have no problem initializing it changing my row values
like so
grid[0][0] = 0;
grid[1][0] = 0;
grid[2][0] = 0;
grid[3][0] = 0;

...but when I do this "grid[0][1] = 0;" or "grid[0][2] = 0;" and all the way up to 8 the program will crash. I am confused I originally made a simple nested loop to cut down on the code to initialize the array but that won't work. The problem might be an out of bounds error but my array is setup in the constructor as int grid[4][9]. Its been awhile since I initialized an array in a constructor so please help its probably something simple I missed. Thank ya!

// Class automatically generated by Dev-C++ New Class wizard

//header
#ifndef BACKGROUND_H
#define BACKGROUND_H

#include <allegro.h>
#include "mouse.h"
#include "character.h"
#include <iostream>

using namespace std;
/*
 * No description
 */
class background
{
	public:
		// class constructor
		background();
		// class destructor
		~background();
		
		BITMAP* changeBackground(BITMAP *buffer, mouse& click, character& murderface);
		
		void setLevel(int counter);
	private:
        BITMAP *startUpScreen;
        BITMAP *level;
        BITMAP *secondLevel;
        BITMAP *thirdLevel;
        BITMAP *fourthLevel;
        BITMAP *rightArrow;
        BITMAP *leftArrow;
        BITMAP *upArrow;
        BITMAP *shiftKey;
        bool nextLevel;
        int counter;
        int grid[4][9];
};

#endif // BACKGROUND_H


//cpp
#include "background.h" // class's header file

// class constructor
background::background()
{
	startUpScreen = load_bitmap( "C:\\VGArt\\Dethklokbackground.bmp", NULL);
	level = load_bitmap( "C:\\VGArt\\firstBack.bmp", NULL);
	secondLevel = load_bitmap( "C:\\VGArt\\secondBack.bmp", NULL);
	thirdLevel = load_bitmap( "C:\\VGArt\\thirdBack.bmp", NULL);
	fourthLevel = load_bitmap( "C:\\VGArt\\fourthBack.bmp", NULL);
	rightArrow = load_bitmap( "C:\\VGArt\\right-arrow.bmp", NULL);
	leftArrow = load_bitmap( "C:\\VGArt\\left-arrow.bmp", NULL);
	upArrow = load_bitmap( "C:\\VGArt\\up-arrow.bmp", NULL);
	shiftKey = load_bitmap( "C:\\VGArt\\shift-key.bmp", NULL);
	nextLevel = false;
	counter = 0;
    
    grid[0][0] = 0;
    grid[1][0] = 0;
    grid[2][0] = 0;
    grid[3][0] = 0;
}

Recommended Answers

All 7 Replies

Remember, grid[4][9] means it goes from 0-3 and 0-8, the values 4 and 9 will not work since 0 is included. That is my first guess on the program. I haven't compiled it, but just a fast point to look at, maybe u r doing:

grid[4][0] = 0; // Not valid since only 0-3 are allowed

yea that was the first thing I looked at but i don't use 4 or 9. Thanks anyways maybe you could find the problem if you compiled it.

What did your nested for loop look like?

int i = 0;
    int j = 0; 
	 while(i <= 2){
         while(j <= 8){ grid[i][j] = 0; j++;}
         j = 0;
         i++;
    }

I have a feeling its because you are not initializing all the array. You are leaving part of it blank.

here is my code, u can use it, runs fine.

You may not need the #include"stdafx" (feel free to remove it)

#ifndef BACKGROUND_H_
#define BACKGROUND_H_

#include <iostream>

class Background
{
	private:
		int grid[4][9];
	public:
		Background();
		~Background();
		friend std::ostream & operator<<(std::ostream & os, const Background & bg);
};

#endif // End of ifndef
// Background.cpp - Definitions for background class

#include "stdafx.h"
#include "background.h"

Background::Background()
{
	int i = 0, j = 0;
	while (i < 4)
	{
		while (j < 9)
		{
			grid[i][j] = 0;
			j++;
		}
		j = 0;
		i++;
	}
}

Background::~Background()
{
}

std::ostream & operator<<(std::ostream & os, const Background & bg)
{
	int i = 0, j = 0;
	while (i < 4)
	{
		while (j < 9)
		{
			os << "grid[" << (i+1) << "][" << (j+1) << "] = " << bg.grid[i][j] << std::endl;
			j++;
		}
		j = 0;
		i++;
	}
	return os;	
}
// 2-D-ARray.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "background.h"
#include <iostream>

int _tmain(int argc, _TCHAR* argv[])
{
	Background test;
	std::cout << test << std::endl;
	system("PAUSE");
	return 0;
}
int i = 0;
    int j = 0; 
	 while([B]i <= 2[/B]){
         while(j <= 8){ grid[i][j] = 0; j++;}
         j = 0;
         i++;
    }

Why are you only going up to 2?

There's nothing wrong with the way your loop is structured, except for that index (and MasterGBerry's works fine too), but I think it's much easier to read with a for loop:

for(int i = 0;i<4;i++)
{
    for (int j =0;j<9;j++)
    {
         grid[i][j] = 0;
    }
}
//braces added for clarity

I took a break from programming and came back to it and it magically worked without any crashes. It's weird how these things work sometimes. Thanks for the help!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.