Why does the program freeze

The program is still not finished but i dont know why it freezes...

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
#include <iostream>
#include <conio.h>
#include <ctime>
#include <cstdlib>

using namespace std;

int boards();
int play();

char board[100];
char player = 'G';
char trap = 'T';
char finish = 'X';
char course = '=';
int counting=-1;

int main()
{
    boards();
    play();
    return 0;
}

int play()
{
    do
    {
        char key;
        key = _getch();
        for(int x = 0;x<100;x++)
        {
            counting++;
            if(board[x]=='G')
            {
                break;
            }
        }
        while(key!='w' && key!='a' && key!='s' && key!='d')
        {
            key = _getch();
        }
        if(key=='w')
        {
            while(counting-10<0)
            {
                key = _getch();
            }
            if(board[counting-10]==trap)
            {
                system("CLS");
                cout<<"You loose"<<endl;
            }
            else if(board[counting-10]==finish)
            {
                system("CLS");
                cout<<"You win"<<endl;
            }
            board[counting]=course;
            board[counting-10]=player;
        }
        else if(key=='a')
        {
            while(counting-1<0)
            {
                key = _getch();
            }
            if(board[counting-1]==trap)
            {
                system("CLS");
                cout<<"You loose"<<endl;
            }
            else if(board[counting-1]==finish)
            {
                system("CLS");
                cout<<"You win"<<endl;
            }
            board[counting]=course;
            board[counting-1]=player;
        }
        else if(key=='s')
        {
            while(counting+10>0)
            {
                key = _getch();
            }
            if(board[counting+10]==trap)
            {
                system("CLS");
                cout<<"You loose"<<endl;
            }
            else if(board[counting+10]==finish)
            {
                system("CLS");
                cout<<"You win"<<endl;
            }
            board[counting]=course;
            board[counting+10]=player;
        }
        else if(key=='d')
        {
            while(counting+1>0)
            {
                key = _getch();
            }
            if(board[counting+1]==trap)
            {
                system("CLS");
                cout<<"You loose"<<endl;
            }
            else if(board[counting+1]==finish)
            {
                system("CLS");
                cout<<"You win"<<endl;
            }
            board[counting]=course;
            board[counting+1];
        }
    }while(board[99]=='X');
}


int boards()
{
    srand(time(0));

    board[0]=player;
    board[99]=finish;
    for(int i = 1;i<=98;i++)
    {
        int chance = rand()%10+1;
        if(chance<=2)
        {
            board[i] = 'T';
        }
        else if(chance>2)
        {
            board[i] = '=';
        }
    }
    cout<<board[0]<<" "<<board[1]<<" "<<board[2]<<" "<<board[3]<<" "<<board[4]<<" "<<board[5]<<" "<<board[6]<<" "<<board[7]<<" "<<board[8]<<" "<<board[9]<<endl;
    cout<<board[10]<<" "<<board[11]<<" "<<board[12]<<" "<<board[13]<<" "<<board[14]<<" "<<board[15]<<" "<<board[16]<<" "<<board[17]<<" "<<board[18]<<" "<<board[19]<<endl;
    cout<<board[20]<<" "<<board[21]<<" "<<board[22]<<" "<<board[23]<<" "<<board[24]<<" "<<board[25]<<" "<<board[26]<<" "<<board[27]<<" "<<board[28]<<" "<<board[29]<<endl;
    cout<<board[30]<<" "<<board[31]<<" "<<board[32]<<" "<<board[33]<<" "<<board[34]<<" "<<board[35]<<" "<<board[36]<<" "<<board[37]<<" "<<board[38]<<" "<<board[39]<<endl;
    cout<<board[40]<<" "<<board[41]<<" "<<board[42]<<" "<<board[43]<<" "<<board[44]<<" "<<board[45]<<" "<<board[46]<<" "<<board[47]<<" "<<board[48]<<" "<<board[49]<<endl;
    cout<<board[50]<<" "<<board[51]<<" "<<board[52]<<" "<<board[53]<<" "<<board[54]<<" "<<board[55]<<" "<<board[56]<<" "<<board[57]<<" "<<board[58]<<" "<<board[59]<<endl;
    cout<<board[60]<<" "<<board[61]<<" "<<board[62]<<" "<<board[63]<<" "<<board[64]<<" "<<board[65]<<" "<<board[66]<<" "<<board[67]<<" "<<board[68]<<" "<<board[69]<<endl;
    cout<<board[70]<<" "<<board[71]<<" "<<board[72]<<" "<<board[73]<<" "<<board[74]<<" "<<board[75]<<" "<<board[76]<<" "<<board[77]<<" "<<board[78]<<" "<<board[79]<<endl;
    cout<<board[80]<<" "<<board[81]<<" "<<board[82]<<" "<<board[83]<<" "<<board[84]<<" "<<board[85]<<" "<<board[86]<<" "<<board[87]<<" "<<board[88]<<" "<<board[89]<<endl;
    cout<<board[90]<<" "<<board[91]<<" "<<board[92]<<" "<<board[93]<<" "<<board[94]<<" "<<board[95]<<" "<<board[96]<<" "<<board[97]<<" "<<board[98]<<" "<<board[99]<<endl;
}
Last edited on
$ g++ -W{all,extra,pedantic} foo.cpp
|| foo.cpp: In function ‘int play()’:
foo.cpp|117 col 29| warning: statement has no effect [-Wunused-value]
||              board[counting+1];
foo.cpp|120 col 1| warning: no return statement in function returning non-void [-Wreturn-type]

