Thread: How do I write my own iosflag?

  1. #1
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146

    Question How do I write my own iosflag?

    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:
    Code:
    cout << setiosflags(ios::crypt)
         << "THISISONLYATESTTESTINGONETWOTHREE";
    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.
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

  2. #2
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    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).

  3. #3
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    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.)
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

  4. #4
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    >> 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.

  5. #5
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    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?
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

  6. #6
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    You mean like this:

    Code:
    my_cout ÿ "hello world" ÿ std::endl;
    Where "ÿ" is your operator?

    No.

  7. #7
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    Okay, then, what's the syntax to write my own cout?
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

  8. #8
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    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.

  9. #9
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146

    Unhappy

    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.
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

  10. #10
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    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.

    Example
    Code:
    class YourEncryptionClass {
    // .. your stuff
    public:
        friend ostream &operator << (std::ostream &, YourEncryptionClass &);
    };
    
    ostream &operator << (std::ostream &o, YourEncryptionClass &enc) {
    // .. do stuff here
    }

  11. #11
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    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

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

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

  12. #12
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Your in luck, I see you have been using cout. So you can include iostream. iostream includes ostream so you should be okay.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 03-18-2006, 11:25 AM
  2. program to make a floppy write protected
    By shrijeetp in forum C Programming
    Replies: 1
    Last Post: 10-03-2005, 06:00 AM
  3. Reroute where programs write to
    By willc0de4food in forum C Programming
    Replies: 7
    Last Post: 09-21-2005, 04:48 PM
  4. Function to write string 3 times
    By Giggs in forum C++ Programming
    Replies: 15
    Last Post: 12-24-2002, 04:00 PM
  5. write in c
    By PutoAmo in forum C Programming
    Replies: 6
    Last Post: 04-03-2002, 07:53 PM