stringstream of characters

Hi there guys,

This is my first post. I am new to C++ and would like to print out a 'maze' of characters. Please could you help me with fixing my code?

I don't really know why it wont print when i cout. Thank you very much

#include <iostream>
#include <sstream>
#include <string>
using namespace std;

int main() {
const int mazeheight = 12;
const int mazewidth = 12;

char maze[mazeheight][mazewidth] =
{
{"#","#","#","#","#","#","#","#","#","#","#","#"},

{"#",".",".",".","#",".",".",".",".",".",".","#"},

{".",".","#",".","#",".","#","#","#","#",".","#"},

{"#","#","#",".","#",".",".",".",".","#","#","#"},

{"#",".",".",".",".","#","#","#",".","#",".","."},

{"#","#","#","#",".","#",".","#",".","#",".","#"},

{"#",".",".","#",".","#",".","#",".","#",".","#"},

{"#","#",".","#",".","#",".","#",".","#",".","#"},

{"#",".",".",".",".",".",".",".",".","#",".","#"},

{"#","#","#","#","#","#",".","#","#","#",".","#"},

{".",".",".",".",".",".",".","#",".",".",".","#"},

{"#","#","#","#","#","#","#","#","#","#","#","#"},

};

std::stringstream s;
s << maze;

for (int i = 0; i < mazeheight; i++) {
for (int j = 0; j < mazewidth; j++) {
maze[i][j];
cout << maze[i][j];
}
}
system("pause");
return 0;
}
Characters are surrounded by ', not ", so you need to change that in your initialization. Also, all your characters will be printed on the same line. I suggest an endl after the inner for loop.

Please use code tags next time.
http://www.cplusplus.com/forum/articles/42672/
Last edited on
Or try something like this.
Note char maze[mazeheight][mazewidth+1] to allow for the terminating null character.

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
#include <iostream>

using namespace std;

int main() 
{
    const int mazeheight = 12;
    const int mazewidth  = 12;
    
    char maze[mazeheight][mazewidth+1] =
    {
        "############",
        "#...#......#",
        "..#.#.####.#",
        "###.#....###",
        "#....###.#..",
        "####.#.#.#.#",
        "#..#.#.#.#.#",
        "##.#.#.#.#.#",
        "#........#.#",
        "######.###.#",
        ".......#...#",
        "############"
    };
    
    
    for (int i = 0; i < mazeheight; i++) 
    {
        cout << maze[i] << endl;
    }
    
    return 0;
}
Topic archived. No new replies allowed.