Return EXIT_SUCCESS not executing.

So I've got an assignment that is due in a few days and the code itself works perfectly. However, when it comes to exiting the program, EXIT_SUCCESS is being ignored. I know the code block with EXIT_SUCCESS in it is running because the cout statement above it displays, but the program will not exit without a hard Alt F4. Can anyone suggest how to make EXIT_SUCCESS do what it's supposed to? Sorry for the long code block.

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
#include "GameConsts.h"

using namespace conio;


bool gameRunning;

bool levelRunning;

struct ConioChar
{
 
    unsigned char ch;

    COLORS bgColour;

    COLORS textColour;

    int x;

    int y;

    int lastX;

    int lastY;

    bool visible;
};

ConioChar enemy;

const int ENEMY_ROW = SCREEN_ROWS - 2;

ConioChar plane;

const int PLANE_ROW = 2;

ConioChar bomb;

const int BOMB_ROW = PLANE_ROW + 1;

const int GROUND_ROW = ENEMY_ROW + 1;


int bombHit = 0;
int bombMiss = 0;


bool introduction();

void initGameData();

void drawGameScreen();
// get the key presses
void handleEvents();

void update();

void draw();


bool check4Win();

bool playAgain();

void drawChar( ConioChar cch );

int main()
{

    srand( time( NULL ) );

    gameRunning = ( introduction() );

    while ( gameRunning )
    {
        initGameData();

        drawGameScreen();

        levelRunning = true;
        while( levelRunning )
        {

            handleEvents();

            update();

            draw();

            if ( check4Win() )
            {

            }
            Sleep( 32 );
        }

        gameRunning = ( playAgain() );
    }

    cout << setbk( BLACK ) << setclr( WHITE ) << setxy( 25, 15 ) << "Thanks for playing!";
    kbhit();
    cout << clrscr;
    return EXIT_SUCCESS;
}

void initGameData()
{

    enemy = { 'E', BLUE, WHITE, 1, ENEMY_ROW, 1, ENEMY_ROW, true };

    plane = { 'W', BLUE, YELLOW, SCREEN_COLS / 2, 1, SCREEN_COLS / 2, 1, true };

    bomb = { 'B', BLUE, YELLOW, 0, 0, 0, 0, false };
}


void handleEvents()
{
    if( kbhit() )
    {
        unsigned char ch = getch();
        if( ch == ARROW_KEY )
        {
            ch = getch();
            if( ch == KEY_LEFT && plane.x > 1 )
            {
                plane.lastX = plane.x;
                plane.x -= 1;
            }
            else if ( ch == KEY_RIGHT && plane.x < SCREEN_COLS )
            {
                plane.lastX = plane.x;
                plane.x += 1;
            }
        }
        else if ( ch == SPACE && bomb.visible == false )
        {
            bomb.visible = true;
            bomb.lastY = bomb.y = plane.y + 1;
            bomb.lastX = bomb.x = plane.x;
        }
    }
}

void update()
{
    enemy.lastX = enemy.x;
    if( enemy.x < SCREEN_COLS )
    {
        enemy.x += 1;
    }
    else
    {
        enemy.x = 1;
    }
    if( bomb.visible && bomb.y <= ENEMY_ROW )
    {
        bomb.lastY = bomb.y;
        bomb.y += 1;
        if( bomb.y >= ENEMY_ROW - 1 &&  bomb.x >= enemy.x - 1 && bomb.x <= enemy.x + 1  )
        {
            enemy.visible = false;
            bomb.visible = false;
            ++bombHit;
            playAgain();

        }
        if ( bomb.y == ENEMY_ROW )
        {
            bomb.visible = false;
            ++bombMiss;
        }
    }
}


void draw()
{
    drawChar( enemy );
    drawChar( plane );
    drawChar( bomb );
}


void drawGameScreen()
{
    cout << setbk( RED ) << clrscr;
    drawRectangle( BLUE, 1, 1, SCREEN_COLS, SCREEN_ROWS - 2 );
    drawRectangle( GREEN, 1, SCREEN_ROWS - 1, SCREEN_COLS, 1 );
}

void drawChar( ConioChar cch )
{
    if( cch.lastX != cch.x || cch.lastY != cch.y )
    {
        cout << setxy( cch.lastX, cch.lastY ) << setbk( cch.bgColour ) << " ";
        cch.lastX = cch.x;
        cch.lastY = cch.y;
    }

    if( cch.visible )
    {
        cout << setbk( cch.bgColour ) << setclr( cch.textColour );
        cout << setxy( cch.x, cch.y ) << cch.ch;
    }
}

bool check4Win()
{
    return false;
}

bool playAgain()
{
    cout << setbk( WHITE ) << setclr( RED ) << setxy( 22, 19 ) << "You asploded the enemy!";
    cout << setbk( WHITE ) << setclr( RED ) << setxy( 22, 20 ) << "Hits: " << bombHit << " Misses: " << bombMiss;
    cout << setbk( WHITE ) << setclr( RED ) << setxy( 22, 21 ) << "Press Y to play again: ";
    char ch = getch();
    if( toupper( ch ) == 'Y' )
    {
        drawGameScreen();
        enemy.visible = true;
        return true;
    }
    else
    {
        cout << setbk( WHITE ) << setclr( RED ) << setxy( 25, 15 ) << "Thanks for playing!";
        kbhit();
        return 0;
    }
}

bool introduction()
{
    cout << setbk( BLUE ) << clrscr;
    cout << setclr( WHITE );
    cout << setxy( 32, 12 ) << "The Introduction";
    char ch;
    do
    {
        cout << setclr( YELLOW ) << setxy( 26, 22 ) << "Do you want to Play? Y or N";
        ch = getch();
        ch = toupper( ch );
    }
    while( ch != 'N' && ch != 'Y' );

    if( ch == 'Y' )
    {
        return true;
    }
    else
    {
        return 0;
    }
}
#include <cstdio>
Can anyone suggest how to make EXIT_SUCCESS do what it's supposed to?


EXIT_SUCCESS doesn't "do" anything, it's just a macro expanding to a value (usually 0), so

return EXIT_SUCCESS;

is no more magical than

return 0;.

but the program will not exit without a hard Alt F4.

Having to shut down your program by hand is a telltale sign of an infinite loop -- one of your while() loops never ends.
Last edited on
Sorry Smac, that doesn't do anything.
Last edited on
Thanks Catfish I figured as much, but I can't determine after 3 days of looking WHY the loop is repeating, hence why I'm posting here asking for help.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
        levelRunning = true;
        while( levelRunning )
        {

            handleEvents();

            update();

            draw();

            if ( check4Win() )
            {

            }
            Sleep( 32 );
        }


Question, do you ever set levelRunning = false;?
No, I don't... I'll have to change that.
Topic archived. No new replies allowed.