[HELP] Program shutting down

So this is my code (some of the text is in Dutch, just ignore that):

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
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

using namespace std;

int BootVier, BootDrie, BootTwee, BootEen;
int BootTotaal = BootVier + BootDrie + BootTwee + BootEen;

char input;

char array[10][10] =
{
     {'-', '-', '-', '-', '-', '-', '-', '-', '-', '-'},
     {'-', '-', '-', '-', '-', '-', '-', '-', '-', '-'},
     {'-', '-', '-', '-', '-', '-', '-', '-', '-', '-'},
     {'-', '-', '-', '-', '-', '-', '-', '-', '-', '-'},
     {'-', '-', '-', '-', '-', '-', '-', '-', '-', '-'},
     {'-', '-', '-', '-', '-', '-', '-', '-', '-', '-'},
     {'-', '-', '-', '-', '-', '-', '-', '-', '-', '-'},
     {'-', '-', '-', '-', '-', '-', '-', '-', '-', '-'},
     {'-', '-', '-', '-', '-', '-', '-', '-', '-', '-'},
     {'-', '-', '-', '-', '-', '-', '-', '-', '-', '-'}
};

char array2[10] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'};
int array3[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
int ran1;
int ran2;


void Speelbord ()
{
    cout<<"  0 1 2 3 4 5 6 7 8 9\n";
    cout<<"A "<<array[0][0]<<" "<<array[0][1]<<" "<<array[0][2]<<" "<<array[0][3]<<" "<<array[0][4]<<" "<<array[0][5]<<" "<<array[0][6]<<" "<<array[0][7]<<" "<<array[0][8]<<" "<<array[0][9]<<"\n";
    cout<<"B "<<array[1][0]<<" "<<array[1][1]<<" "<<array[1][2]<<" "<<array[1][3]<<" "<<array[1][4]<<" "<<array[1][5]<<" "<<array[1][6]<<" "<<array[1][7]<<" "<<array[1][8]<<" "<<array[1][9]<<"\n";
    cout<<"C "<<array[2][0]<<" "<<array[2][1]<<" "<<array[2][2]<<" "<<array[2][3]<<" "<<array[2][4]<<" "<<array[2][5]<<" "<<array[2][6]<<" "<<array[2][7]<<" "<<array[2][8]<<" "<<array[2][9]<<"\n";
    cout<<"D "<<array[3][0]<<" "<<array[3][1]<<" "<<array[3][2]<<" "<<array[3][3]<<" "<<array[3][4]<<" "<<array[3][5]<<" "<<array[3][6]<<" "<<array[3][7]<<" "<<array[3][8]<<" "<<array[3][9]<<"\n";
    cout<<"E "<<array[4][0]<<" "<<array[4][1]<<" "<<array[4][2]<<" "<<array[4][3]<<" "<<array[4][4]<<" "<<array[4][5]<<" "<<array[4][6]<<" "<<array[4][7]<<" "<<array[4][8]<<" "<<array[4][9]<<"\n";
    cout<<"F "<<array[5][0]<<" "<<array[5][1]<<" "<<array[5][2]<<" "<<array[5][3]<<" "<<array[5][4]<<" "<<array[5][5]<<" "<<array[5][6]<<" "<<array[5][7]<<" "<<array[5][8]<<" "<<array[5][9]<<"\n";
    cout<<"G "<<array[6][0]<<" "<<array[6][1]<<" "<<array[6][2]<<" "<<array[6][3]<<" "<<array[6][4]<<" "<<array[6][5]<<" "<<array[6][6]<<" "<<array[6][7]<<" "<<array[6][8]<<" "<<array[6][9]<<"\n";
    cout<<"H "<<array[7][0]<<" "<<array[7][1]<<" "<<array[7][2]<<" "<<array[7][3]<<" "<<array[7][4]<<" "<<array[7][5]<<" "<<array[7][6]<<" "<<array[7][7]<<" "<<array[7][8]<<" "<<array[7][9]<<"\n";
    cout<<"I "<<array[8][0]<<" "<<array[8][1]<<" "<<array[8][2]<<" "<<array[8][3]<<" "<<array[8][4]<<" "<<array[8][5]<<" "<<array[8][6]<<" "<<array[8][7]<<" "<<array[8][8]<<" "<<array[8][9]<<"\n";
    cout<<"J "<<array[9][0]<<" "<<array[9][1]<<" "<<array[9][2]<<" "<<array[9][3]<<" "<<array[9][4]<<" "<<array[9][5]<<" "<<array[9][6]<<" "<<array[9][7]<<" "<<array[9][8]<<" "<<array[9][9]<<"\n";
}



char inputGen()
{
     cin>> input;
                      
     if (input == 'o' || input == 'x')
     {
               array[ran1][ran2] = input;
     }
     else
     {
               cout<<"Sorry, dat is geen correcte input, probeer het opnieuw:";
               inputGen();
     }
}
char random ()
{
     
     for(int i = 0; i < 100; i++)
     {
             
     srand ( (unsigned)time(NULL) );
     ran1 = rand()%10;
     ran2 = rand()%10;
     
     if (array[ran1][ran2] == '-')
     {
           cout<<array2[ran1]<<array3[ran2]<<":\n";
           
           inputGen();
                
     }
     else
     {
         random();
     }
     Speelbord();
     }
};


char inputBeginC2()
{
     cout<<"c2:\n";
     
     cin>> input;
     if (input == 'o' || input == 'x')
     {
              array[2][2] = input;
     }
     else
     {
         cout<<"Sorry, dat is geen correcte input, probeer het opnieuw";
         inputBeginC2();
     }
}

char inputBeginC7()
{
     cout<<"c7:\n";
     
     cin>> input;
     if (input == 'o' || input == 'x')
     {
              array[2][7] = input;
     }
     else
     {
         cout<<"Sorry, dat is geen correcte input, probeer het opnieuw";
         inputBeginC7();
     }
}

char inputBeginH2()
{
     cout<<"h2:\n";
     
     cin>> input;
     if (input == 'o' || input == 'x')
     {
              array[7][2] = input;
     }
     else
     {
         cout<<"Sorry, dat is geen correcte input, probeer het opnieuw";
         inputBeginH2();
     }
}

char inputBeginH7()
{
     cout<<"h7:\n";
     
     cin>> input;
     if (input == 'o' || input == 'x')
     {
              array[7][7] = input;
     }
     else
     {
         cout<<"Sorry, dat is geen correcte input, probeer het opnieuw";
         inputBeginH7();
     }
}

void begin ()
{
     Speelbord ();

     inputBeginC2();              
     Speelbord ();
    
     inputBeginC7();
     Speelbord ();
     
     inputBeginH2();
     Speelbord ();
    
     inputBeginH7();
     Speelbord ();

}

int main()
{
    begin();
    random();
    Speelbord();
    cin.ignore();
    cin.get();
    

}


But everytime I run it, it lets me do a couple of things, then it shuts down, and a Windows message pops up and says "<filename>.exe stopped working".

Why does it do this?
First of all all of your functions do not return a value.
Second use srand () once during your application lifetime.

Try putting cin.sync () after cin >> input in inputGen ().
Last edited on
I get a stack overflow in random(). This is because you call srand(time(0)) before generating ran1 and ran2. During the same second they will always get the same values, so if array[ran1][ran2] != '-' it will continue to call random() for the rest of that second, which is likely to generate a stack overflow.
Last edited on
Thanks for the help guys,
I took awaysrand ( (unsigned)time(NULL) ); from random(),
and I placed it in main(),
now it runs normally, but at the end, after I filled the entire board with o's or x's
I get that message again
When the board is filed array[ran1][ran2] == '-' is always false so random() will get called, and called until the program crashes.
Last edited on
Wouldn't it just be an infinite loop then, if that's the case, then I wouldn't be able to do anything after begin(), because that's where random() gets called.
Then it would crash at the beginning of random(), not that the end of it.
There is nothing wrong in begin(). The problem is in random(). It can't find a free position so random() gets called until the program crashes, as I said. It is similar to an infinite loop but each function call will need some stack space to store things like variable values so at some point it will run out of stack space and the program crashes.
When the board is filed array[ran1][ran2] == '-' is always false

I don't actually understand why this is the case?
When the board is filled all positions are either 'x' or 'o'. There no possibility to find a '-' anywhere.
oh... I get it now, silly me. Hey, thanks a lot

EDIT: Do you have a suggestion on how to fix this
Last edited on
Topic archived. No new replies allowed.