an alternate key word for getch

i was trying to make a tic tac toe game where the user can play against the computer
it s in borland c++ and not in c++ 11 so i wanted to convert it into c++11
my program shows error since there is nothing called conio.h in c++11
hence i want to know if there is any alternate keyword gor the function getch()
so that i could ask the user to press [ENTER].

HERE IS THE CODE
I COULD NOT CORRECT IT
PLEASE GUYS I WANT TO SEE IT WORKING
AND IM NOT USED TO GRAPHICS AND WOULD LOVE TO USE IT IN THE PROGRAM
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
#include<process.h>
#include<stdlib.h>
#include<string.h>
#include<iostream.h>
#include<conio.h>
int usbox,i,j,nandu=0,result,cusb,cobox,r,d;
int playl=0;
char copybox[10];
char box[10]={'1','2','3','4','5','6','7','8','9'};
int gene()
{
 int loop;
 for(loop=0;loop<9;loop++)
 {
  if(box[loop]!='X'|| box[loop]!='O')
  break;
 }
return loop;
}
int checkwin(int a,char *copy);
void interface()
{
 clrscr();
  cout<<"\n\n\t\t\t TIC TAC TOE [CROSS N\' KNOTS]";
  cout<<"\n\t\t\t-----------------------------";
 cout<<"\n\n\t\t\tPLAYER 1 [O]        PLAYER 2 [X]\n\n";

 cout<<"\t\t\t            |      |    "<<endl;
 cout<<"\t\t\t        "<<box[0]<<"   |  "<<box[1]<<"   |  "<<box[2]<<"  "<<endl;
 cout<<"\t\t\t      ______|______|______"<<endl;
 cout<<"\t\t\t            |      |    "<<endl;
 cout<<"\t\t\t        "<<box[3]<<"   |  "<<box[4]<<"   |  "<<box[5]<<"  "<<endl;
 cout<<"\t\t\t      ______|______|______"<<endl;
 cout<<"\t\t\t            |      |"<<endl;
 cout<<"\t\t\t        "<<box[6]<<"   |  "<<box[7]<<"   |  "<<box[8]<<"  "<<endl;                                       //
 cout<<"\t\t\t            |      |"<<endl;                                                                             // [x] comp
}

