Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » How bad is a goto statement?

This thread is locked; no one can reply to it. rss feed Print
 1   2   3   4 
How bad is a goto statement?
Sirocco
Member #88
April 2000
avatar

I think the reasonable response is to say that GOTO is but one tool of many in a programmer's kit. A good programmer knows when and how often to implement the tools at his/her disposal; that is, it's there if you feel the situation warrants it.

I've used a handful of GOTO statements in the past, and I'm sure at some point in the future it will crop up once more. I've larger things to worry about ;)

-->
Graphic file formats used to fascinate me, but now I find them rather satanic.

orz
Member #565
August 2000

I think that the anti-goto rhetoric is largely a leftover from the bygone era when many programers learned to program in asm or basic where gotos and jmps were natural. People who started with C or higher level languages usually use higher level flow control primitives because, for most code, they're easier to write, maintain, and think in.

Quote:

Most of Allegro's goto usage is for error handling. Given C's lack of exceptions, there's not always a good alternative.

That's what longjmp() is for.
(/me is mostly joking)

edit: first paragraph was edited for clarity

kosmitek
Member #8,151
December 2006

I try change this code as you say (it is also in ATTACHMENT) - but when I changed it wrong run - for example if I press "M" then pawn doesn't run BUT when I next press "->" then it goes 2 times !

1 
2#include<iostream>
3#include<ctime>
4#include <allegro.h>
5using namespace std;
6 
7 
8 
9 
10 
11class player
12{
13 private:
14 int money;
15 int health;
16 int weapon;
17 int power;
18
19
20 public:
21 player(int a, int b, int c, int d)
22 {
23 money=a;
24 health=b;
25 weapon=c;
26 power=d;
27 }
28
29 int change_money(int x)
30 {
31 money=money+x;
32 return money;
33 }
34
35 int change_health(int x)
36 {
37 health=health+x;
38 return health;
39 }
40
41
42 int change_weapon(int x)
43 {
44 weapon=weapon+x;
45 return weapon;
46 }
47
48
49 int change_power(int x)
50 {
51 power=power+x;
52 return power;
53 }
54
55~player() {}
56
57};
58 
59 
60 
61 
62 
63void start_game(BITMAP *board, BITMAP *pawn, BITMAP *buffer)
64{
65
66int tab1[2][10]={
67 {100,220,370,500,700,700,500,350,220,100},
68 {100,100,100,100,100,340,340,340,340,340},
69 };
70 
71int tab2[10]={0,1,2,3,4,5,6,7,8,9};
72 
73int *index=&tab2[0];
74int *zero=&tab2[0];
75int *nine=&tab2[9];
76int module;
77int eyes;
78srand (time(0));
79int i=21;
80int y=1;
81 
82player game(99, 99, 99, 99);
83 
84while(!key[KEY_ESC]&&i!=0)
85{
86i--;
87eyes = 1+rand()%3;
88clear_to_color(buffer,makecol(255,255,255));
89blit(board,buffer,0,0,0,0,board->w,board->h);
90blit(pawn,buffer,0,0,tab1[0][*index],tab1[1][*index],pawn->w,pawn->h);
91textprintf_ex(buffer, font, 267, 580, makecol(0, 0, 0), -1, "You have: %d money, %d health, %d weapon and %d power.", game.change_money(y), game.change_money(y), game.change_weapon(y), game.change_power(y));
92textprintf_ex(buffer, font, 267, 600, makecol(0, 0, 0), -1, "You throw: %d, it left yo %d movements", eyes, i);
93textprintf_ex(buffer, font, 267, 620, makecol(0, 0, 0), -1, "-> - left, <- - right");
94textprintf_ex(buffer, font, 267, 640, makecol(0, 0, 0), -1, "ESC - the end of the game");
95blit(buffer,screen,0,0,0,0,SCREEN_W,SCREEN_H);
96
97 
98 
99 
100direction:
101readkey();
102if (key[KEY_LEFT])
103{
104if(*index>=0&&*index<=4)
105 {
106 if ((*index-eyes)>=*zero)
107 {
108 index=index-eyes;
109 }
110 else
111 {
112
113 index=nine-(eyes-*index-1);
114 }
115
116 }
117else if(*index>=5&&*index<=9)
118 
119 {
120 if ((*index+eyes)<=*nine)
121 {
122 index=index+eyes;
123 }
124 else
125 {
126 module=eyes-(*nine-*index);
127 if (module<0)
128 {
129 module=-module;
130 }
131 index=zero+module-1;
132 }
133
134 }
135}
136 
137 
138 
139
140else if (key[KEY_RIGHT])
141{
142 if(*index>=0&&*index<=4)
143 {
144 index=index+eyes;
145
146 }
147 else
148 {
149 index=index-eyes;
150 }
151
152
153}
154
155
156
157else
158{
159 if (!key[KEY_ESC])
160 {
161 goto direction;
162 }
163
164}
165 
166 
167 
168}
169 
170delete game;
171
172
173}
174 
175 
176 
177 
178 
179 
180int main()
181{
182BITMAP *board = NULL;
183BITMAP *pawn = NULL;
184BITMAP *buffer = NULL;
185allegro_init();
186install_keyboard();
187set_color_depth(32);
188set_gfx_mode(GFX_AUTODETECT,1024,768,0,0);
189buffer=create_bitmap(SCREEN_W,SCREEN_H);
190 
191 
192board = load_bitmap("board.bmp",NULL);
193pawn = load_bitmap("pawn.bmp",NULL);
194 
195start_game(board, pawn, buffer);
196 
197 
198destroy_bitmap(board);
199destroy_bitmap(pawn);
200destroy_bitmap(buffer);
201allegro_exit();
202return 0;
203}
204END_OF_MAIN()

