goto function wont work

my goto function wont work when I call it in the else if statement b==3. Thank you!

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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
  #include <iostream>
#include <windows.h>
#include <stdio.h>

COORD coord= {0,0};

using namespace std;

void gotoxy(int x, int y)
{
    coord.X=x;
    coord.Y=y;
    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),coord);
}
class Piece
{
private:
    int x,y,color;
public:
    Piece()
    {
        x=-1;
        y=-1;
        color=2;
    }
    void setcolor(int c)
    {
        color=c;
    }
    void setposition(int x_pos,int y_pos, int c)
    {
        x=x_pos;
        y=y_pos;
        color=c;
    }
    void set(int x_pos, int y_pos)
    {
        x=x_pos;
        y=y_pos;
    }
    int getc()
    {
        return color;
    }
    int getx()
    {
        return x;
    }
    int gety()
    {
        return y;
    }
    void display();
};
void Piece::display()
{
    int a=getc();
    int x=getx();
    int y=gety();
    if(a==0)
    {
        gotoxy(4*x+21,2*y+6);
        SetConsoleTextAttribute( GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_RED );
        cout<<"O";
        SetConsoleTextAttribute( GetStdHandle(STD_OUTPUT_HANDLE), 15);
    }
    else if(a==1)
    {
        gotoxy(4*x+21,2*y+6);
        SetConsoleTextAttribute( GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | 2);
        cout<<"X";
        SetConsoleTextAttribute( GetStdHandle(STD_OUTPUT_HANDLE), 15);
    }
    /*else if(a==2)//the piece has not been set so this outputs 0 on the default cordinates
    {
        gotoxy(4*x+21,2*y+5);
        SetConsoleTextAttribute( GetStdHandle(STD_OUTPUT_HANDLE), BACKGROUND_BLUE | FOREGROUND_INTENSITY | 7);
        cout<<"0";
        SetConsoleTextAttribute( GetStdHandle(STD_OUTPUT_HANDLE), 15);
    }*/
}
class Board
{
private:
    Piece list[64];
    int x1, y1, h;
    int status, redpiece, greenpiece, winner;
    int stat(int h);
    int inRange(int x, int y);
    int pieceAtLoc(int x, int y);
    int validPlacement(int x, int y);
    void flip(int x, int y);
    void display();
    void move();
public:
    Board();
    void execute();
};
Board::Board()
{
    list[1].setposition(4,4,1);
    list[2].setposition(5,4,0);
    list[3].setposition(4,5,0);
    list[4].setposition(5,5,1);
    char bb=186;
    char jj=205;
    system("Color 9");
    for(int j=0; j<9; j++)// = on the grid
    {
        for(int i=0; i<8; i++)
        {

            gotoxy(4*i+25,2*j+7);
            cout<<jj;
        }
    }
    for(int j=0; j<8; j++)// || on the grid
    {
        for(int i=0; i<9; i++)
        {
            gotoxy(4*i+23,2*j+8);
            cout<<bb;
        }
    }
    SetConsoleTextAttribute( GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | 6);
    for(int i=1; i<9; i++)//creates the numbers for the x-axis
    {
        gotoxy(4*i+21,6);
        cout<<i;
    }
    for(int i=1; i<9; i++)//creates the numbers for the y-axis
    {
        gotoxy(21,2*i+6);
        cout<<i;
    }
    SetConsoleTextAttribute( GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_RED );
    gotoxy(58,7);
    cout<<"2 of Player O pieces";
    SetConsoleTextAttribute( GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | 2);
    gotoxy(58,9);
    cout<<"2 of Player X pieces";
    SetConsoleTextAttribute( GetStdHandle(STD_OUTPUT_HANDLE), 15);
    gotoxy(35,4);
    cout<<"Othello";
    gotoxy(30,5);
    cout<<"By: Ephraim Moges";
}
int Board::stat(int h)
{
    gotoxy(7,9);
    cout<<"Status";
    if(h==1)
    {
        SetConsoleTextAttribute( GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | 2);// X turn
        gotoxy(5,12);
        cout<<"||     ||";
        gotoxy(5,13);
        cout<<" ||   ||";
        gotoxy(5,14);
        cout<<"  || ||  ";
        gotoxy(5,15);
        cout<<"   |||    ";
        gotoxy(5,16);
        cout<<"  || ||   ";
        gotoxy(5,17);
        cout<<" ||   || ";
        gotoxy(5,18);
        cout<<"||     ||";
        gotoxy(5,19);
        cout<<"          ";
        gotoxy(62,13);
        cout<<"Cordinates";
        gotoxy(63,15);
        cout<<"(   ,   )";
    }
    else
    {
        SetConsoleTextAttribute( GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_RED );// O turn
        gotoxy(5,12);
        cout<<"   ||||  ";
        gotoxy(5,13);
        cout<<"  ||  ||";
        gotoxy(5,14);
        cout<<" ||    ||";
        gotoxy(5,15);
        cout<<"||      ||";
        gotoxy(5,16);
        cout<<"||      || ";
        gotoxy(5,17);
        cout<<" ||    ||";
        gotoxy(5,18);
        cout<<"  ||  || ";
        gotoxy(5,19);
        cout<<"   ||||";
        gotoxy(62,13);
        cout<<"Cordinates";
        gotoxy(63,15);
        cout<<"(   ,   )";
    }
    SetConsoleTextAttribute( GetStdHandle(STD_OUTPUT_HANDLE), 15);
}
int Board::inRange(int x, int y)
{
    if(((x>0) && (x<9)) && ((y>0) && (y<9))) //on the grid
    {
        return 1;
    }
    else
    {
        return 0;
    }
}
int Board::pieceAtLoc(int x, int y)
{
    int i=1;
    while(i!=0)
    {
        if((list[i].getx()==x) && (list[i].gety()==y))
        {
            return 1;
            i=0;
        }
        else if(list[i].getc()==2)
        {
            return 0;
            i=0;
        }
        i++;
    }

}
int Board::validPlacement(int x, int y)
{
    SetConsoleTextAttribute( GetStdHandle(STD_OUTPUT_HANDLE), 4);
    cout<<"b";
    SetConsoleTextAttribute( GetStdHandle(STD_OUTPUT_HANDLE), 15);
}
void Board::move()
{

}
void Board::display()
{
    for(int i=1; i<65; i++)
    {
        list[i].display();
    }
}
void Board::execute()
{
    int b=1;
    int x=0;
    int y=0;
    gotoxy(62,11);
    cout<<"PASS:( )";
    while(b!=0)
    {
        int p=1;
        int an=1;
        two:
        if(b==1)
        {
            while(an==1)
            {
                display();
                if(p%2==0)
                {
                    stat(1);
                }
                else
                {
                    stat(0);
                }
                gotoxy(68,11);
                cin>>an;//0 means no and 1 means yes
                p++;
            }
            b=2;
        }
        else if(b==2)
        {
            while(inRange(x,y)==0)
            {
                gotoxy(65,15);
                cin>>x;
                gotoxy(69,15);
                cin>>y;
                if(inRange(x,y)==0)
                {
                    gotoxy(65,16);
                    cout<<"Again!";
                    gotoxy(65,15);
                    cout<<" ";
                    gotoxy(69,15);
                    cout<<" ";
                }
            }
            b=3;
        }
        else if(b==3)
        {
            if(pieceAtLoc(x,y)==1)
            {
                SetConsoleTextAttribute( GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | 2);// X turn
                gotoxy(65,20);
                cout<<"done";
                SetConsoleTextAttribute( GetStdHandle(STD_OUTPUT_HANDLE), 15);
                b=2;
                goto two;
            }
            else if(pieceAtLoc(x,y)==0)
            {
                b=4;
            }
        }
        else if(b==4)
        {
            SetConsoleTextAttribute( GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | 2);// X turn
            gotoxy(65,20);
            cout<<"done";
            SetConsoleTextAttribute( GetStdHandle(STD_OUTPUT_HANDLE), 15);
            b=0;
            /*if(valid)
            {

            }*/
        }

    }
    SetConsoleTextAttribute( GetStdHandle(STD_OUTPUT_HANDLE), 9);
    gotoxy(23,25);
    cout<<" ";
}
int main()
{
    Board b;
    b.execute();
    Piece p;
    p.display();
}
I'm not on Windows, so I can't really see what's wrong, but I think you'll be much better off using the continue keyword rather than goto.
They should be if instead of else if

1
2
3
4
5
6
7
8
int b = 1;

while ( b ! = 0 )
{
  if ( b == 1 ) { std::cout << "One ";   b = 3; }
  if ( b == 2 ) { std::cout << "Two ";   b = 0; }
  if ( b == 3 ) { std::cout << "Three "; b = 2; }
}


Probably better to use a switch statement.
Last edited on
If your goto doesn't work, I have one nice advice: don't use goto. If it works, don't use goto either.

Goto is not used too often, because it's considered bad programming practice. Why? Because its nature makes code that consists of goto operations harder to read. If you write really big program, it can be nearly impossible to get an idea of what is going on with gotos.

Good news: you can replace any goto you want with good design of your program. Try focusing on using conditionals, loops, etc. in such way so that you don't have to use goto. Really, these could not exist and it would be ok. Gotos are not mandatory, but they are annoying.

I also don't use windows, but couldn't it be because pieceAtLoc() returned 0?
Topic archived. No new replies allowed.