Thread: operator overloading >>

  1. #1
    Registered User
    Join Date
    May 2013
    Posts
    59

    operator overloading >>

    anyone have any idea how to ask the user to enter for each attributes without using cout in my operator overloading >> ? I still want to print out the Enter program name etc but not inside this function. According to my lecturer, its bad programming practice. Care to shed some light on how to do that?

    my current code
    Code:
    istream &operator >> (istream& is, clsUniversityProgram &arg)
    {
        // prompt to enter program name
        cout << endl << "Enter program name\t: ";
        is >> arg.programName;
    
    
        // prompt to enter program code
        cout << "Enter program code\t: ";
        is >> arg.programCode;
    
    
        // prompt to enter program fees
        cout << "Enter program fees\t: ";
        is >> arg.programFees;
    
    
        return is;
    }
    Last edited by Alexius Lim; 11-20-2013 at 02:08 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,834
    Generally, you would overload operator>> for input streams when you have some pre-determined input format such that you can say, parse input from a file. If you are going to prompt the user for input to populate the object part by part interactively, then overloading operator>> is not the right approach: just write another function.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    May 2013
    Posts
    59
    i see... thanks. By the way, why is putting cout in input operator overloading a bad practice?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,834
    Quote Originally Posted by Alexius Lim
    why is putting cout in input operator overloading a bad practice?
    The convention is that operator>> when overloaded for std::istream is for reading formatted input. The input format is already known, so no prompting is necessary (or the prompting would have happened prior to the function call). Therefore, if there is output, it would likely just be for logging.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by Alexius Lim View Post
    i see... thanks. By the way, why is putting cout in input operator overloading a bad practice?
    Because the purpose of overloading an operator is to act on its operand (or, in case of operator>>(), its operands). So a statement "s >> x" does something based on s and x, not something that affects some unrelated other object (like cout). If the operator>>() is a streaming operator, that means it reads a value to x from input stream s.

    In your case, what would be the point of writing to cout if the supplied input stream is not cin?
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. overloading [] operator
    By Raigne in forum C++ Programming
    Replies: 1
    Last Post: 06-24-2007, 02:44 PM
  2. Overloading -> operator
    By Hunter2 in forum C++ Programming
    Replies: 0
    Last Post: 05-10-2004, 03:08 PM
  3. operator overloading
    By blue_gene in forum C++ Programming
    Replies: 6
    Last Post: 04-29-2004, 04:06 PM
  4. overloading an operator
    By drdodirty2002 in forum C++ Programming
    Replies: 6
    Last Post: 04-28-2004, 09:47 PM
  5. operator overloading ()
    By terracota in forum C++ Programming
    Replies: 4
    Last Post: 04-01-2004, 07:52 AM

Tags for this Thread