after change with following code my program wrong run:

1 
2while(....)
3{
4INSTRUCTION;
5 
6 do {
7 bool repeat = false;
8 if()
9 {}
10 else if ()
11 {}
12 else()
13 {
14 repeat = true;
15 }
16 } while( repeat );
17}

1while(....)
2{
3 INSTRUCTION;
4 do
5 {
6 if()
7 {
8 // blah
9 }
10 else if()
11 {
12 // blah
13 }
14 else()
15 {
16 continue;
17 }
18 } while (FALSE);
19}

also wrong run

1bool breakInsideLoop = false;
2while(...)
3{
4 EXPRESSION;
5 
6 while (!breakInsideLoop)
7 {
8 ...
9 
10 if (...)
11 breakInsideLoop = true;
12 }
13 
14 ... // Now, outside of the "inside" loop
15}

is also wrong run

LennyLen
Member #5,313
December 2004
avatar

If you're using the exact code that you've just put in those last three examples, then no, that won't work. That's just skeleton code to show you examples of how a loop can be done.

You want something like this:

1int done = 0;
2 
3while (!done)
4{
5 
6 if (key[KEY_LEFT])
7 {
8 if(*index>=0&&*index<=4)
9 {
10 if ((*index-eyes)>=*zero)
11 {
12 index=index-eyes;
13 }
14 else
15 {
16 
17 index=nine-(eyes-*index-1);
18 }
19 
20 }
21
22 else if(*index>=5&&*index<=9)
23 {
24 if ((*index+eyes)<=*nine)
25 {
26 index=index+eyes;
27 }
28 else
29 {
30 module=eyes-(*nine-*index);
31 if (module<0)
32 {
33 module=-module;
34 }
35 index=zero+module-1;
36 }
37 
38 }
39
40 }
41 
42 else if (key[KEY_RIGHT])
43 {
44 if(*index>=0&&*index<=4)
45 {
46 index=index+eyes;
47 
48 }
49 else
50 {
51 index=index-eyes;
52 }
53 
54 }
55 
56 else if (key[KEY_ESC]) done = 1;
57 
58}

kosmitek
Member #8,151
December 2006

LennyLen your code also is wrong :) Pawn don't want go and it is reason why I use "goto" here, every other method is wrong.

LennyLen
Member #5,313
December 2004
avatar

I didn't notice you were counting down i to give the player 20 moves.

Here's a version that works exactly like the executable you included. I've restructured the program completely though, and changed parts that could be made simpler.

You were also using the key[] array incorrectly, which is why you were getting multiple rolls each time you pressed a key.

main.cpp:

