# C++ console input handling



## burnthepc (Aug 12, 2007)

I think it best if I start explaining what I'm trying to do.

I started off making a cat and mouse program with some AI learning involved. I'm now trying to add a human player to this setup using the keyboard.

The critical problem is that if I do something like

char c = cin.get();
if(c== UP) 
if(c==DOWN)
if(c==LEFT)
if(c==RIGHT)

This will pause the program to wait for input. I want it to skip over this section if there's no new input from the istream.

What I need is something that can check the cin stream and read the last item and clear it.
If the stream is clear I'd just like it to pass over and run the rest of the program logic loop.

Is there a simple way to do this?


----------



## TheRobatron (Oct 25, 2007)

What is your program actually doing? Are you trying to make it dynamically interactive (i.e the program carries on doing stuff while waiting for input and then when it gets input, it handles it)?

I found this article on 'multithreading' that might help.


----------



## burnthepc (Aug 12, 2007)

Sort of dynamically interactive.

I built a maze and put a cat and mouse actors in it. The two actors in the maze pick movements based on a simple probability table. Thus every program cycle I simply pick two random moves.

I thought about adding a human actor into the mix, more for fun. I could impliment it by demanding input before continuing a cycle. (But that's simple and there's nothing to learn there.) I was trying to get it to run by taking input as and when given. Sort of like in that old game frogger, you press the button and it moves, you don't and the frog stays still.

It's just for fun though, if there's no simple solution then it's probably not that useful in the long run.


----------



## TheRobatron (Oct 25, 2007)

I'm not sure that there's an easy way to do this in the console window. Can you post your code? I'd be interested to see the program and fiddle about with it


----------



## burnthepc (Aug 12, 2007)

The code's a bit long and not that well documented at the moment: 
The header pretty much explains the setup though - and I was good and documented that:

class Mouse
{
public:

Mouse() {xcoord = 0; ycoord = 0;}
void move(); // moves random direction
int x_loc() {return xcoord;}
int y_loc() {return ycoord;}

private:

int xcoord;
int ycoord;
bool up(); // side effect alters coord
bool down(); // bool reports if move valid
bool left(); // bool used in move funct
bool right();

};

class Cat
{
public:

Cat() {xcoord = 5; ycoord = 5; totalmoves = 0;}
void move(Mouse& ms ); // moves cat using memory index
int x_loc() {return xcoord;}
int y_loc() {return ycoord;}
void load_mem(); // opens and reads "mem.txt" in home directory
void save_mem(); // saves "mem.txt"

private:

int xcoord;
int ycoord;
int lastmove; // used for reinforcing. Added to mem.txt when mouse caught
int totalmoves; // track total moves in this round. Cosmetic
bool up(); // same as mouse
bool down();
bool left();
bool right();
bool towards_x(Mouse& ms ); // moves towards mouse
bool towards_y(Mouse& ms ); //

vector<int> memory; // for storing moves - changes over successive runs

};

// report if this run is over. Stops program loop.
bool caught(Mouse ms, Cat ct) {return (ms.x_loc() == ct.x_loc()) && (ms.y_loc() == ct.y_loc());}


----------



## kwmd (Mar 27, 2008)

I always used to use kbhit() to check to see if any keystrokes had occurred, then read in what the key was.


----------



## burnthepc (Aug 12, 2007)

That looks very promising. Using something like:

if(kbhit)
{
char c = cin.get()
}

I'll give it a try, thanks!


----------



## blaqDeaph (Nov 22, 2005)

Just put it into a loop, and feed the getchar() function with a return ('\n') first (don't clear it first) that way it'll run without pausing until a key is pressed to change the value of getchar(). Then save the key somewhere else and feed getchar() with a '\n' again.


----------



## waseemsafder (Nov 10, 2006)

Consider that program it will continuesly print the ascii char from keyboard??
while(1){
int a=getch();
if(a!=27){

cout<<a;
}


}


----------