|| foo.cpp: In function ‘int boards()’:
foo.cpp|151 col 1| warning: no return statement in function returning non-void [-Wreturn-type]

you should resolve those warnings.

Running through a debugger (user input is marked with >)
$ gdb a.out
(gdb) run
Starting program: /datos/ne555/documentos/informatica/test/forum/a.out 
G = = = = T T = = T
= = = T T T = = T T
= T T = = = = = T =
= = = T T T = T T =
= = = T T = T = = =
= = = = T = = T = T
= = = = = = = = = =
= = = = = = T = = =
= = = T = = = = = =
= = = T = T = = = X
> d
> <C-c>
Program received signal SIGINT, Interrupt.
0x00007ffff7275510 in __read_nocancel () from /usr/lib/libc.so.6
(gdb) backtrace
#0  0x00007ffff7275510 in __read_nocancel () from /usr/lib/libc.so.6
#1  0x00007ffff720dad0 in __GI__IO_file_underflow () from /usr/lib/libc.so.6
#2  0x00007ffff720ebd2 in __GI__IO_default_uflow () from /usr/lib/libc.so.6
#3  0x00007ffff72095b0 in getc () from /usr/lib/libc.so.6
#4  0x00007ffff7b3edfd in __gnu_cxx::stdio_sync_filebuf<char, std::char_traits<char> >::syncgetc (
    this=0x7ffff7dd6680 <__gnu_internal::buf_cin_sync>)
    at /build/gcc/src/gcc-build/x86_64-pc-linux-gnu/libstdc++-v3/include/ext/stdio_sync_filebuf.h:225
#5  __gnu_cxx::stdio_sync_filebuf<char, std::char_traits<char> >::uflow (
    this=0x7ffff7dd6680 <__gnu_internal::buf_cin_sync>)
    at /build/gcc/src/gcc-build/x86_64-pc-linux-gnu/libstdc++-v3/include/ext/stdio_sync_filebuf.h:141
#6  0x00007ffff7b4c826 in std::basic_streambuf<char, std::char_traits<char> >::sbumpc (this=<optimized out>)
    at /build/gcc/src/gcc-build/x86_64-pc-linux-gnu/libstdc++-v3/include/streambuf:325
#7  std::istream::get (this=0x6022c0 <std::cin@@GLIBCXX_3.4>)
    at /build/gcc/src/gcc-build/x86_64-pc-linux-gnu/libstdc++-v3/include/bits/istream.tcc:247
#8  0x0000000000400a49 in _getch () at foo.cpp:24
#9  0x0000000000400d5e in play () at foo.cpp:104
#10 0x0000000000400a34 in main () at foo.cpp:21
(gdb) frame 9
#9  0x0000000000400d5e in play () at foo.cpp:104
104	                key = _getch();
(gdb) list
99	        }
100	        else if(key=='d')
101	        {
102	            while(counting+1>0)
103	            {
104	                key = _getch();
105	            }
106	            if(board[counting+1]==trap)
107	            {
108	                system("CLS");
There you see
102
103
104
105
while(counting+1>0)
{
    key = _getch();
}
there is nothing in the body of the loop that may change the condition, so infinite loop. It will ask over and over again for input


By the way, ¿what were you thinking when writting lines 141--150?
you can use a loop there.
Last edited on
Topic archived. No new replies allowed.