not executing cout

i am writing a game that involes computer players to run over a character and when they move off of it, the previous character is printed.
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
// ghost

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "defines.h"
#include "externVar.h"

struct Ghost{
       int gposX;
       int gposY;
       int gpreX;
       int gpreY;
       int direction;
       bool ghostMoved;
       bool FOOD;
       
       void updateGhost(){
            field[gposX][gposY] = ghost;
            if (FOOD)
               field[gpreX][gpreY] = food; \\ put the "food" back after ghost passed 
            else {
                 field[gpreX][gpreY] = ' ';
                 }
            ghostMoved = true;
            return;
       }
       
       void ghostMove (int dir){
            if (!ghostMoved){
               if (dir == _UP){
                  gpreX = gposX;
                  gpreY = gposY;
                  gposX -= 1;
                  if (!ghostCanMove(gposX, gposY)){
                       std::cout << "Came here instead" << std::endl; \\ not executing
                       system("pause");
                       gposX = gpreX;
                       ghostTurn();
                       ghostMove(direction);
                       }
                  else{
                         std::cout << "going to updateGhost" << std::endl; // not executing
                         updateGhost();
                         std::cout << "Back from updateGhost" << std::endl; // not executing
                         system("pause");
                         if (field[gposX][gposY] == food)
                            FOOD = true;
                         else
                             FOOD = false;
                         return;
                         }
                    }
               else if (dir == _DOWN){
                    gpreX = gposX;
                    gpreY = gposY;
                    gposX += 1;
                    if (!ghostCanMove(gposX, gposY)){
                       std::cout << "Came here instead" << std::endl;
                       system("pause");
                       gposX = gpreX;
                       ghostTurn();
                       ghostMove(direction);
                       }
                    else{
                         std::cout << "going to updateGhost" << std::endl;
                         updateGhost();
                         std::cout << "Back from updateGhost" << std::endl;
                         system("pause");
                         if (field[gposX][gposY] == food)
                            FOOD = true;
                         else
                             FOOD = false;
                         return;
                         }
                    }
               else if (dir == _LEFT){
                    gpreX = gposX;
                    gpreY = gposY;
                    gposY -= 1;
                    if (!ghostCanMove(gposX, gposY)){
                       std::cout << "Came here instead" << std::endl;
                       system("pause");
                       gposX = gpreX;
                       ghostTurn();
                       ghostMove(direction);
                       }
                    else{
                         std::cout << "going to updateGhost" << std::endl;
                         updateGhost();
                         std::cout << "Back from updateGhost" << std::endl;
                         system("pause");
                         if (field[gposX][gposY] == food)
                            FOOD = true;
                         else
                             FOOD = false;
                         return;
                         }
                    }
               else if (dir == _RIGHT){
                    gpreX = gposX;
                    gpreY = gposY;
                    gposY += 1;
                    if (!ghostCanMove(gposX, gposY)){
                       std::cout << "Came here instead" << std::endl;
                       system("pause");
                       gposX = gpreX;
                       ghostTurn();
                       ghostMove(direction);
                       }
                    else{
                         std::cout << "going to updateGhost" << std::endl;
                         updateGhost();
                         std::cout << "Back from updateGhost" << std::endl;
                         system("pause");
                         if (field[gposX][gposY] == food)
                            FOOD = true;
                         else
                             FOOD = false;
                         return;
                         }
                    }
               }
            else
                std::cout << "somehow skipping everything but still moving" << std::endl; // still not executing
                system("pause");
                return;
       }

       void ghostTurn(){
            if (direction == _UP)
               direction = _LEFT;
            else if (direction == _DOWN)
                 direction = _RIGHT;
            else if (direction == _LEFT)
                 direction = _DOWN;
            else if (direction == _RIGHT)
                 direction = _UP;
       }
       
       bool ghostCanMove(int x, int y){
            if ((field[x][y] == wall) || (field[x][y] == ghost) || (field[x][y] == player))
                  return false;
            else 
                 return true;
       }
       
       void ghostComputer (){
            if ((field[gposX - 1][gposY] == player) || (field[gposX - 2][gposY] == player) || (field[gposX - 3][gposY] == player) || (field[gposX - 4][gposY] == player)){
               direction = _UP;
               ghostMove(direction);
               }
            else if ((field[gposX + 1][gposY] == player) || (field[gposX + 2][gposY] == player) || (field[gposX + 3][gposY] == player) || (field[gposX + 4][gposY] == player)){
                 direction = _DOWN;
                 ghostMove(direction);
                 }
            else if ((field[gposX][gposY - 1] == player) || (field[gposX][gposY - 2] == player) || (field[gposX][gposY - 3] == player) || (field[gposX][gposY - 4] == player)){
                 direction = _LEFT;
                 ghostMove(direction);
                 }
            else if ((field[gposX][gposY + 1] == player) || (field[gposX][gposY + 2] == player) || (field[gposX][gposY + 3] == player) || (field[gposX][gposY + 4] == player)){
                 direction = _RIGHT;
                 ghostMove(direction);
                 }
            else{
                   ghostMove(direction);
                }
       }
};

so my problem is that it will run over the piece it will not put it back. the struct is entered by ghostComputer(). so it will not "cout" things, but somehow it still moves

field is an external char array[31][28]
the field is drawn with a method that uses two for loops, that cout works... i'm lost
Step through your program with a debugger, and see if it is reaching the correct lines in your code. Perhaps the flow control has a mistake in it somewhere?
Topic archived. No new replies allowed.