469,623 Members | 1,800 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,623 developers. It's quick & easy.

overloading cout

I'd like to have better control of text output to the console. I'm using
Windows and have a little (non-standard) function to help me out:

#include <windows.h>
void locate(int x, int y )
{
COORD cur = { x, y };
SetConsoleCursorPosition( GetStdHandle( STD_OUTPUT_HANDLE ), cur );
}

Which is easy enough to use:
locate(x,y); cout << "text here";

However...(and this is why the question is relevant in this group)...
I would prefer to use cout like:

cout(x,y) << "text here";

Can anyone instruct me on how to do this?

Thanks,

Joe
Jul 23 '05 #1
7 7211
"Joe C" <jk*****@bellsouth.net> wrote in message
news:ub******************@bignews3.bellsouth.net.. .
I'd like to have better control of text output to the console. I'm using
Windows and have a little (non-standard) function to help me out:

#include <windows.h>
void locate(int x, int y )
{
COORD cur = { x, y };
SetConsoleCursorPosition( GetStdHandle( STD_OUTPUT_HANDLE ), cur );
}

Which is easy enough to use:
locate(x,y); cout << "text here";

However...(and this is why the question is relevant in this group)...
I would prefer to use cout like:

cout(x,y) << "text here";

Can anyone instruct me on how to do this?

Thanks,

Joe

Well, you can't "overload cout" but there are a lot of functions or functors
which you could write. For instance:

void write_at(int x, int y, const std::string &text, std::ostream &os =
std::cout);

template <typename THING>
void write_at(int x, int y, const THING &thing, std::ostream &os =
std::cout);

class write_at
{
public:
write_at(std::ostream &);
template <typename THING>
void operator (int x, int y, const THING &);
private:
std::ostream & m_os;
};

--
Cy
http://home.rochester.rr.com/cyhome/
Jul 23 '05 #2
"Joe C" <jk*****@bellsouth.net> schrieb im Newsbeitrag
news:ub******************@bignews3.bellsouth.net.. .
I'd like to have better control of text output to the console. I'm using
Windows and have a little (non-standard) function to help me out:

#include <windows.h>
void locate(int x, int y )
{
COORD cur = { x, y };
SetConsoleCursorPosition( GetStdHandle( STD_OUTPUT_HANDLE ), cur );
}

Which is easy enough to use:
locate(x,y); cout << "text here";

However...(and this is why the question is relevant in this group)...
I would prefer to use cout like:

cout(x,y) << "text here";

Can anyone instruct me on how to do this?


The C++ style to do such things would be

cout << locate(x,y) << "Some text"

You can write an IO manipulator for that, then it would work with all other
kinfs of streams, even those not based on char; or you can simply change
void locate(...) {...} to char const* locate(...){... return "";}

HTH
Heinz
Jul 23 '05 #3


Joe C wrote:
I'd like to have better control of text output to the console. I'm using
Windows and have a little (non-standard) function to help me out:

#include <windows.h>
void locate(int x, int y )
{
COORD cur = { x, y };
SetConsoleCursorPosition( GetStdHandle( STD_OUTPUT_HANDLE ), cur );
}

Which is easy enough to use:
locate(x,y); cout << "text here";

However...(and this is why the question is relevant in this group)...
I would prefer to use cout like:

cout(x,y) << "text here";

Can anyone instruct me on how to do this?

Thanks,

Joe


The easiest approach is to define an operator<< for the type of object
being output. As an example, we'll declare our own Coordinate struct
but you could just as easily use a preexisting Coordinate type:

struct Coordinate
{
Coordinate( long inX, long inY)
: x(inX), y(inY)
{
}

long y;
long x;
};

then we need only define the operator<<() for our Coordinate type:

std::ostream& operator<<( std::ostream& lhs, const Coordinate& rhs)
{
lhs << rhs.x << "," << rhs.y;
return lhs;
}

then we can output the Coordinate type as follows:

std::cout << Coordinate(x, y) << "some text";

This example takes advantage of Coordinate's constructor to build the
object in place.

Greg

Jul 23 '05 #4
----- Original Message -----
From: "Greg" <gr****@pacbell.net>
Newsgroups: comp.lang.c++
Sent: Monday, July 04, 2005 12:20 AM
Subject: Re: overloading cout



Joe C wrote:
I'd like to have better control of text output to the console. I'm using
Windows and have a little (non-standard) function to help me out:

#include <windows.h>
void locate(int x, int y )
{
COORD cur = { x, y };
SetConsoleCursorPosition( GetStdHandle( STD_OUTPUT_HANDLE ), cur );
}

Which is easy enough to use:
locate(x,y); cout << "text here";

However...(and this is why the question is relevant in this group)...
I would prefer to use cout like:

cout(x,y) << "text here";

Can anyone instruct me on how to do this?

Thanks,

Joe


The easiest approach is to define an operator<< for the type of object
being output. As an example, we'll declare our own Coordinate struct
but you could just as easily use a preexisting Coordinate type:

struct Coordinate
{
Coordinate( long inX, long inY)
: x(inX), y(inY)
{
}

long y;
long x;
};

