cout

this is really wierd and i need help. here is my code.
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
#include <iostream>


void fillBoard(int board[3][3]);
void printBoard(int board[3][3]);
void evaluate(int board[3][3], int sums[8]);
void translate(int board[3][3], int choice);
void step1(int board[3][3], int sums[]);
void step2(int board[3][3], int sums[]);
void step3(int board[3][3], bool &mid);
void step4(int board[3][3], int sums[], bool mid, bool &blockedCheck);

int main () {

		int nums[3][3]={}, sums[8]={}, choice;
		bool win=false, mid=false, blockedCheck=false;
		
		fillBoard(nums);
		
		while (win==false) {
		
				printBoard(nums);
	
				std::cin >> choice;
		
				translate(nums, choice);
		
				evaluate(nums, sums);
				std::cout << "\nStarting step 1\n";
				step1(nums, sums);
				std::cout << "\nEnding step 1 \n starting step 2"; 
				step2(nums, sums);
				std::cout << "\nEnding step 2 \n starting step 3"; 
				step3(nums, mid);
				std::cout << "\nEnding";
				std::cout << " step 3* starting step 4"; 
				step4(nums, sums, mid, blockedCheck);
				std::cout << "\nEnding step 4"; 
		}
		
		return 0;

}

void fillBoard(int board[3][3]){
		
		for (int i = 0; i < 3; i++){
				for (int j = 0; j < 3; j++){
						
						board[i][j]=1;
						
				}
		}
		
}

void printBoard(int board[3][3]){

		for (int a = 0; a < 3; a++){
				std::cout << '\n';
				for (int b =0; b<3; b++){
						std::cout << board[a][b] << "  ";
						
				}
		}
		
}

void evaluate(int board[3][3], int sums[8]){

		std::cout<<"\n\n";
		
		sums[0]=board[0][0]+board[0][1]+board[0][2];
		
		sums[1]=board[1][0]+board[1][1]+board[1][2];
		
		sums[2]=board[2][0]+board[2][1]+board[2][2];
		
		sums[3]=board[0][0]+board[1][0]+board[2][0];
		
		sums[4]=board[0][1]+board[1][1]+board[2][1];
		
		sums[5]=board[0][2]+board[1][2]+board[2][2];
	
		sums[6]= board[0][0]+board[1][1]+board[2][2];
		
		sums[7]= board[2][0]+board[1][1]+board[0][2];
		
		for (int i = 0; i<8; i++) {
				std::cout << sums[i] << "  ";
		}
		
		std::cout << '\n';

		
}

void translate(int board[3][3], int choice){

		switch (choice) {
				case 1:
						board[0][0]=10;
						break;
				case 2:
						board[0][1]=10;
								break;
				case 3:
						board[0][2]=10;
						break;
				case 4:
						board[1][0]=10;
						break;
				case 5:
						board[1][1]=10;
						break;
				case 6:
						board[1][2]=10;
						break;
				case 7:
						board[2][0]=10;
						break;
				case 8:
						board[2][1]=10;
						break;
				case 9:
						board[2][2]=10;
						break;
				default:
						break;
		}
		
}

void step1(int board[3][3], int sums[]){
		
		for (int a = 0; a<8; a++){
				if (sums[a]==11){
				//row 1 = sum[0], column 1 = sum[3], diag1=sum[6]
						for (int i = 0; i < 3; i++){
								switch (a) {
										case 0:
												if (board[0][i]==1)
														board[0][i]=5;
												break;
										case 1:
												if (board[1][i]==1)
														board[1][i]=5;
												break;
										case 2:
												if (board[2][i]==1)
														board[2][i]=5;
												break;
										case 3:
												if(board[i][0]==1)
														board[i][0]=5;
												break;
										case 4:
												if (board[i][1]==1) {
														board[i][1]=5;
												}
												break;
										case 5:
												if (board[i][2]==1)
														board[i][2]=5;
												break;
										case 6:
												if(board[0][0]==1)
														board[0][0]=5;
												if(board[1][1]==1)
														board[1][1]=5;
												if (board[2][2])
														board[2][2]=5;
												break;
										case 7:
												if(board[0][2]==1)
														board[0][2]=5;
												if(board[1][1]==1)
														board[1][1]=5;
												if (board[2][0])
														board[0][2]=5;
												break;

		

										default:
												break;
								}
								
						}
				}
		}
}

void step2(int board[3][3], int sums[]){
		if (board[1][1]==1) {
				board[1][1]=5;
		}
}

void step3(int board[3][3], bool &mid){
		if (board[1][1]==1) {
				board[1][1]=5;
				mid=true;
		}
}

void step4(int board[3][3], int sums[], bool mid, bool &blockedCheck){
		
		while (blockedCheck==false){
				if (mid==true) {
						
						if(board[0][0]==10 && board[2][2]==10){
								
								if (board[0][1]==1) {
										board[0][1]=5;
								}
								blockedCheck=true;
						}
				
						if (board[0][2]==10 && board[2][0]==10) {
								
								if (board[0][1]==1) {
										board[0][1]=5;
								}
								blockedCheck=true;
						}
				}
		
		
		}
		
		
}

here is the output

[Session started at 2012-05-16 14:13:55 -0400.]
GNU gdb 6.3.50-20050815 (Apple version gdb-1461.2) (Fri Mar  5 04:43:10 UTC 2010)
Copyright 2004 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB.  Type "show warranty" for details.
This GDB was configured as "x86_64-apple-darwin".tty /dev/ttys000
Loading program into debugger…
sharedlibrary apply-load-rules all
Program loaded.
run
[Switching to process 369]
Running…

1  1  1  
1  1  1  
1  1  1  1


12  3  3  12  3  3  12  3  
Starting step 1

Ending step 1 
 starting step 2
Ending step 2 
 starting step 3
kill
quit

The Debugger has exited with status 0.
[Session started at 2012-05-16 14:16:37 -0400.]
GNU gdb 6.3.50-20050815 (Apple version gdb-1461.2) (Fri Mar  5 04:43:10 UTC 2010)
Copyright 2004 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB.  Type "show warranty" for details.
This GDB was configured as "x86_64-apple-darwin".tty /dev/ttys001
Loading program into debugger…
sharedlibrary apply-load-rules all
Program loaded.
run
[Switching to process 411]
Running…

1  1  1  
1  1  1  
1  1  1  5


3  12  3  3  12  3  12  12  

Starting step 1

Ending step 1 
 starting step 2
Ending step 2 
 starting step 3

it appears that the program is stopping mid cout and i have no idea why this happens. help me please.
it's possible more text has been sent to cout, but cout did not display it onscreen yet because it wasn't flushed. Do not rely on cout statements alone to tell you where a program is hanging.

This is a perfect opportunity to learn how to use the debugger.

Set a breakpoint on the call to step3 and step through the code and find out what the program is doing.

I suspect it's hanging inside of your step4 function because that loop is very iffy. If 'mid' is false, or if your board isn't arranged just so, that loop will deadlock your program.
i have tried the breaks with the debugger. the problem is it stops in the middle of a cout function, not in the middle of a loop. that is the issue and im not sure why that happens. so it does not even get to the step 4 function. PS this is a friends code so im not the familiar with the logic of it.
You have an endless loop in step 4, not quite sure why you have a loop there in any case. Get rid of it and the program should run.
Topic archived. No new replies allowed.