#include<iostream>
using std::cout;
using std::cin;
using std::endl;
#include<conio.h>
class Mouse
{   public:
	void move();
	void turn();
};
void Mouse::move()
{
 char ch;
 int length;
 int width;

 switch (ch)
 {
 case '1':
	 cout<<"To move forward";
	 for(int y;y==length;y--)
		 break;
 case'2':
	 for(int x;x<width;x++)
		 break;
 case'3':
	 for(int x;x>0;x--)
		 break;
  }
}
void Mouse::turn()
{
 for(int a=0;a<200;a++)
	 cout<<a<<"^"<<(char)23<<endl;
}
int main()
{
  int length,width;
  cout<<"Please enter the width of the room:";
  cin>>width;
  cout<<"Please enter the length of the room:";
  cin>>length;
  int ** myArray = 0;
  myArray = new int*[length];
if (myArray != NULL) {
  for (int i = 0; i < length; i++) {
     myArray[i] = new int[width];
  cout<<"X";
  }
}
for(int x=0;x<width;x++)
{
	for(int y=0;y<length;y++)
		myArray[x][y]=0;
}
cout<<length<<"x"<<width<<"x";
int x,y;
if(x<4&&y<4){
myArray[3][4]=1;
myArray[1][3]=1;
}
else 
cout<<"you entered wrong obstacles"<<endl;
getch();
return 0;
}

/*The compilergives this runtime error could you solve the problem about it?

'hgh.exe': Loaded 'C:\Users\medion\Documents\Visual Studio 2008\Projects\hgh\Debug\hgh.exe', Symbols loaded.
'hgh.exe': Loaded 'C:\Windows\System32\ntdll.dll'
'hgh.exe': Loaded 'C:\Windows\System32\kernel32.dll'
'hgh.exe': Loaded 'C:\Windows\winsxs\x86_microsoft.vc90.debugcrt_1fc8b3b9a1e18e3b_9.0.30729.1_none_bb1f6aa1308c35eb\msvcp90d.dll'
'hgh.exe': Loaded 'C:\Windows\winsxs\x86_microsoft.vc90.debugcrt_1fc8b3b9a1e18e3b_9.0.30729.1_none_bb1f6aa1308c35eb\msvcr90d.dll'
Run-Time Check Failure #3 - The variable 'x' is being used without being initialized.
Run-Time Check Failure #3 - The variable 'y' is being used without being initialized.
First-chance exception at 0x00241a0f in hgh.exe: 0xC0000005: Access violation writing location 0xabababbb.
Unhandled exception at 0x00241a0f in hgh.exe: 0xC0000005: Access violation writing location 0xabababbb.
First-chance exception at 0x00241a0f in hgh.exe: 0xC0000005: Access violation writing location 0xabababbb.
Unhandled exception at 0x00241a0f in hgh.exe: 0xC0000005: Access violation writing location 0xabababbb.
First-chance exception at 0x00241a0f in hgh.exe: 0xC0000005: Access violation writing location 0xabababbb.
Unhandled exception at 0x00241a0f in hgh.exe: 0xC0000005: Access violation writing location 0xabababbb.
First-chance exception at 0x00241a0f in hgh.exe: 0xC0000005: Access violation writing location 0xabababbb.
Unhandled exception at 0x00241a0f in hgh.exe: 0xC0000005: Access violation writing location 0xabababbb.
First-chance exception at 0x00241a0f in hgh.exe: 0xC0000005: Access violation writing location 0xabababbb.
Unhandled exception at 0x00241a0f in hgh.exe: 0xC0000005: Access violation writing location 0xabababbb.
First-chance exception at 0x00241a0f in hgh.exe: 0xC0000005: Access violation writing location 0xabababbb.
Unhandled exception at 0x00241a0f in hgh.exe: 0xC0000005: Access violation writing location 0xabababbb.
The program '[3028] hgh.exe: Native' has exited with code 0 (0x0).*/

Dani AI

Generated

As correctly noticed, the immediate problems are uninitialized locals and mismatched indexing. Uninitialized loop counters or variables produce unpredictable values; using those as array indexes will write outside the memory that was allocated and trigger the access-violation you saw. The code also allocates the grid with one dimension order and then fills it using the opposite order, which guarantees out-of-bounds writes. Finally, the mouse methods declare ch, length, and width locally but never set them, so those routines cannot work as written.

Concrete checklist and safe approach (apply in order)

  • Initialize every local variable when declared or set it from input before use.
  • Allocate and fill the grid with a consistent row/column order. Prefer std::vector so allocation/cleanup is automatic.
  • Create walls by writing the border cells after the grid is allocated and initialized. Use explicit bounds checks.
  • Make movement methods operate on well-defined state: either pass the grid/position as parameters or make them members that are initialized in the constructor.
  • Test with small sizes (3x3, 5x5) and print the grid to verify before adding input handling or animation.

Example pattern (safe, different from the original snippet)

int rows = height;
int cols = width;
std::vector<std::vector<int>> grid(rows, std::vector<int>(cols, 0));

// set walls (mark with 1)
for (size_t r = 0; r < rows; ++r) {
    grid[r][0] = 1;
    grid[r][cols-1] = 1;
}
for (size_t c = 0; c < cols; ++c) {
    grid[0][c] = 1;
    grid[rows-1][c] = 1;
}

// simple print for verification
for (size_t r = 0; r < rows; ++r) {
    for (size_t c = 0; c < cols; ++c)
        std::cout << (grid[r][c] ? '#' : '.');
    std::cout << '\n';
}

Extra notes

  • If raw new[] is used, free memory with matching delete[] and keep the same row/col ordering.
  • Replace platform-specific console calls only after game logic is stable.
  • For obstacles or snake body, reserve distinct integer codes (e.g., 0=empty, 1=wall, 2=obstacle, 3=snake) and always check bounds before writes.

Follow these fixes; they address the runtime errors and provide a clear, maintainable way to build room walls and game logic.

Recommended Answers

All 4 Replies

lines 21 and 24: >> for(int x;x<width;x++)
You forgot the variable initializer. Should be like this: for(int x = 0;x<width;x++)

thanks a lot!

But i couldnt create the walls of the game and comile it could you help me
Ancient Dragon

line 17: the value of ch is undefined. Similar problem with all the other variables declared in that function. Variables have to be initialized with something before using them.

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.