I have a (probably stupid) question about the input buffer.

I want to do something while there is no space pressed. But if nothing is pressed, it should be doing it too.

The problem is that by using getch(), the script will keep waiting for an input of the user.

So I was wondering if there is the possibility to add a character to the input buffer, so the script won't pause at the getch() line.

I have tried all kind of stuff. Here is a shortened version of my code:

char c;
do
{
   if(stdin==NULL)
      c='d';
   else
      c=getch();
} while(c!=' ');

But nothing get's me closer to a solution. What should I do?

Hi.
I will try to help you but i do not get it what you want in your program.What goal your program have?

Basicly it is a program the show a sort of movie. Just some dots (representing people) moving around. It should be paused when space is pressed, and continue to play when space is pressed again.
But the dots can make one move at a time.

You mean:

do {
.....
} while (kbhit() != ' ');

There is no standard way to accomplish that. But there are at least a couple non-standard ways. (1) use kbhit() from conio.h -- but the problem here is that conio.h is non-standard and may not be supported by your compiler so we normally discourage its use. (2) create another thread for keyboard input so that the primary thread can do something while waiting for input.

I tried the kbhit but it doesn't stop when I press the space.
I did it like this

#include <stdio.h>
#include <conio.h>

int main(void)
{
	do
	{
	     printf("test");
	} while(kbhit()!=' ');

	return 0;
}

But it doesn't work :(. How do I do the other solution? :s

I see .
Try something like this to put in your loop.

if (  kbhit () == '  '  )
system ( "pause" );

I got it!

#include <conio.h>

int main(void)
{
	char c;
	do
	{
		printf("test");
		if(kbhit())
			c=getch();
		else
			c='d';

	} while(c!=' ');
	return 0;
}

kbhit() doesn't return the character, it only says if there is a character in the inputbuffer. So I have to do an if function with getch();

All thanks for your help. If you didn't help me, I would probably be crying the whole next week :).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.