Program stops when I choose different directions

Why does my program stop when I switch directions (like "right" to "left", or "down" to "up"), but if I keep going in the same direction (like "left" to "left") it works fine?

All 4 blocks of code (one block for one of the 4 directions) have identical base code, and they all refer back to the original input "M," so I fail to see why switching to another direction bugs the program.

For reference, this is the "Dungeon Crawl" assignment from:
http://www.cplusplus.com/forum/articles/12974/

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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
using namespace std;

int main()
{
    int coord1 = 0, coord2 = 0;
    int coord3 = 0;
    int coord4 = 0;
    int TreasureX = ((int)(rand()%8));
    int TreasureY = ((int)(rand()%7)) + 1;
    int x = 0;
    int RandomChance = 0;
    string M;
    string Response;

    cout << "You, the player, is at the coordinates [0][0]. Your goal is to find the treasure" << endl;


    cout << "Do you want to move: left, right, up, or down?" << endl;
    cin >> M;

    while (M == "left")
    {
        cout << "You moved one space to the left." << endl;
        coord1 = coord1 - 1 ;
        if (coord1 = coord1 - 1)
        {
            int pickedChoice = ((int)(rand() % 4)) + 1;
            if (pickedChoice = 1)
            {
                coord3 --;
            }
            else if (pickedChoice = 2)
            {
                coord3 ++;
            }
            else if (pickedChoice = 3)
            {
                coord4 --;
            }
            else if (pickedChoice = 4)
            {
                coord4 ++;
            }
            cout << "The monster is now at coordinates: " << coord3 << "," << coord4 << endl;
        }
        if (coord1 == TreasureX && coord2 == TreasureY)
        {
            cout << "You've found the treasure and won the game!" << endl;
            break;
        }
        cout << "You are at the coordinates: " << coord1 << "," << coord2 << endl;
        cout << "Choose again, where do you want to go: left, right, up, or down?" << endl;
        cin >> M;
        std::cin.ignore (std::numeric_limits<std::streamsize>::max(), '\n');

    }

    while (M == "right")
    {
        cout << "You moved one space to the right." << endl;
        coord1++ ;
        if (coord1++)
        {
            int pickedChoice = ((int)(rand() % 4)) + 1;
            if (pickedChoice = 1)
            {
                coord3 --;
            }
            else if (pickedChoice = 2)
            {
                coord3 ++;
            }
            else if (pickedChoice = 3)
            {
                coord4 --;
            }
            else if (pickedChoice = 4)
            {
                coord4 ++;
            }
            cout << "The monster is now at coordinates: " << coord3 << "," << coord4 << endl;
        }
        if (coord1 == TreasureX && coord2 == TreasureY)
        {
            cout << "You've found the treasure and won the game!" << endl;
            break;
        }
        cout << "You are at the coordinates: " << coord1 << "," << coord2 << endl;
        cout << "Choose again, where do you want to go: left, right, up, or down?" << endl;
        cin >> M;
        std::cin.ignore (std::numeric_limits<std::streamsize>::max(), '\n');

    }

    while (M == "up")
    {
        cout << "You moved one space upward." << endl;
        coord2-- ;
        if (coord2--)
        {
            int pickedChoice = ((int)(rand() % 4)) + 1;
            if (pickedChoice = 1)
            {
                coord3 --;
            }
            else if (pickedChoice = 2)
            {
                coord3 ++;
            }
            else if (pickedChoice = 3)
            {
                coord4 --;
            }
            else if (pickedChoice = 4)
            {
                coord4 ++;
            }
            cout << "The monster is now at coordinates: " << coord3 << "," << coord4 << endl;
        }
        if (coord1 == TreasureX && coord2 == TreasureY)
        {
            cout << "You've found the treasure and won the game!" << endl;
            break;
        }
        cout << "You are at the coordinates: " << coord1 << "," << coord2 << endl;
        cout << "Choose again, where do you want to go: left, right, up, or down?" << endl;
        cin >> M;
        std::cin.ignore (std::numeric_limits<std::streamsize>::max(), '\n');

    }


    while (M == "down")
    {
        cout << "You moved one space downward." << endl;
        coord2++ ;
        if (coord2++)
        {
            int pickedChoice = ((int)(rand() % 4)) + 1;
            if (pickedChoice = 1)
            {
                coord3 --;
            }
            else if (pickedChoice = 2)
            {
                coord3 ++;
            }
            else if (pickedChoice = 3)
            {
                coord4 --;
            }
            else if (pickedChoice = 4)
            {
                coord4 ++;
            }
            cout << "The monster is now at coordinates: " << coord3 << "," << coord4 << endl;
        }
        if (coord1 == TreasureX && coord2 == TreasureY)
        {
            cout << "You've found the treasure and won the game!" << endl;
            break;
        }
        cout << "You are at the coordinates: " << coord1 << "," << coord2 << endl;
        cout << "Choose again, where do you want to go: left, right, up, or down?" << endl;
        cin >> M;
        std::cin.ignore (std::numeric_limits<std::streamsize>::max(), '\n');

    }

    if (coord3 == coord1 && coord4 == coord2)
    {
        cout << "The monster caught you... game over." << endl;
    }


return 0;
} 
Last edited on
Ahem, look at the warnings:
||=== Build: Debug in Test (compiler: GNU GCC 4.9.1x64) ===|
\Test\main.cpp||In function 'int main()':|
\Test\main.cpp|33|warning: suggest parentheses around assignment used as truth value [-Wparentheses]|
\Test\main.cpp|36|warning: suggest parentheses around assignment used as truth value [-Wparentheses]|
\Test\main.cpp|40|warning: suggest parentheses around assignment used as truth value [-Wparentheses]|
\Test\main.cpp|44|warning: suggest parentheses around assignment used as truth value [-Wparentheses]|
\Test\main.cpp|48|warning: suggest parentheses around assignment used as truth value [-Wparentheses]|
\Test\main.cpp|73|warning: suggest parentheses around assignment used as truth value [-Wparentheses]|
\Test\main.cpp|77|warning: suggest parentheses around assignment used as truth value [-Wparentheses]|
\Test\main.cpp|81|warning: suggest parentheses around assignment used as truth value [-Wparentheses]|
\Test\main.cpp|85|warning: suggest parentheses around assignment used as truth value [-Wparentheses]|
\Test\main.cpp|110|warning: suggest parentheses around assignment used as truth value [-Wparentheses]|
\Test\main.cpp|114|warning: suggest parentheses around assignment used as truth value [-Wparentheses]|
\Test\main.cpp|118|warning: suggest parentheses around assignment used as truth value [-Wparentheses]|
\Test\main.cpp|122|warning: suggest parentheses around assignment used as truth value [-Wparentheses]|
\Test\main.cpp|148|warning: suggest parentheses around assignment used as truth value [-Wparentheses]|
\Test\main.cpp|152|warning: suggest parentheses around assignment used as truth value [-Wparentheses]|
\Test\main.cpp|156|warning: suggest parentheses around assignment used as truth value [-Wparentheses]|
\Test\main.cpp|160|warning: suggest parentheses around assignment used as truth value [-Wparentheses]|
\Test\main.cpp|18|warning: unused variable 'x' [-Wunused-variable]|
\Test\main.cpp|19|warning: unused variable 'RandomChance' [-Wunused-variable]|
||=== Build finished: 0 error(s), 19 warning(s) (0 minute(s), 3 second(s)) ===|

The bunch of similar warnings refers to lines like these: if (pickedChoice = 1) 
Read about problem with them here: http://www.cplusplus.com/faq/beginners/logic/#eq-vs-assign
@MiiNiPaa thanks for the help, I fixed all the warnings but the main problem still remains.

For example, when I go from ("down" to "up) or ("left" to "right" to "left"), the program just stops/breaks.
Your program is essentually:

1
2
3
4
5
6
7
8
9
while(M == "left") {
    //Handle moving left
}
//As soon you stopped moving left, you will never return to previous loop.
//Now you will never be able to move left
while (M == "right") //...
while (M == "up") //...
while (M == "down")  ///...
//As soon as you made past all these loops you cannot move. 
Topic archived. No new replies allowed.