1#include <iostream>
2#include <ctime>
3#include <allegro.h>
4#include "player.h"
5 
6 
7using namespace std;
8 
9 
10// Global variables
11// lots of them as I was too lazy to restructure the program properly
12BITMAP *board = NULL;
13BITMAP *pawn = NULL;
14BITMAP *buffer = NULL;
15int module, eyes, y = 1, index = 0, moves = 20;
16player game(99, 99, 99, 99);
17 
18 
19// Function Declarations
20void initiate();
21void shutdown();
22void run_game();
23void display_screen();
24 
25 
26// int main()
27int main()
28{
29 initiate();
30 run_game();
31 shutdown();
32 return 0;
33}
34END_OF_MAIN()
35 
36 
37// void initialize() - gets Allegro set up
38void initiate()
39{
40 allegro_init();
41 install_keyboard();
42 
43 set_color_depth(32);
44 if (set_gfx_mode(GFX_AUTODETECT_WINDOWED,1024,768,0,0) != 0)
45 {
46 allegro_message("Error with gfx mode!");
47 exit(EXIT_FAILURE);
48 }
49 
50 buffer=create_bitmap(SCREEN_W,SCREEN_H);
51 
52 board = load_bitmap("board.bmp",NULL);
53 if (!board)
54 {
55 allegro_message("Error loading board.bmp!");
56 exit(EXIT_FAILURE);
57 }
58 
59 pawn = load_bitmap("pawn.bmp",NULL);
60 if (!pawn)
61 {
62 allegro_message("Error loading pawn.bmp!");
63 exit(EXIT_FAILURE);
64 }
65}
66 
67 
68// void shutdown() - frees memory and exits Allegro
69void shutdown()
70{
71 destroy_bitmap(board);
72 destroy_bitmap(pawn);
73 destroy_bitmap(buffer);
74 allegro_exit();
75}
76 
77 
78// void run_game() - does main game loop
79void run_game()
80{
81 
82 srand (time(0));
83 int press;
84 
85 while (moves > 0)
86 {
87 eyes = 1 + rand() % 3;
88 display_screen();
89 
90 press = readkey();
91 
92 if ( (press >> 8) == KEY_LEFT)
93 {
94 if ( (index >= 0) && (index <= 4) )
95 {
96 if ( (index - eyes) >= 0)
97 index = index - eyes;
98 else
99 index = 9 - (eyes - index - 1);
100 }
101 else if ( (index >= 5) && (index <= 9) )
102 {
103 if ( (index + eyes) <= 9)
104 index = index + eyes;
105 else
106 {
107 module = eyes - (9 - index);
108 if (module < 0)
109 module =- module;
110 index = 0 + module - 1;
111 }
112 
113 }
114 moves--;
115 }
116 
117 if ( (press >> 8) == KEY_RIGHT)
118 {
119 if ( (index >= 0) && (index <= 4) )
120 index = index + eyes;
121 else
122 index = index - eyes;
123 moves--;
124 }
125 
126 if ( (press >> 8) == KEY_ESC) break;
127 
128 }
129 
130}
131 
132 
133// void display_screen() - updates and displays the buffer to the screen
134void display_screen()
135{
136 int tab1[2][10]={
137 {100, 220, 370, 500, 700, 700, 500, 350, 220, 100},
138 {100, 100, 100, 100, 100, 340, 340, 340, 340, 340},
139 };
140 
141 clear_to_color(buffer, makecol(255, 255, 255));
142 blit(board, buffer, 0, 0, 0, 0, board->w, board->h);
143 blit(pawn, buffer, 0, 0, tab1[0][index], tab1[1][index], pawn->w, pawn->h);
144 textprintf_ex(buffer, font, 267, 580, makecol(0, 0, 0), -1, "You have: %d money, %d health, %d weapon and %d power.", game.change_money(y), game.change_money(y), game.change_weapon(y), game.change_power(y));
145 textprintf_ex(buffer, font, 267, 600, makecol(0, 0, 0), -1, "You throw: %d, it left you %d movements", eyes, moves);
146 textprintf_ex(buffer, font, 267, 620, makecol(0, 0, 0), -1, "-> - left, <- - right");
147 textprintf_ex(buffer, font, 267, 640, makecol(0, 0, 0), -1, "ESC - the end of the game");
148 blit(buffer, screen, 0, 0, 0, 0, SCREEN_W, SCREEN_H);
149}

player.h:

1#ifndef PLAYER_H
2#define PLAYER_H
3 
4class player
5{
6private:
7 int money;
8 int health;
9 int weapon;
10 int power;
11 
12public:
13 player(int a, int b, int c, int d);
14 int change_money(int x);
15 int change_health(int x);
16 int change_weapon(int x);
17 int change_power(int x);
18 
19};
20 
21#endif // PLAYER_H

