String array problem? >_>;

With the following code, the '@' key set on line 80 does not appear.

Also, just noticed that the '@' key won't clear after you edit the string. >_>;

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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include <iostream>
#include <limits>
#include <conio.h>
using namespace std;
namespace
{
    string board[14] =
    {
        "################################################################################"
        "#                                                                              #"
        "#                                                                              #"
        "#                                                                              #"
        "#                                                                              #"
        "#                                                                              #"
        "#                                                                              #"
        "#                                                                              #"
        "#                                                                              #"
        "#                                                                              #"
        "#                                                                              #"
        "#                                                                              #"
        "#                                                                              #"
        "################################################################################"
    };
    int curr_x = 1;
    int curr_y = 1;
}

void dispboard(int moves);
int main()
{

    cout << "Welcome to Duncrawl! Press ENTER to start.\n";
    cin.ignore(numeric_limits<streamsize>::max(), '\n');
    int moves = 0;
    dispboard(moves);
    do
    {
        char c = _getch();
        switch (c)
        {
            case 'w': case 'W':
                if(curr_y != 1)
                {
                    curr_y--;
                    dispboard(++moves);
                }
                break;
            case 'a': case 'A':
                if(curr_x != 1)
                {
                    curr_x--;
                    dispboard(++moves);
                }
                break;
            case 's': case 'S':
                if(curr_y != 12)
                {
                    curr_y++;
                    dispboard(++moves);
                }
                break;
            case 'd': case 'D':
                if(curr_x != 78)
                {
                    curr_x++;
                    dispboard(++moves);
                }
                break;
            case 'q': case 'Q':
                return 0;
                break;
            default:
                break;
        }
    }while(true);
}

void dispboard(int moves)
{
    (board[curr_y])[curr_x] = '@';
    for(int i = 0; i < 14; i++)
        cout << board[i];
    cout << "Moves: " << moves << endl;
    cout << "Up: W\tDown: S\tLeft: A\tRight: D\n";
}
Last edited on
So what's your question?
My question is how I get the '@' key to at least display; editing the array doesn't seem to help.
line 82, that should read

cout << (board[curr_y])[i];

or you need to have nested loops. I can't even get it to compile other wise.
Ah, so true.
EDIT: well, I'm displaying one row at a time.
Last edited on
Where is the comma operator separating each string?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
std::string board[14] = {

                   "************",
                   "************",
                   "************",
                   "************",
                   "************",
                   "************",
                   "************",
                   "************",
                   "************",
                   "************",
                   "************",
                   "************",
                   "************",
                   "************"
};


Without the comma my array would seem like the first string is a huge one and the 13 other are just "";

Was that intended???
OH, DAMMIT!

Thanks!

It's the little syntactical errors that get me!
For those interested, here is the result.

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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include <iostream>
#include <limits>
#include <conio.h>
#include <string>
using namespace std;
namespace
{
    string board[14] =
    {
        "################################################################################",
        "#                                                                              #",
        "#                                                                              #",
        "#                                                                              #",
        "#                                                                              #",
        "#                                                                              #",
        "#                                                                              #",
        "#                                                                              #",
        "#                                                                              #",
        "#                                                                              #",
        "#                                                                              #",
        "#                                                                              #",
        "#                                                                              #",
        "################################################################################"
    };
    int curr_x = 1;
    int curr_y = 1;
}

void dispboard(int moves);
int main()
{

    cout << "Welcome to Duncrawl! Press ENTER to start.\n";
    cin.ignore(numeric_limits<streamsize>::max(), '\n');
    int moves = 0;
    dispboard(moves);
    do
    {
        char c = _getch();
        switch (c)
        {
            case 'w': case 'W':
                if(curr_y != 1)
                {
                    (board[curr_y])[curr_x] = ' ';
                    curr_y--;
                    dispboard(++moves);
                }
                break;
            case 'a': case 'A':
                if(curr_x != 1)
                {
                    (board[curr_y])[curr_x] = ' ';
                    curr_x--;
                    dispboard(++moves);
                }
                break;
            case 's': case 'S':
                if(curr_y != 12)
                {
                    (board[curr_y])[curr_x] = ' ';
                    curr_y++;
                    dispboard(++moves);
                }
                break;
            case 'd': case 'D':
                if(curr_x != 78)
                {
                    (board[curr_y])[curr_x] = ' ';
                    curr_x++;
                    dispboard(++moves);
                }
                break;
            case 'q': case 'Q':
                return 0;
                break;
            default:
                break;
        }
    }while(true);
}

void dispboard(int moves)
{
    (board[curr_y])[curr_x] = '@';
    cout << string(100, '\n');
    for(int i = 0; i < 14; i++)
        cout << board[i];
    cout << "Moves: " << moves << endl;
    cout << "Up: W\tDown: S\tLeft: A\tRight: D\n";
}

Nice proper use of an unnamed namespace. :)
Topic archived. No new replies allowed.