I'm currently working on a program that encrypts and decrypts messages. Traditionally, messages in cryptography are displayed in groups of five letters regardless of actual spaces. Something like this:

Code:

THISI SONLY ATEST TESTI NGONE TWOTH REE

I know that I could use a loop to put in those spaces, but what I'd really like to do is write my own iosflag. I'd like to display what I just wrote using code like this:

My teacher said he's never done this and doesn't know how. Does anyone here know the syntax for writing my own iosflag? Your help will be greatly appreciated.

12-13-2002

master5001

Just add your flag to the enumerations in ios or ios.h (or whatever it is with your compiler) then make a class that inherits ostream.

The bottom line is that you can't do exactly what you want. Even if you add a flag that doesn't mean that cout will recognize it. So you are going to have to make a proprietary cout (not that hard).

12-13-2002

joshdick

Quote:

Originally posted by master5001 So you are going to have to make a proprietary cout (not that hard).

What do you mean by proprietary? Do you mean that I need to overload the cout operator, or that I should write something like cout, but slightly different and using a different identifier? And, how would I do such a thing? (Just asking for the general syntax form. I don't want anyone to write my program for me.)

12-14-2002

master5001

>> should write something like cout, but slightly different <<

That is what I mean by proprietary. You can use the same operator. Other than changing cout's definition in the source of your compiler (which isn't always possible, since you may not be using a free compiler) you can't just slap a new identifier in your code and expect cout do know what you mean. You'll need to write your own cout. It could actually use cout for everything except for encryption, which you would have to write yourself. And no, no one is going to write this for you.

12-14-2002

joshdick

I know that it's possible to overload operators like << but is it possible to create a new operator to do something like what I want to do?

12-14-2002

master5001

You mean like this:

Code:

my_cout ÿ "hello world" ÿ std::endl;

Where "ÿ" is your operator?

No.

12-15-2002

joshdick

Okay, then, what's the syntax to write my own cout?

12-15-2002

master5001

Here is a start:

Code:

#include <ostream>

class encrypt_ostream : public ostream {
// write code here
};

encrypt_ostream e_cout;

Sorry for not doing much, but I'm sure you can do this yourself.

12-15-2002

joshdick

I'm starting to think it'd just be easier to write a function that takes a string as an argument and prints out what I want.

12-15-2002

master5001

Well there is an alternative that would incorporate the existing ostream (cout included) but would not incoporate your new ios flag. You could just override bitshift operators for your class.

That looks like a good idea, but my teacher doesn't like it when I include things in my program that he hasn't taught yet, and he hasn't taught classes yet. Really, I'm not even supposed to be using arrays, but he'll just have to live with that:p

Thanks for all of your help, but I guess I'll just have to settle for something dinky that gets the job done.

12-15-2002

master5001

Your in luck, I see you have been using cout. So you can include iostream. iostream includes ostream so you should be okay.