void singleplayerc()              //GAME PLAY  COMP 1ST USER NEXT COMP [O] USER [X]
{randomize();
 for(playl=0;playl<9;playl++)
{ interface();
  if(playl%2!=0)
  {
	us:
	cout<<"ENTER A BOX NO : USER [X] : ";
	cin>>usbox;
	if(usbox<10 && usbox>0 && box[usbox-1]!='O' && box[usbox-1]!='X')
	  {
		box[usbox-1]='X';
		result=checkwin(usbox,box);
		if(result==5)
		{
		 nandu=-1;
		 goto end;
		}
	  }
	  else
		{cout<<"invalid move ! \n";goto us;}
  }

 else
 {
	for(i=0;i<9;i++)
	{
	 strcpy(copybox,box);
	 if(copybox[i]!='X' && copybox[i]!='O')
	 {
	  copybox[i]='O';
	  result=checkwin(i+1,copybox);
	  if(result==5)
	  {
		box[i]='O';
		nandu=1;
		goto end;
	  }
	 }
	}
  if(i==9)
  {
	for(j=0;j<9;j++)
	{
	 strcpy(copybox,box);
	 if(copybox[j]!='X' && copybox[j]!='O')
	 {
	  copybox[j]='X';
	  result=checkwin(j+1,copybox);
	  if(result==5)
	  {
		box[j]='O';
		goto end;
	  }
	 }
	}
  }
  if(j==9)                                   // FORGING
  {
	 if(playl==0)
	 {
	  cobox=2*(random(5));
	  box[cobox]='O';
	 }
	 else if(playl==2)
	 {
	  if(cobox!=4)
	  {
		if(usbox==5)
		{
		 switch(cobox)
		 {
		  case 0:cobox=8;break;
		  case 2:cobox=6;break;
		  case 6:cobox=2;break;
		  case 8:cobox=0;break;
		 }
		 box[cobox]='O';
		}
		else if(usbox%2==0)
		{ d=cobox-usbox;
		 if(d==1 || d==-1)
		 {
		  switch(cobox)
		  {
			case 0:cobox=6;break;
			case 2:cobox=8;break;
			case 6:cobox=0;break;
			case 8:cobox=2;break;
		  }
		  box[cobox]='O';
		 }
		 if(d==3||d==-3)
		 {
		  switch(cobox)
		  {
			case 0:cobox=2;break;
			case 2:cobox=0;break;
			case 6:cobox=8;break;
			case 8:cobox=6;break;
		  }
		  box[cobox]='O';
		 }
		}
		else if(cobox-usbox==3||cobox-usbox==-3||cobox-usbox==1||cobox-usbox==-1)
		{
		 switch(cobox)
		  {
			case 0:cobox=8;break;
			case 2:cobox=6;break;
			case 6:cobox=2;break;
			case 8:cobox=0;break;
		  }
		  box[cobox]='O';
		}
		else if(cobox-usbox==4||cobox-usbox==-4||cobox-usbox==8||cobox-usbox==-8)
		{
		 switch(cobox)
		  {
			case 0:cobox=6;break;
			case 2:cobox=8;break;
			case 6:cobox=0;break;
			case 8:cobox=2;break;
		  }
		  box[cobox]='O';
		}
		else
		{cobox=gene();
		 box[cobox]='O';
		}
	  }
	  else
	  {
		if(usbox%2==0)
		{
		 cobox=2*(random(5));
		 box[cobox]='O';
		}
		else if(usbox%2!=0)
		{
		 switch(usbox)
		 {
		  case 1:cobox=8;break;
		  case 3:cobox=6;break;
		  case 7:cobox=2;break;
		  case 9:cobox=0;break;
		 }
		 box[cobox]='O';
		}
		else
		{
		 cobox=gene();
		 box[cobox]='O';
		}
	  }
	 }
	 else if(playl==4)
	 {
	  if(usbox%2==0)
	  {
		switch(cobox)
		{
		 case 0:cobox=8;break;
		 case 2:cobox=6;break;
		 case 6:cobox=2;break;
		 case 8:cobox=0;break;
		}
		box[cobox]='O';
		result=checkwin(cobox+1,box);
		if(result==5)
		{
		 nandu=1;
		 goto end;
		}
	  }
	 }
	 else
	 {
     cobox=gene();
	  box[cobox]='O';
	 }
	}
   

  }

 

end:
	 switch(nandu)
	  {
		case 1:interface();
				cout<<"computer won the match";exit(0);
				break;
		case -1:interface();
				 cout<<"user won the match";exit(0);
				 break;
	  }
}// end => for loop
if(playl==9)
{interface();
 cout<<"game tied";
}
}  //end of function single player c
void main()
{
 singleplayerc();
}


int checkwin(int a,char *copy)
{int win=0;
 switch(a)
{
 case 1:   if(copy[0]==copy[1]&&copy[1]==copy[2])
				 win=5;
			  if(copy[0]==copy[3]&&copy[3]==copy[6])
				 win=5;
			  if(copy[0]==copy[4]&&copy[4]==copy[8])
				 win=5;break;
 case 2:   if(copy[1]==copy[4]&&copy[4]==copy[7])
				 win=5;
			  if(copy[0]==copy[1]&&copy[1]==copy[2])
				 win=5;break;
 case 3:   if(copy[0]==copy[1]&&copy[1]==copy[2])
				 win=5;
			  if(copy[2]==copy[4]&&copy[4]==copy[6])
				 win=5;
			  if(copy[2]==copy[5]&&copy[5]==copy[8])
				 win=5;break;
 case 4:   if(copy[0]==copy[3]&&copy[3]==copy[6])
				 win=5;
			  if(copy[3]==copy[4]&&copy[4]==copy[5])
				 win=5;break;
 case 5:   if(copy[6]==copy[4]&&copy[4]==copy[2])
					win=5;
			  if(copy[1]==copy[4]&&copy[4]==copy[7])
					win=5;
			  if(copy[3]==copy[4]&&copy[4]==copy[5])
					win=5;
			  if(copy[0]==copy[4]&&copy[4]==copy[8])
					win=5; break;
 case 6:   if(copy[2]==copy[5]&&copy[5]==copy[8])
				 win=5;
			  if(copy[5]==copy[4]&&copy[4]==copy[3])
				 win=5;break;
 case 7:   if(copy[0]==copy[3]&&copy[3]==copy[6])
				 win=5;
			  if(copy[6]==copy[7]&&copy[7]==copy[8])
				 win=5;
			  if(copy[6]==copy[4]&&copy[4]==copy[2])
				 win=5;break;
 case 8:   if(copy[1]==copy[4]&&copy[4]==copy[7])
				 win=5;
			  if(copy[6]==copy[7]&&copy[7]==copy[8])
				 win=5;break;
 case 9:   if(copy[2]==copy[5]&&copy[5]==copy[8])
				 win=5;
			  if(copy[6]==copy[7]&&copy[7]==copy[8])
				 win=5;
			  if(copy[0]==copy[4]&&copy[4]==copy[8])
				 win=5;break;
}

return win;
}
@keskiverto
Methinks you misread the question. OP wants to use getch() but has no getch() function.

