vForums Support > Programming & Coding :: Programming Discussion :: > Question on _getch()

Question on _getch() - Posted By lordotaku (lordotaku) on 9th Mar 08 at 7:24pm
Hey everyone, I am working on a project in which a character moves about a room. (This is only console output with ASCII characters)

I already have the code which generates the room and the character set up, and am now currently working on my movement functions.

To retrieve the value of the key pressed for the movement, I am using _getch(), and then using that value for a switch statement to determine which move function to call (up, down, left, right).

However, do I need to clear the buffer after using the _getch()?
If so, how exactly could I clear it?
Thanks for any help in advance. {Smile}

Oh and I am coding in C++

Re: Question on _getch() - Posted By Marc (cr0w) on 9th Mar 08 at 11:46pm
Not sure about how to clear the buffer in C++, but I believe you do need to; otherwise the image will be drawn in the new place without removing where it was previously, causing a big line. {Wink}

Re: Question on _getch() - Posted By lordotaku (lordotaku) on 10th Mar 08 at 4:50am
Thanks for the help Marc {Smile}

Well, if the buffer needs to be cleared I know that endl will clear it, but this would add an unwanted new line.

Does anyone who codes in C++ no another way to do it?


EDIT:
Just in case anyone else had this question, I found out that you can just code cout << flush to clear the buffer.

Re: Question on _getch() - Posted By Avtar (avtar) on 21st Mar 08 at 10:17am
fflush();

That function will clear the buffer for you. endl; is used like '\n'. It doesn't clear the buffer, simply adds a newline. endl was basically an easier way to add a new line in C++ since people found using escape sequences an issue in C. {Smile}