player.cpp:

1#include "player.h"
2 
3player::player(int a, int b, int c, int d)
4{
5 money = a;
6 health = b;
7 weapon = c;
8 power = d;
9}
10 
11int player::change_money(int x)
12{
13 money = money + x;
14 return money;
15}
16 
17int player::change_health(int x)
18{
19 health = health + x;
20 return health;
21}
22 
23int player::change_weapon(int x)
24{
25 weapon = weapon + x;
26 return weapon;
27}
28 
29 
30int player::change_power(int x)
31{
32 power = power + x;
33 return power;
34}

kosmitek
Member #8,151
December 2006

Thanks LennyLen, but it is also wrong because:
please start this game, please press "m" or other letter on keyboard 5 or more times and you see that information: "You throw: X " - and that X(number of eyes) it will be change - and X(number of eyes) should change ONLY when somebody press "->" or "<-", when somebody press something else X(number of eyes) shouldn't change - it is a problem which can be solved only when wy use "goto" - that I think.

LennyLen
Member #5,313
December 2004
avatar

This new run_game() function "fixes" the problem of it rolling when other keys are pressed. I'll leave it up to you to figure out how to only display information when M is pressed.

1void run_game()
2{
3 
4 srand (time(0));
5 int press;
6 
7 display_screen();
8 
9 while (moves > 0)
10 {
11 eyes = 1 + rand() % 3;
12 
13 press = readkey();
14 
15 if ( (press >> 8) == KEY_LEFT)
16 {
17 if ( (index >= 0) && (index <= 4) )
18 {
19 if ( (index - eyes) >= 0)
20 index = index - eyes;
21 else
22 index = 9 - (eyes - index - 1);
23 }
24 else if ( (index >= 5) && (index <= 9) )
25 {
26 if ( (index + eyes) <= 9)
27 index = index + eyes;
28 else
29 {
30 module = eyes - (9 - index);
31 if (module < 0)
32 module =- module;
33 index = 0 + module - 1;
34 }
35 
36 }
37 moves--;
38 display_screen();
39 }
40 
41 if ( (press >> 8) == KEY_RIGHT)
42 {
43 if ( (index >= 0) && (index <= 4) )
44 index = index + eyes;
45 else
46 index = index - eyes;
47 moves--;
48 display_screen();
49 }
50 
51 if ( (press >> 8) == KEY_ESC) break;
52 
53 }
54 
55}

BAF
Member #2,981
December 2002
avatar

Quote:

I would think a much better design would be to have exit() call a "cleanup" function of the programmer's choice:

Can't you use atexit() for that?

kosmitek
Member #8,151
December 2006

WOOOW !!!!! Run very good - I must seeing through very attentively - thank you LennyLen :)

--------------------------
ups - pawn after throw bone doesn't go after right fields - I tried change it but only when I use "goto" was okay :(

LennyLen
Member #5,313
December 2004
avatar

Here's a version I rewrote that extends the player class so that it now stores what tile the player is on, as well as the result of the latest roll. I also added a method to reroll the dice. The program now relies less on global variables, and is a little more object oriented.

I don't normally use C++, so there may be more efficient ways of doing things than I what I do.

By the way, I've left the logic of your game untouched, as I'm not 100% certain what you're trying to do. I'm curious as to why there's a lot more code for when the player presses left then for when they press right.

main.cpp:

