I wrote a class called Console that does such things... Source of a sample app is attached. Please forgive the state it's in. It works and all, but I never did get to putting the finishing touches on it...
Hope it helps.
Code:
class Console {
HANDLE _scr;
WORD _textColor;
WORD _bgColor;
void setColor() { if (isValid()) SetConsoleTextAttribute(_scr, _textColor | _bgColor); }
public:
Console(int textColor=COLOR_WHITE | COLOR_BOLD, int bgColor=COLOR_BLUE);
~Console();
bool open();
void show();// show our console (more than one may be used)
void close();
void clear();// "cls"
void goToXY(int x, int y);// reposition cursor
void setTextColor(int color);
void setBgColor(int color);
DWORD print(const LPTSTR text);
DWORD print(TCHAR ch);
void flush();// empty the buffer
bool keyHit();// was a key pressed?
DWORD read(LPTSTR buf, int size);// buffered read
bool getChar(TCHAR &ch);// unbuffered read
TCHAR getChar() { TCHAR ch = 0; getChar(ch); return ch; }
void setTitle(const LPTSTR title) { SetConsoleTitle(title); }
bool isValid() const { return _scr != INVALID_HANDLE_VALUE; }
// output operators
Console& operator<<(const LPTSTR text) { print(text); return *this; }
Console& operator<<(TCHAR ch) { print(ch); return *this; }
Console& operator<<(int num);
Console& operator<<(long num);
Console& operator<<(double num);
// input operators
Console& operator>>(int &num);
Console& operator>>(long &num);
Console& operator>>(double &num);
};