then we need only define the operator<<() for our Coordinate type:

std::ostream& operator<<( std::ostream& lhs, const Coordinate& rhs)
{
lhs << rhs.x << "," << rhs.y;
return lhs;
}

then we can output the Coordinate type as follows:

std::cout << Coordinate(x, y) << "some text";

This example takes advantage of Coordinate's constructor to build the
object in place.

Greg


Yes, but I dont' think this resolves the original problem, in that I believe
the locate(x,y) function has to be called. so shouldn't the << override
actually be:

std::ostream& operator<<( std::ostream& lhs, const Coordinate& rhs)
{
locate(x, y);
return lhs;
}

or

std::ostream& operator<<( std::ostream& lhs, const Coordinate& rhs)
{
COORD cur = { rhs.x, rhs.y };
SetConsoleCursorPosition( GetStdHandle( STD_OUTPUT_HANDLE ), cur );
return lhs;
}
Jul 23 '05 #5


Joe C schreef:
I'd like to have better control of text output to the console. I'm using
Windows and have a little (non-standard) function to help me out:

#include <windows.h>
void locate(int x, int y )
{
COORD cur = { x, y };
SetConsoleCursorPosition( GetStdHandle( STD_OUTPUT_HANDLE ), cur );
}

Which is easy enough to use:
locate(x,y); cout << "text here";

However...(and this is why the question is relevant in this group)...
I would prefer to use cout like:

cout(x,y) << "text here";

Can anyone instruct me on how to do this?


It wouldn't exactly be std::cout, but my::cout. That nicely solves the
name problem:

#include <iostream>
#include <iosfwd>
namespace my {
void locate(int x, int y );
std::ostream& cout( int x, int y ) {
locate(x,y);
return std::cout;
}
}

my::cout << "Hello, world" << std::endl;

HTH,
Michiel Salters

Jul 23 '05 #6

"msalters" <Mi*************@logicacmg.com> wrote in message
news:11*********************@g49g2000cwa.googlegro ups.com...


Joe C schreef:
I'd like to have better control of text output to the console. I'm using
Windows and have a little (non-standard) function to help me out:

#include <windows.h>
void locate(int x, int y )
{
COORD cur = { x, y };
SetConsoleCursorPosition( GetStdHandle( STD_OUTPUT_HANDLE ), cur );
}

Which is easy enough to use:
locate(x,y); cout << "text here";

However...(and this is why the question is relevant in this group)...
I would prefer to use cout like:

cout(x,y) << "text here";

Can anyone instruct me on how to do this?


It wouldn't exactly be std::cout, but my::cout. That nicely solves the
name problem:

#include <iostream>
#include <iosfwd>
namespace my {
void locate(int x, int y );
std::ostream& cout( int x, int y ) {
locate(x,y);
return std::cout;
}
}

my::cout << "Hello, world" << std::endl;

HTH,
Michiel Salters


Thanks Michiel and the others who responded. This namespace solution is
what I am looking for, and the other solutions helped me better understand
the use of overloaded inserters and extracters.
Jul 23 '05 #7
test prog follows:

#include <iostream>
#include <iosfwd>

namespace my {
void locate(int x, int y );
std::ostream& cout( int x = -1, int y = -1 ) {
if(~x || ~y){
locate(x,y);
}
return std::cout;
}
void cls();
void wait(){std::cin.get();}

}

using namespace my;

int main(){
cout() << "my::cout() behaves just like std::cout\n";
cout() << "with letters following the cursor";

cout(20,12) << "but you can use the function with arguments";
cout(20,13) << "to print to arbitrary coordinates";
wait();
cls();
cout(0,0) << "All";
wait();
cout(76, 0) << "you";
wait();
cout(75, 23) << "need";
wait();
cout(0,23) << "is";
wait();
cout(38, 12) << "love";
wait();
cls();

cout(25, 12) << "goodbye cruel world";
cout(0,24) << "Press Enter to continue";
std::cin.get();

int t=clock();
for(int z=0; z<100; z++){
cout(z%80, z%25) << z;
cls();
}
t=clock()-t;
cout()<< t << "secs";
cls();
int t2=clock();
for(int z=0; z<100; z++){
cout(z%80, z%25) << z;
system("cls");
}
t2 = clock() - t2;
cls();
cout()<< t << std::endl << t2 ;

wait();
return 0;

}
#include <windows.h>

void my::locate(int x, int y )
{
COORD cur = { x, y };
SetConsoleCursorPosition( GetStdHandle( STD_OUTPUT_HANDLE ), cur );
}
#include <string>
void my::cls(){
static const std::string b(2000, ' ');
my::cout(0,0) << b;
my::locate(0,0);
}

Jul 23 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Dave Theese | last post: by
7 posts views Thread by Hendrik Schober | last post: by
2 posts views Thread by Kamran | last post: by
20 posts views Thread by KL | last post: by
6 posts views Thread by TuxC0d3 | last post: by
3 posts views Thread by silver360 | last post: by
2 posts views Thread by Colonel | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.