#SelectExpand
1#include <iostream> 2#include <ctime> 3#include <allegro.h> 4#include "player.h" 5 6using namespace std; 7 8// Global variables 9// lots of them as I was too lazy to restructure the program properly 10BITMAP *board = NULL; 11BITMAP *pawn = NULL; 12BITMAP *buffer = NULL; 13 14 15// Function Declarations 16void initiate(); 17void shutdown(); 18void run_game(); 19void display_screen(player *mike); 20 21// int main() 22int main() 23{ 24 initiate(); 25 run_game(); 26 shutdown(); 27 return 0; 28} 29END_OF_MAIN() 30 31 32// void initialize() - gets Allegro set up 33void initiate() 34{ 35 allegro_init(); 36 install_keyboard(); 37 38 set_color_depth(32); 39 if (set_gfx_mode(GFX_AUTODETECT_WINDOWED,1024,768,0,0) != 0) 40 { 41 allegro_message("Error with gfx mode!"); 42 exit(EXIT_FAILURE); 43 } 44 45 buffer=create_bitmap(SCREEN_W,SCREEN_H); 46 47 board = load_bitmap("board.bmp",NULL); 48 if (!board) 49 { 50 allegro_message("Error loading board.bmp!"); 51 exit(EXIT_FAILURE); 52 } 53 54 pawn = load_bitmap("pawn.bmp",NULL); 55 if (!pawn) 56 { 57 allegro_message("Error loading pawn.bmp!"); 58 exit(EXIT_FAILURE); 59 } 60 61 srand(time(0)); 62} 63 64 65// void shutdown() - frees memory and exits Allegro 66void shutdown() 67{ 68 destroy_bitmap(board); 69 destroy_bitmap(pawn); 70 destroy_bitmap(buffer); 71 allegro_exit(); 72} 73 74 75// void run_game() - does main game loop 76void run_game() 77{ 78 player theplayer(99, 99, 99, 99); 79 80 int module, press; 81 82 display_screen(&theplayer); 83 84 while (theplayer.get_moves() > 0) 85 { 86 87 press = readkey(); 88 89 if ( (press >> 8) == KEY_LEFT) 90 { 91 theplayer.roll_dice(); 92 93 if ( (theplayer.get_tile() >= 0) && (theplayer.get_tile() <= 4) ) 94 { 95 if ( (theplayer.get_tile() - theplayer.get_roll()) >= 0) 96 theplayer.move_to_tile(theplayer.get_tile() - theplayer.get_roll()); 97 else 98 theplayer.move_to_tile(9 - (theplayer.get_roll() - theplayer.get_tile() - 1)); 99 } 100 else if ( (theplayer.get_tile() >= 5) && (theplayer.get_tile() <= 9) ) 101 { 102 if ( (theplayer.get_tile() + theplayer.get_roll()) <= 9) 103 theplayer.move_to_tile(theplayer.get_tile() + theplayer.get_roll()); 104 else 105 { 106 module = theplayer.get_roll() - (9 - theplayer.get_tile()); 107 if (module < 0) 108 module =- module; 109 theplayer.move_to_tile(0 + module - 1); 110 } 111 112 } 113 display_screen(&theplayer); 114 } 115 116 if ( (press >> 8) == KEY_RIGHT) 117 { 118 theplayer.roll_dice(); 119 120 if ( (theplayer.get_tile() >= 0) && (theplayer.get_tile() <= 4) ) 121 theplayer.move_to_tile(theplayer.get_tile() + theplayer.get_roll()); 122 else 123 theplayer.move_to_tile(theplayer.get_tile() - theplayer.get_roll()); 124 display_screen(&theplayer); 125 } 126 127 if ( (press >> 8) == KEY_ESC) break; 128 129 } 130 131} 132 133 134// void display_screen() - updates and displays the buffer to the screen 135void display_screen(player *mike) 136{ 137 int tab1[2][10]={ 138 {100, 220, 370, 500, 700, 700, 500, 350, 220, 100}, 139 {100, 100, 100, 100, 100, 340, 340, 340, 340, 340}, 140 }; 141 142 clear_to_color(buffer, makecol(255, 255, 255)); 143 blit(board, buffer, 0, 0, 0, 0, board->w, board->h); 144 blit(pawn, buffer, 0, 0, tab1[0][mike->get_tile()], tab1[1][mike->get_tile()], pawn->w, pawn->h); 145 textprintf_ex(buffer, font, 267, 580, makecol(0, 0, 0), -1, "You have: %d money, %d health, %d weapon and %d power.", mike->get_money(), mike->get_health(), mike->get_weapon(), mike->get_power()); 146 textprintf_ex(buffer, font, 267, 600, makecol(0, 0, 0), -1, "You throw: %d, it left you %d movements", mike->get_roll(), mike->get_moves()); 147 textprintf_ex(buffer, font, 267, 620, makecol(0, 0, 0), -1, "-> - left, <- - right"); 148 textprintf_ex(buffer, font, 267, 640, makecol(0, 0, 0), -1, "ESC - the end of the game"); 149 blit(buffer, screen, 0, 0, 0, 0, SCREEN_W, SCREEN_H); 150}

