wait for a keyboard input without breaking the loop

i want the loop go on but not breaking down the loop.

that is actually a snake game, i don't want to keep pressing the key in order to make it move. How to make that happen?

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
  void input()
{
	system("stty raw");

	char key;
	key = getchar();
	{
		switch(key)
		{
			case 'a':
				dir = LEFT;
				break;
			case 'w':
				dir = UP;
				break;
			case 'd':
				dir = RIGHT;
				break;
			case 's':
				dir = DOWN;
				break;
			case 'x':
				gameover = true;	
		}
	}

	

}
void logic()
{	
	switch (dir)
	{
		case LEFT:
			x --;
			break;
		case UP:
			y --;
			break;
		case RIGHT:
			x ++;
			break;
		case DOWN:
			y ++;
			break;
		default:
			break;			  
	}

	system("stty cooked");
Hi,

Could I ask you to keep your other post going? It's about the same subject / code. The trouble is you might get replies to both of them, with respondents saying the same things (perhaps not realising the other thread even exists) - which is ultimately a waste of time for the respondents - especially if someone does a long detailed reply.

There is also potential for there to be different but similar code posted in each topic, which is another source of confusion, and ultimately a time waster as well.

I am not trying to be really stern or anything, it's just some good advice for the future :+)

Cheers
okok, I am so sorry about that. i have been struggled with this problem for few day. just hoping this problem can be solved as soon as possible. i appreciate everyone's help. Thank you. i will be careful about that next time
I am guessing you are trying to do a snake game in the console? I am not sure how to do this without any libraries that handle events, so what I would do is learn SDL or SFML and use their event handlers, also, I would just do the whole thing with GUI because... why not? But if you insist on doing it in the console, you can still use the event handler libraries from a graphics library.

http://lazyfoo.net/tutorials/SDL/04_key_presses/index.php
http://www.sfml-dev.org/tutorials/2.3/window-events.php
I should of said something about suggesting the OP to directing people to not reply to this thread, reply to this one instead:

http://www.cplusplus.com/forum/beginner/170624/
You need to check to see if a key was pressed.

http://www.cplusplus.com/forum/general/64802/#msg350595

Enjoy!
Topic archived. No new replies allowed.