Help with a bizarre problem!

I have a odd problem, which involves if statements in a for loop. I am a beginner but this problem just doesn't seem logical... My if loop as seen below repeats multiple times until the case is "else" and breaks it returning normal functioning. I tested and the for loop doesnt appear to loop abnormally at all... also this only happens when i give the console the letters 's' and 'd' but it works when i put 'w' or 'a'...

heres the code... (mind my code, its not professional, and is a bit messy)
(abnormalities around line 100... look for my // notes)
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
#include <iostream>
#include <stdlib.h>
#include <cstdlib>
#include <time.h>

using namespace std;

int main()
{
    char w = 'X';
    char o = ' ';
    char p = 'P';
    char b = 'H';
    char r = 'O';
    char g = 'G';
    char d = '+';
    char e = 'E';
    char kdir;
    int ki1;
    int kj1;
    int ki2;
    int kj2;
    int keys;
    //while

//001
//STAGES
    int fieldz1[2] = {7,7};
    char field1[7][7] = {
        {w,w,d,w,w,w,w},
        {w,o,o,o,o,o,w},
        {w,o,o,r,o,w,w},
        {w,o,o,o,w,o,w},
        {w,o,o,b,o,o,w},
        {w,o,o,p,o,o,w},
        {w,w,w,w,w,w,w},
    };

//003
//SECONDARY CODE

    bool deadall = 0;

    int fieldz[2] = {fieldz1[0],fieldz1[1]};
    char field[fieldz1[0]][fieldz1[1]];
    for(int i = 0; i < fieldz[0]; i++){
        for(int j = 0; j < fieldz[1]; j++){
                field[i][j] = field1[i][j];}
    }



//002
//MAIN CODE


    while(deadall = 1){

        system("cls");
        for(int i = 0; i < fieldz[0]; i++){
            for(int j = 0; j < fieldz[1]; j++){
                cout << field[i][j] << ' ';}
            cout << "\n";
        }

        cin >> kdir;

        if (kdir == 's'){
            ki1 = -1;
            kj1 = 0;
            ki2 = -2;
            kj2 = 0;
        }else if(kdir == 'w'){
            ki1 = 1;
            kj1 = 0;
            ki2 = 2;
            kj2 = 0;
        }else if(kdir == 'a'){
            ki1 = 0;
            kj1 = -1;
            ki2 = 0;
            kj2 = -2;
        }else if(kdir == 'd'){
            ki1 = 0;
            kj1 = 1;
            ki2 = 0;
            kj2 = 2;
        }
        if (kdir == 'w' || kdir == 's' || kdir == 'a' || kdir == 'd'){
            for(int i = 0; i < fieldz[0]; i++){

                for(int j = 0; j < fieldz[1]; j++){

//DOESNT LOOP ODDLY

                    if(field[i][j] == p){

//LOOPS ODDLY WHEN kdir = 's' or 'd'
                        if(field[i+ki1][j+kj1] == o){
                            field[i+ki1][j+kj1] = p;
                            field[i][j] = o;
                        }else if(field[i+ki1][j+kj1] == b){
                            field[i+ki1][j+kj1] = p;
                            field[i][j] = o;
                        }else if(field[i+ki1][j+kj1] == r){
                            if(field[i+ki2][j+kj2] == o){
                                field[i+ki2][j+kj2] = r;
                                field[i+ki1][j+kj1] = p;
                                field[i][j] = o;
                            }

                        }else if(field[i+ki1][j+kj1] == e){}

                    }
                    system("pause");
                }
            }
        }
        //system("pause");


    }




}


so any ideas whats wrong?
Last edited on
How exactly would you like it to behave? to "run" in a given direction until the player hits a wall/object, or to move just one step in the given direction?

The nested loops to find the player (at line 90 and 92) will be a bit tricky to get out of (a 'break' will only exit the innermost loop). Have you thought to keep track of the player position (eg add a pi and pj coords) so you don't need to search the entire grid each time? This would simplify the logic between line 90 and 117 a little...

Also, should line 57 really be an assignment, not a test?
also you don't need stdlib.h because cstdlib is the same thing in c++ and time.h is ctime
Topic archived. No new replies allowed.