HELP !!

hi friends,
i wrote a c++ program, actually it's a game; Robots game ...
but i've recieved this error "Windows has triggered a breakpoint in bla bla.."
can someone (who is kind!) help me?
that's code, but error occur on line AAA:


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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <stdio.h>
#include <vector>
#include<iostream>
#include <windows.h>

using namespace std;

void sleep(unsigned milliseconds);
bool filter(int player_i,int player_j,int size,int round,char* m_vec);
void print_map(char **map, int size);
int robots_finder(char **map,int size,int *r_location_i,int *r_location_j);
int robots_number(char **map,int size);
void player_finder(char **map,int size,int &player_i,int &player_j);
void p_motion(char **map,int size,int round,char* m_vec);

void r_motions(char **map,int size)
{
	int parameter_i,parameter_j,player_i,player_j;
	int *r_location_i = new int[1],*r_location_j = new int[1];                                     
	player_finder(map,size,player_i,player_j);
	int r_num = robots_finder(map,size,r_location_i,r_location_j);
	for(int i=1;i<=r_num;i++)
	{
		parameter_i =0;
		parameter_j =0;
		if(r_location_i[i]>player_i) parameter_i = -1;
		if(r_location_i[i]<player_i) parameter_i =  1;
		if(r_location_j[i]>player_j) parameter_j = -1;
		if(r_location_j[i]<player_j) parameter_j =  1;
		map[r_location_i[i]][r_location_j[i]] = '.';
		r_location_i[i] = r_location_i[i]+parameter_i;
		r_location_j[i] = r_location_j[i]+parameter_j;
	}
	for(int i=1;i<=r_num;i++)
	{
		switch (map[r_location_i[i]][r_location_j[i]])
	{
	case '.': 
		map[r_location_i[i]][r_location_j[i]] = '+';
		break;
	case '@': 
		map[r_location_i[i]][r_location_j[i]] = '+';
		print_map(map,size);
		cout << "Robots win ." << endl;
		sleep(1000);
		exit(1);
		break;
	case '+':
		map[r_location_i[i]][r_location_j[i]] = '*';
		break;
	case '*': 
		map[r_location_i[i]][r_location_j[i]] = '*';
		break;
	default: cout << "what r u doin' ??";
		break;
	}
	}
}

int main()
{
	////////////////////////////////////////////////////////////////////////////////	
	int size,har,round = 1;
	cin >> size;
	char **map = new char*[size+1];
	char *motions = new char[3];
	for (int i = 1; i <= size; i++) map[i] = new char[size+1];
	map[1][0] = getchar();
	for(int i = 1; i <= size; i++){ for(int j = 1; j <= size+1; j++) map[i][j] = getchar(); }
	for(har = 1 ;; har++){motions[har] = getchar(); if (motions[har] == 10 && har !=0) break; }

	do{
		p_motion(map,size,round,motions);
		r_motions(map,size);
		print_map(map,size);
		//________________________________________________________________
		if(robots_number(map,size) == 0){cout << "Player wins ." << endl;
		sleep(1000);
		exit(1);}
		//________________________________________________________________
		round++;
		sleep(2000);
	}while(1);
	////////////////////////////////////////////////////////////////////////////////	
	return 0;
}


THANKS DEARS ..
Last edited on
I'm not sure if you can declare and assign multiple objects on the stack like you did. Try this instead:

1
2
int *r_location_i = new int[1];
int *r_location_j = new int[1];


also, the array of size one is redundant. When you create a new object, one is assumed.

1
2
int *r_location_i = new int;
int *r_location_j = new int;


If you are trying to assign the value of 1 to the pointer, then what you would need to do is just this:

1
2
3
4
5
int *r_location_i = new int;
int *r_location_j = new int;

*r_location_i = 1;
*r_location_j = 1;
tnx pogrady due to your attention ...
i use your advices about seprating the parts of code, and removing [1];

but .. i still recieve that error ...!! :(
i guess , the problem is about not to free the memory that allocated on

1
2
int *r_location_i = new int;
int *r_location_j = new int;


but when i use

1
2
delet[] r_location_i ;
delet[] r_location_j ;

also i recive another error!! is it the right way to use delet?

... o god!! it's my course project !! help me !!! .. :((
Last edited on
delete r_location_i;
delete r_location_j;

You only use delete[] when deleting contents of an array declared dynamically. for single items that created by the new keyword you only need delete.
Topic archived. No new replies allowed.