Array error

I have an array, map[10] which has the folloing data:
1
2
3
4
5
6
7
8
9
10
11
maze[0] = "#------*>             ";
maze[1] = "l...f..l>             ";
maze[2] = "l......l>             ";
maze[3] = "l......$------------*>";
maze[4] = "l...................l>";
maze[5] = "l...................l>";
maze[5] = "l..........#--------&>";
maze[6] = "$----------&>         ";
maze[7] = "............>         ";
maze[8] = "x...........>         ";
maze[9] = "y...........>         ";


Now heres the problem. I want to replace the "f" with another "." so i have this code at the moment:

maze[y] = "l......l> ";
which works in replacing the "f" with an "." but this isnt practical, cause if the "f" was in another place, the code would need to change. So i tried

 
maze[y][x] = '.';

but i get some weird error: Unhandled exception at 0x012f5aee in Text Game.exe: 0xC0000005: Access violation writing location 0x012f8920.

My question is how can i replace the "f" with a "." without having to replace the whole line?

Sorry if this is a bit vague, but can ANYONE help?
Thanks in advance,
Sysem
You could do that by casting your "..." to std::string (otherwise, const char* is assumed). That would be for lines 1-11:
maze[x] = std::string("...");

Hope that helps.
When i change it to:
 
maze[y][x] = string(".");


i get the following error,

error C2440: '=' : cannot convert from 'std::basic_string<_Elem,_Traits,_Ax>' to 'char'
if your maze array is an array of string then you don't need maze[y][x] = string(".");
This will try to replace the 'f' with a string. You need to replace 'f' with a '.'
maze[y][x] = '.';
Argh, sorry, I misunderstood. Please post the definition of maze.
at the moment, the array is declared as char* maze[10] so maze[y][x] = '.'; doesnt work
Change it to
1
2
3
4
5
6
7
8
#include <vector>
#include <string>
...
std::vector<std::string> > maze;
maze.push_back("#------*>             ");
maze.push_back("l...f..l>             ");
...
maze[x][y] = 'X';

...and it should work.

Edit:
Alternatively, you can write
1
2
3
std::vector<std::string> > maze(10);
maze[0] = "#------*>             ";
...
Last edited on
To keep your existing code you can just change the char* maze[10] to string maze[10]; and then the maze[y][x] = '.'; will be valid.
Tried that exception, but got 56 errors.
Im new to C++ so sorry bout this :)
will try your one Mitsakos..
Last edited on
Tried the string maze[10] but got a different error saying Debug Assertion Error. Maybe i should stick to VB :P
Of course, whenever you expected your old array, you now have to use the vector. It might seem a bit complicated at first, but nevertheless you should use it. The type of array you were using is called "C-style array". It is in C++ merely for compatibility with C. You sholudn't use it in most cases. The C++ substitute for C-style arrays are vectors, used as in the example:
vector<T> my_vector(size);
where T is any typename (like int, char*, std::string, ...) and size is the initial size. It can be ommited for a size of 0 (vectors can be resized using either resize() or push_back()). One advantage over a C-style array is, among many others, that vectors know their size. So if you want a larger maze, you can simply "push_back" one more line, and if your other code is dependent on maze.size() rather than a fixed number, everything works fine with the new size.
Just rewrite your functions from
void f(char* maze[10])
to
void f(const std::vector<std::string> >& maze)
and most of the errors should vanish. For the rest, just ask ;-)
Last edited on
Topic archived. No new replies allowed.