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 7 7554
"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/
"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
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
----- 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;
}
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
"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.
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);
} This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Dave Theese |
last post by:
Hello all,
I'm trying to get a grasp of the difference between specializing a function
template and overloading it. The example below has a primary template, a
specialization and an overload. ...
|
by: Hendrik Schober |
last post by:
Hi,
I have a problem, that boils down to the following code:
#include <iostream>
#include <typeinfo>
class Test1 {};
class Test2 {};
class Test3 {};
|
by: Kamran |
last post by:
Hi
I have very little experience of C++, nevertheless I have
been asked to write a gui using QT/QWT.
I know that I should direct the question to the relevant
mailing list and I have done that but...
|
by: KL |
last post by:
I am working on a school assignment, so please don't tell me the
solution. I just want some direction.
I am supposed to overload the >, <, ==, !=, >=, and <= operators using
bool. I am having...
|
by: TuxC0d3 |
last post by:
Hi!
I'm diving into the some more ++ specific aspects of c++ (and finally
accepting that c++ is more than "a plus added to c" :), so that means
using namespaces, templates, std::strings, lists,...
|
by: silver360 |
last post by:
Hello,
I'm trying to create a basic Heap manager and i have some question
about new/delete overloading.
The following code give me this output :
>> $./heap
>> registered : 0x804d098
>>...
|
by: brzozo2 |
last post by:
Hello, this program might look abit long, but it's pretty simple and
easy to follow. What it does is read from a file, outputs the contents
to screen, and then writes them to a different file. It...
|
by: clicwar |
last post by:
A simple program with operator overloading and copy constructor:
#include <iostream>
#include <string>
using namespace std;
class Vector {
private:
float x,y;
public:
Vector(float u, float...
|
by: Colonel |
last post by:
It seems that the problems have something to do with the overloading of
istream operator ">>", but I just can't find the exact problem.
// the declaration
friend std::istream &...
|
by: Nikhil.S.Ketkar |
last post by:
Hi,
Does the following construct qualify as overloading on return type ?
If not, in what way ? I overloaded the type conversion operator and
used pointers to member functions. Its pretty...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
| |