Thread: std::cout / streams question

  1. #1
    Registered User
    Join Date
    Dec 2006
    Posts
    69

    std::cout / streams question

    Hello, I have several cout statements in my code, that all have to print similar strings.

    Example:
    Code:
    std::cout << "WARNING: corrupt CHARACTER_LIST_REQUEST packet received from: \n"
                    << "SystemID: " << sourceSystem << "\n"
                    << "IP: " << (net->getSystemAddress(sourceSystem)).ToString(true) << "\n"
                    << (dataLength < 8 ? "packet too small" : "packet too large") << std::endl;
    The parts where it prints the SystemID and IP are needed in every warning/error message, so I'd like to create a function that does it for me. Ofcourse I could easily write a function that uses cout itself, but I'd like to be able to put it in the cout statement itself.

    Like this:
    Code:
    std::cout << "WARNING: corrupt CHARACTER_LIST_REQUEST packet received from: \n"
                    << systemIP(sourceSystem)
                    << (dataLength < 8 ? "packet too small" : "packet too large") << std::endl;
    I could let systemIP return a char* or std::string, but then I'd have to somehow deal with deallocating them.

    How can this be done? I'd like to only have that single function call line in the std::cout.

    PS. this isn't a big deal, I'm mainly asking to learn something from it.

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    If you return a std::string, you have nothing to worry about.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Dec 2006
    Posts
    69
    So this is ok?

    Code:
    getIPStr(SystemID systemID)
    {
       stringstream ss;
       ss << "SystemID: " << systemID
          << "IP: " << (net->getSystemAddress(systemID)).ToString(true);// << endl;
       string s = ss.str();
       return s;
    }
    
    ..
    
    std::cout << "WARNING: corrupt CHARACTER_LIST_REQUEST packet received from: \n"
                      << getIPStr(sourceSystem) << "\n"
                      << (dataLength < 8 ? "packet too small" : "packet too large") << std::endl;

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You missed the return type, but yes, it's safe since std::string will destruct when not needed and delete its memory with it.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Dec 2006
    Posts
    69
    Thank you

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. another do while question
    By kbpsu in forum C++ Programming
    Replies: 3
    Last Post: 03-23-2009, 12:14 PM
  2. A question about file streams.
    By Lawn Gnomusrex in forum C++ Programming
    Replies: 2
    Last Post: 11-07-2008, 06:05 AM
  3. Very simple question, problem in my Code.
    By Vber in forum C Programming
    Replies: 7
    Last Post: 11-16-2002, 03:57 PM
  4. Question type program for beginners
    By Kirdra in forum C++ Programming
    Replies: 7
    Last Post: 09-15-2002, 05:10 AM
  5. a question re: Streams
    By HOWY in forum C Programming
    Replies: 1
    Last Post: 08-10-2002, 11:38 AM