Consol crash using Graphics.h

Hi!
So basically everything goes okey, no crashes. It display what is asked. But when I create a loop which can only be broken by the user, it crashes after 6 seconds (the timing is always the same). During that time everything looks fine and respond correctly. This is the part of code that cause problems:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <graphics.h>

using namespace std;

int main()
{
    int gd = DETECT, gm;
    initgraph(&gd, &gm, "C:\\tc\\bgi");
    bool turn = true;
    while(turn){

    }
    getch();
    closegraph();
    return 0;
}


Using Code::Blocks.
Any suggestions to prevent craches?
Thanks in advance !
Last edited on
1
2
3
    while(true){

    }


This loop. How are you expecting it to end?
did it crash, with an error code, or did it just lock up and sit there doing nothing? Because you told it to sit there, doing nothing, forever. That isn't a crash or even a lockup or hang, because it was intentional.
Sorry, I miss typed it. It suppose to be "turn". But still it does't change the problem. User will press the button " x" on keyboard which changes "turn" value to false and then it would close.
What happens is that it works fine for 6 seconds. Then it just stops responding and then it crashes with an error code.
Last edited on
If needed, I could give full code that I wrote
Show us the actual code that reproduces what you are witnessing.
If your code is pages upon pages long, don't show that. Trim it down until you only have the essential parts that still reproduce the issue.
Last edited on
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

#include <iostream>
#include <graphics.h>
#include <windows.h>
#include <conio.h>

using namespace std;

bool gameOver;
enum eDirection {STOP = 0, UP, END};
eDirection dir;
int l = 100, t = 250, r = 150, b = 300;

void setup(){
    gameOver = false;
    dir = STOP;
    line(0, 300, 700, 300);
    rectangle(l, t, r, b);
}
void draw(){
    line(0, 300, 700, 300);
    rectangle(l, t, r, b);
}
void imput(){
    if(_kbhit()){
        switch(_getch()){
            case 'w':
                dir = UP;
                break;
            case 'x':
                dir = END;
                break;
        }
    }
}
void logic(){
    switch(dir){
        case UP:
            for(int i = 30; i < 120; i += 30){
                t -= i;
                b -= i;
                line(0, 300, 700, 300);
                rectangle(l, t, r, b);
                Sleep(70);
                cleardevice();
            }
            for(int i = 30; i < 120; i += 30){
                t += i;
                b += i;
                line(0, 300, 700, 300);;
                rectangle(l, t, r, b);
                Sleep(70);
                cleardevice();
            }
            dir = STOP;
            break;
        case END:
            gameOver = true;
            break;
    }
}
int main()
{
    int gd = DETECT, gm;
    initgraph(&gd, &gm, "C:\\tc\\bgi");
    setup();
    while(!gameOver){
        imput();
        cout << dir << endl;
        logic();
        draw();
        Sleep(70);
        cleardevice();
    }
    getch();
    closegraph();
    return 0;
}


It is like a simple dino game that you pres "W" to avoid objects that come at you. It responds on keyoard hit and goes up and down. But again.. Console stops responding after 6 seconds.
I solved the problem. I changed initgraph to initwindow, deleted getch() and closegraph() in main, removed " int gd = DETECT, gm; " and replaced libbgi.a file.
Topic archived. No new replies allowed.