Thread: Overloading using istream / ostream:Can't figure out why it isn't outputting objects

  1. #1
    Registered User
    Join Date
    Jun 2014
    Location
    Tampa, Florida, United States
    Posts
    28

    Overloading using istream / ostream:Can't figure out why it isn't outputting objects

    I'm implementing istream and ostream overloading for a program that takes in 3 objects, adds them, subtracts them, and multiplies them. I'm having difficulty in outputting the objects. The program outputs the first object just fine, but for some reason defaults the other two...and I've looked through the program and can't figure out what I'm doing wrong. Here's my code:

    Code:
     
    
    #ifndef COMPLEX_H
    #define COMPLEX_H
    #include <string>
    
    
    
    
    class Complex
    {
    
    
        friend std::ostream &operator<<(std::ostream &, const Complex &);
        friend std::istream &operator>>(std::istream &, Complex &);
    public:
        explicit Complex(double = 0.0, double =  0.0);
        Complex operator+(const Complex &) const; //addition
        Complex operator-(const Complex &) const; //subtration
        Complex operator*(const Complex &) const; // mult
        bool operator !=(const double &); 
        bool operator ==(const double &);
    
    
    private:
        double real; 
        double imag; 
    
    
    };
    
    
    #endif
    Code:
     
    
    #include <iostream>
    #include "complex.h"
    #include <string>
    
    
    
    
    using namespace std; 
    //constructor
    Complex::Complex(double realPart, double imaginaryPart)
        : real(realPart),
        imag(imaginaryPart)
    {
    
    
    }
    
    
    Complex Complex::operator+(const Complex &operand2) const
    {
        return Complex(real + operand2.real, imag + operand2.imag); 
    }
    
    
    Complex Complex::operator-(const Complex &operand2) const
    {
        return Complex(real - operand2.real, imag - operand2.imag);
    }
    
    
    Complex Complex::operator*(const Complex &operand2) const
    {
        return Complex(real * operand2.real, imag * operand2.imag);
    }
    
    
    ostream &operator<<(ostream &output, const Complex &d)
    {
        cout << "(" << d.real << "," << d.imag << ")" << "\n";
        return output; 
    }
    istream &operator>>(istream &input, Complex &d)
    {
        input.ignore(); //ignore (
        input >> d.real;
        input.ignore(); //comma
        input >> d.imag; 
        input.ignore(); //)
        
    
    
        return input; 
    
    
    }
    Code:
    #include <iostream> 
    #include "complex.h"
    
    
    using namespace std; 
    
    
    int main()
    {
        Complex x; 
        
    
    
        cout << "Please enter x object in the form (1.2, 3.4)" << endl; 
        cin >> x; 
    
    
        Complex y; 
    
    
        cout << "Please enter y object in the form (1.2,3.4)" << endl; 
        cin >> y; 
    
    
        Complex z; 
    
    
        cout << "Please enter z object in the form (1.2,3.4) " << endl; 
        cin >> z; 
    
    
    
    
    
    
         
    
    
    
    
        cout << "The objects were:" << endl;
    
    
        cout << x << "\n" << endl; 
        cout << y << "\n" << endl; 
        cout << z << "\n" << endl; 
    
    
    
    
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You accidentally wrote cout instead of output.
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. use <fstream> and ostream overloading together
    By mosu' in forum C++ Programming
    Replies: 3
    Last Post: 08-16-2006, 01:06 PM
  2. istream and ostream not declared?
    By aciarlillo in forum C++ Programming
    Replies: 4
    Last Post: 10-02-2005, 01:02 AM
  3. issues using ostream while overloading []
    By Mr_Jack in forum C++ Programming
    Replies: 1
    Last Post: 01-12-2004, 12:00 PM
  4. ostream and istream
    By xddxogm3 in forum C++ Programming
    Replies: 2
    Last Post: 10-20-2003, 07:36 AM
  5. ostream<< overloading for bookinventory program
    By curlious in forum C++ Programming
    Replies: 2
    Last Post: 09-27-2003, 02:41 PM