What's the problem here

I tried everything but x and y values always point 0 and -1

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
    
for(;;) {
        cout << " TELL ME WHERE YOU WANT TO GO : UP, DOWN, LEFT, RIGHT " << endl;

        cin >> way;

        if(way.compare("UP") || way.compare("up")) {
        
            updateRoom(0, -1, xPos, yPos);
        }else if(way.compare("DOWN") || way.compare("down")) {

            updateRoom(0, 1, xPos, yPos);
        }else if(way.compare("LEFT") || way.compare("left")) {

            updateRoom(-1, 0, xPos, yPos);
        }else if(way.compare("RIGHT") || way.compare("right")) {

            updateRoom(1, 0, xPos, yPos);
        } 
    }
    return 0;
}

void updateRoom(int x, int y, int &posx, int &posy) {
    
    
    posx += x;
    posy += y;
    
    system("CLS");
    cout << x;
    cout << y;
    cout << posx;
    cout << posy;
    cout << endl;
}
closed account (Lv0f92yv)
Your x and y values here will not change because you are passing by value only 0 and -1. If you want to modify them, you must either pass them by reference and change the value that way, or return a new value and store it into x and/or y.

1
2
3
4
5
6
7
8
9
updateRoom( int* x, int* y, ... )
{
   //change x and y
   int newXValue = 5;
   int newYValue = 6;
   *x = newXValue;
   *y = newYValue;
//now both x and y that were passed in have changed.
}


Hope this helps, if not, post back and someone will try and help.
But how about other calling functions . I call updateRoom with 0 -1, 0 1 , 1 , 0, -1 0. Why dont they work
Because if compare finds strings it returns 0 not 1.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
	for(;;) {
        cout << " TELL ME WHERE YOU WANT TO GO : UP, DOWN, LEFT, RIGHT " << endl;

        cin >> way;

        if(way.compare("UP")== 0 || way.compare("up")==0) {
        
            updateRoom(0, -1, xPos, yPos);
        }else if(way.compare("DOWN")==0 || way.compare("down")==0) {

            updateRoom(0, 1, xPos, yPos);
        }else if(way.compare("LEFT")==0 || way.compare("left")==0) {

            updateRoom(-1, 0, xPos, yPos);
        }else if(way.compare("RIGHT")==0 || way.compare("right")==0) {

            updateRoom(1, 0, xPos, yPos);
        } 
    }
New problem guys

game.cpp: In function ‘void drawRoom()’:
game.cpp:68: error: invalid array assignment
game.cpp:69: error: invalid array assignment
game.cpp:70: error: invalid array assignment
game.cpp:71: error: invalid array assignment
game.cpp:72: error: invalid array assignment
game.cpp:73: error: invalid array assignment
game.cpp:74: error: invalid array assignment
game.cpp:75: error: invalid array assignment
game.cpp:76: error: invalid array assignment
game.cpp:77: error: invalid array assignment
game.cpp:78: error: invalid array assignment
game.cpp:79: error: invalid array assignment
game.cpp:80: error: invalid array assignment
game.cpp:81: error: invalid array assignment
game.cpp:82: error: invalid array assignment
game.cpp:83: error: invalid array assignment
game.cpp:84: error: invalid array assignment
game.cpp:85: error: invalid array assignment


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
	char room[18][63];

		room[0] =  ":............................................................:";

		room[1] =  ":                   :                      :                 :";

		room[2] =  ":                   :                      :                 :";

		room[3] =  ":....+++............:                      :....+++..........:";

		room[4] =  ":                                                            :";

		room[5] =  "+                                                            :";

		room[6] =  "+                                                            :";

		room[7] =  "+                                                            :";

		room[8] =  ":                                                            :";

		room[9] =  ":                                                            :";

		room[10] = ":.......+++.................................                 +";

		room[11] = ":                  :                       :                 +";

		room[12] = ":                  :                       :                 +";

		room[13] = ":                  :.........              :                 :";

		room[14] = ":                           :              +                 :";

		room[15] = ":                           :              +                 :";

		room[16] = ":                           :              :                 :";

		room[17] = ":..........................................:.................:";



		for(int i = 0; i < 19; i++) {



			cout << room[i] << endl;

		}
closed account (Lv0f92yv)
error C2440: '=' : cannot convert from 'const char [63]' to 'char [63]'

Consider using a vector of strings - since the things inside quotes are sequences of characters (which is what a string is).

Put all the strings in a vector (collection), each string will contain the sequence of chars.
it gives segmentation fault

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
	string room[18] = {
		":............................................................:",
		":                   :                      :                 :",

		":                   :                      :                 :",

		":....+++............:                      :....+++..........:",

		":                                                            :",

		"+                                                            :",

		"+                                                            :",

		"+                                                            :",

		":                                                            :",

		":                                                            :",

		":.......+++.................................                 +",

		":                  :                       :                 +",

		":                  :                       :                 +",

		":                  :.........              :                 :",

		":                           :              +                 :",

		":                           :              +                 :",

		":                           :              :                 :",

		":..........................................:.................:"
		};



		for(int i = 0; i < 19; i++) {



			cout << room[i] << endl;

		}
Desh wrote:
Consider using a vector of strings - since the things inside quotes are sequences of characters (which is what a string is).

Put all the strings in a vector (collection), each string will contain the sequence of chars.


1
2
3
4
5
6
7
8
std::vector<std::string> map;
map.push_back("........................................................................\n");
map.push_back(".                                                                      .\n");
map.push_back("........................................................................\n");

for( int i = 0; i < map.size(); i++ )
{
    std::cout << map[i];

}
Topic archived. No new replies allowed.