player.h:

#SelectExpand
1#ifndef PLAYER_H 2#define PLAYER_H 3 4class player 5{ 6private: 7 int money; 8 int health; 9 int weapon; 10 int power; 11 int moves; 12 int roll; 13 int tile; 14 15public: 16 player(int a, int b, int c, int d); 17 void roll_dice(); 18 void move_to_tile(int x); 19 int get_money(); 20 int get_health(); 21 int get_weapon(); 22 int get_power(); 23 int get_moves(); 24 int get_roll(); 25 int get_tile(); 26 int change_money(int x); 27 int change_health(int x); 28 int change_weapon(int x); 29 int change_power(int x); 30 31}; 32 33#endif // PLAYER_H

player.cpp:

#SelectExpand
1#include <ctime> 2#include <iostream> 3#include "player.h" 4 5player::player(int a, int b, int c, int d) 6{ 7 money = a; 8 health = b; 9 weapon = c; 10 power = d; 11 moves = 20; 12 roll = 0; 13 tile = 0; 14} 15 16void player::roll_dice() 17{ 18 roll = 1 + rand() % 3; 19 moves--; 20} 21 22void player::move_to_tile(int x) 23{ 24 tile = x; 25} 26 27int player::get_money() 28{ 29 return money; 30} 31 32int player::get_health() 33{ 34 return health; 35} 36 37int player::get_weapon() 38{ 39 return weapon; 40} 41 42int player::get_power() 43{ 44 return power; 45} 46 47int player::get_moves() 48{ 49 return moves; 50} 51 52int player::get_roll() 53{ 54 return roll; 55} 56 57int player::get_tile() 58{ 59 return tile; 60} 61 62int player::change_money(int x) 63{ 64 money = money + x; 65 return money; 66} 67 68int player::change_health(int x) 69{ 70 health = health + x; 71 return health; 72} 73 74int player::change_weapon(int x) 75{ 76 weapon = weapon + x; 77 return weapon; 78} 79 80 81int player::change_power(int x) 82{ 83 power = power + x; 84 return power; 85}

kosmitek
Member #8,151
December 2006

LennyLen somewhere is little mistake - pawn after throw bone doesn't go after right fields - I tried change it but only when I use "goto" was okay :(

LennyLen
Member #5,313
December 2004
avatar

Quote:

LennyLen somewhere is little mistake - pawn after throw bone doesn't go after right fields - I tried change it but only when I use "goto" was okay

The program I wrote behaves exactly as the one you wrote, so if there's a problem, it's in your logic. I've attached both your original program (I recompiled it to use the dynamic library so it was smaller) and the version I wrote so you can see for yourself that they behave the same.

TeamTerradactyl
Member #7,733
September 2006
avatar

Gee...

This sounds almost like the program I provided earlier to you, kosmitek, before you added the "player" class. Can't for the LIFE of me figure out why that code couldn't be used... It was re-written to be very understandable, use the exact same keys, handle the exact same logic...

Why not just extend what was already written for you and use that? It's not as if the code doesn't compile...

LennyLen, I don't know if you saw that earlier post or not (in the other thread, no less), but you may be able to use it. If not... shrug.

kosmitek
Member #8,151
December 2006

TeamTerradactyl yes, but I didn't use your earlier program because he also was wrong :)
In my program when I throw for example 3, pawn go 3 fields and in yours programs pawn is strange going - somewhere in your programs is little mistake, I must use "goto" in your program and only then pawn going right.

Paul Pridham
Member #250
April 2000
avatar

OK... you guys are posting about how GOTO is ugly and hard to read, and then post this as some sort of counter-example:

for(multiset<CObject*, SSortObjects>::iterator it = CObjectList::o().begin();it != CObjectList::o().end();) // loop through STL-multiset (could be any other container as well)
      if ((*it)->get_x() + (*it)->get_w() < CCamera::o().get_x()) // check, if some statement about the object is true and delete it
      {
        it = CObjectList::o().erase(it); // delete one object from the container
      }
      else
        it++;

::) :D

I mostly use GOTOs for function-level error handling. Setting a flag, checking it in multiple points to break a loop/nest is slower and less efficient than a goto.

Bob
Free Market Evangelist
September 2000
avatar

Quote:

