program not working as expected

hey uhh...i made a simple program, not finished yet so..heres the code

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
92
93
#include <iostream>
#include <cstdlib>
#include <string>
#include <conio.h>
using namespace std;

char key;
const char character = '|';
const char item = '`';
const int maplength = 10;
const int mapwide = 20;
char gamemap[maplength][mapwide] =
{
    {' ','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-',' '},
    {'|',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','|'},
    {'|',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','|'},
    {'|',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','|'},
    {'|',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','|'},
    {'|',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','|'},
    {'|',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','|'},
    {'|',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','|'},
    {'|',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','|'},
    {' ','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-',' '},
};

void printmap();
void charmove();
bool checkwin();

int main()
{
    gamemap[1][1] = item;
    gamemap[4][9] = character;
    printmap();
    charmove();
}

void printmap()
{
    system("cls");
    int X, Y;
    for (X = 0; X < 10; X++)
    {
        cout << endl;
        for (Y = 0; Y < 20; Y++)
        {
            cout << gamemap[X][Y];
        }
    }
}

void charmove()
{
    bool checkkey;
    int x = 4, y = 9;
    key = getch();
    do
    {
        if (key = 'w')
        {
            gamemap[x][y] = ' ';
            x--;
            gamemap[x][y] = '|';
            checkkey = true;
        }
        else if(key = 's')
        {
            gamemap[x][y] = ' ';
            x++;
            gamemap[x][y] = '|';
            checkkey = true;
        }
        else if(key = 'a')
        {
            gamemap[x][y] = ' ';
            y--;
            gamemap[x][y] = '|';
            checkkey = true;
        }
        else if(key = 'd')
        {
            gamemap[x][y] = ' ';
            y++;
            gamemap[x][y] = '|';
            checkkey = true;
        }
        else
        {
            checkkey = false;
        }
    }while (checkkey == false);
    printmap();
}


when I pressed every single keyboard key, the character move forward instead
Last edited on
if (key = 'w') The equal operator is ==
@Thomas1965 thanks bro, but I have rewritten the program and i have got some problem

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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#include <iostream>
#include <string>
#include <conio.h>
#include <cstdlib>
using namespace std;

const char character = '0';
const char item = '*';
const int length = 10;
const int wide = 20;
const int charx = 4;
const int chary = 9;
const int itemx = 1;
const int itemy = 1;
char gamemap[length][wide] =
{
    {' ','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-',' '},
    {'|',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','|'},
    {'|',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','|'},
    {'|',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','|'},
    {'|',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','|'},
    {'|',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','|'},
    {'|',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','|'},
    {'|',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','|'},
    {'|',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','|'},
    {' ','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-',' '}
};

void printmap();
void charmovement();
bool checkwin();

int main()
{
    bool checkvictory;
    gamemap[charx][chary] = character;
    gamemap[itemx][itemy] = item;
    printmap();
    checkvictory = checkwin();
    while (checkvictory == false)
    {
        charmovement();
    }
}

void printmap()
{
    system("cls");
    int X, Y;
    for (X = 0; X < length; X++)
    {
        cout << endl;
        for (Y = 0; Y < wide; Y++)
        {
            cout << gamemap[X][Y];
        }
    }
}

void charmovement()
{
    bool checkkey;
    int x = 4, y = 9;
    char key;
    do
    {
        key = getch();
        switch (key)
        {
        case 'w' :
            gamemap[x][y] = ' ';
            x--;
            gamemap[x][y] = '0';
            checkkey = true;
            break;
        case 'a' :
            gamemap[x][y] = ' ';
            y--;
            gamemap[x][y] = '0';
            checkkey = true;
            break;
        case 's' :
            gamemap[x][y] = ' ';
            x++;
            gamemap[x][y] = '0';
            checkkey = true;
            break;
        case 'd' :
            gamemap[x][y] = ' ';
            y++;
            gamemap[x][y] = '0';
            checkkey = true;
            break;
        default :
            checkkey = false;
            break;
        }
    }while (checkkey = false);
    printmap();
}

bool checkwin()
{
    if (gamemap[1][1] == ' ' || gamemap[1][1] == '0')
        return true;
    else
        return false;
}


the problem is
 
int x = 4, y = 9;

and that everytime the function charmovement is used, x and y is resetted to 4 and 9
closed account (48T7M4Gy)
x and y in charmovement() only have scope within that function. So every time you call that function they will be initialised at those values on line 63 and when the function returns, x and y are destroyed, they no longer exist.

If you want the values to remain you can declare the x and y variables (not to be confused with the upper case ones) to be global which is bad practice, or declare them in main and pass them by reference to the function. You would delete line 63 initialisation in this case otherwise you're back to square one. :)

http://www.cplusplus.com/doc/tutorial/functions/

Last edited on
Topic archived. No new replies allowed.