@N10
What you are looking for is called unbuffered input. The way to turn buffering (and echo) off is different for each system.

You will also need a replacement for kbdhit(): iskeypressed().

Windows

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#ifndef NOMINMAX
#define NOMINMAX
#endif
#include <windows.h>

void raw( bool b )
{
  const DWORD mbits = ENABLE_ECHO_INPUT | ENABLE_LINE_INPUT | ENABLE_PROCESSED_INPUT;
  DWORD mode;
  HANDLE hStdIn = GetStdHandle( STD_INPUT_HANDLE );
  GetConsoleMode( hStdIn, &mode );
  if (b) mode &= ~mbits;
  else   mode |=  mbits;
  SetConsoleMode( hStdIn, mode );
}

bool iskeypressed( unsigned timeout_ms = 0 )
{
  return WaitForSingleObject(
    GetStdHandle( STD_INPUT_HANDLE ),
    timeout_ms
    ) == WAIT_OBJECT_0;
}

Everything else (Linux, Mac OS X, etc)

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
#include <unistd.h>
#include <termios.h>
#include <poll.h>

// All commentary stripped out for size.
// See <http://www.lafn.org/~dave/linux/terminalIO.html> for details.

void raw( bool b )
  {
  struct termios settings;
  static struct termios initial_settings;
  static bool is_initialized = false;

  if (!is_initialized)
    {
    is_initialized = tcgetattr( STDIN_FILENO, &initial_settings ) == 0;
    if (!is_initialized) return;
    }

  if (b)
    {
    tcgetattr( STDIN_FILENO, &settings );
    settings.c_cc[ VTIME ] = 0;
    settings.c_cc[ VMIN  ] = 1;
    settings.c_iflag &= ~(BRKINT | ICRNL | INLCR | ISTRIP | IXOFF);
    settings.c_iflag |=   IGNBRK;
    settings.c_oflag &= ~(OPOST);
    settings.c_cflag &= ~(CSIZE | PARENB);
    settings.c_cflag |=   CS8;
    settings.c_lflag &= ~(ECHO | ICANON | IEXTEN | ISIG);
    }
  else
    settings = initial_settings;

  tcsetattr( STDIN_FILENO, TCSANOW, &settings );
  }

bool iskeypressed( unsigned timeout_ms = 0 )
{
  struct pollfd pls[ 1 ];
  pls[ 0 ].fd     = STDIN_FILENO;
  pls[ 0 ].events = POLLIN | POLLPRI;
  return poll( pls, 1, timeout_ms ) > 0;
}

Now you can write yourself replacement functions:

1
2
3
4
5
6
7
8
9
10
11
12
bool kbdhit()
{
  return iskeypressed();
}

int getch()
{
  raw( true );
  int result = cin.get();
  raw( false );
  return result;
}

Note -- these aren't exactly the same as the old <conio.h> stuff... You cannot read, for example, arrow keys with this.

(Also, I haven't tested these incarnations of the functions -- I may have typoed or otherwise made some dumb mistake. If you have a problem, post back.)

Hope this helps.
Topic archived. No new replies allowed.