You guys are posting about how GOTO is ugly and hard to read, and then post this as some sort of counter-example:

Indeed. That code should be:

typedef std::multiset<CObject*, SSortObjects> myset_t;

bool condition(myset_t::value_type &v) {
    return (v.get_x() + v.get_w() < CCamera::o().get_x());
}

myset_t &myset = CObjectList::o();

myset.erase(std::remove_if(myset.begin(), myset.end(), condition), myset.end());

Somewhat cleaner now, although it's artificially lengthened by the additional typedefs and statement splittings.

--
- Bob
[ -- All my signature links are 404 -- ]

kosmitek
Member #8,151
December 2006

I think in my program must be "goto", without "goto" my program isn't run good.

LENNY LEN - YOUR IDEA WITH 3 files: main.cpp; player.h; player.cpp, one table; and globall variables ARE WONDERFUL - WITHOUT THAT I MUSTED (MUST IN THE PAST ?) WRITE EVERYTHING IN ONE BIG FUNCTION AND NOW I HAVE MANY LITTLE FUNCTIONS - THANK YOU - THIS IDEA IS VERYYYYY GOOD - THANK YOU LENNY LEN FOR YOUR IDEA - I USE YOUR IDEA (but I had use "goto") AND I ADD GLOBAL TABLE - ONLY ONE TABLE !!! - THANKS YOU !!!!!!!!! YOU ARE WONDERFUL - THANK YOU !!!!! ;D;D

Simon Parzer
Member #3,330
March 2003
avatar

Quote:

MUST IN THE PAST ?

had to

Example: Because I forgot to turn off capslock I had to slap myself in the face several times.

LennyLen
Member #5,313
December 2004
avatar

Quote:

LennyLen, I don't know if you saw that earlier post or not (in the other thread, no less), but you may be able to use it. If not... shrug.

Yeah I saw it. Most of my code was actually from when I rewrote the program last time kosmitek posted about it a few weeks ago.

Quote:

TeamTerradactyl yes, but I didn't use your earlier program because he also was wrong
In my program when I throw for example 3, pawn go 3 fields and in yours programs pawn is strange going

The real problem was that you didn't explain very well what you were trying to do, and it's hard to tell from your code exactly what you are doing.

Now that I'm pretty certain that I know what you're trying to do, I've changed the logic to reflect this. I've attached the working code (the changes were to the run_game() function) and an executable. There's still no need for goto.

edit:

Don't rely too much on global variables. They certainly have their uses, but if you have too many of them, it makes the program harder to follow and less modular. There's usually a more elegant solution.

ImLeftFooted
Member #3,935
October 2003
avatar

I think gotos can add much needed clarity to complex operations and I honestly think they are the shit. goto Failure is about as clear as you can get. There are a number of other completely valid and wonderfully clear uses of gotos.

The ability for a language feature to be misused does not render the feature obsolete.

kosmitek
Member #8,151
December 2006

now it runs ideal WITHOUT "GOTO" - LENNY LEN YOU ARE GENIUS !!!!!!

BAF
Member #2,981
December 2002
avatar

Your shift key is sticking again. :-/

FMC
Member #4,431
March 2004
avatar

Thanks for the pretty code Bob, i didn't know you could handle it that way!

What i previously did:

//fl...ake is a vector iterator of the same type as snow
//snow is a vector
for(fl = snow.begin(); fl!=snow.end(); fl++){
    if(fl->dead()){
        snow.erase(fl);
        fl--;
        }
}

Is this wrong or bug prone?

[FMC Studios] - [Caries Field] - [Ctris] - [Pman] - [Chess for allegroites]
Written laws are like spiders' webs, and will, like them, only entangle and hold the poor and weak, while the rich and powerful will easily break through them. -Anacharsis
Twenty years from now you will be more disappointed by the things that you didn't do than by the ones you did do. So throw off the bowlines. Sail away from the safe harbor. Catch the trade winds in your sails. Explore. Dream. Discover. -Mark Twain

CGamesPlay
Member #2,559
July 2002
avatar

Quote:

Is this wrong or bug prone?

Yes, fl is invalid when it is erased: you can't increment or decrement. In practice, however, it will work for vectors so long as you are not erasing the final element.

--
Tomasu: Every time you read this: hugging!

Ryan Patterson - <http://cgamesplay.com/>

 1   2   3   4 


Go to: