Results 1 to 7 of 7

Thread: iostream

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Nov 2001
    Posts
    163

    iostream

    #include <iostream.h>

    class complex
    {
    float real,imag;
    public:
    complex();
    complex(float a,float b);
    friend ostream& operator<< ( ostream& os, complex& dt );
    friend istream& operator>> ( istream& is, complex& dt );
    };


    #include "complex.h"

    complex::complex()
    {
    real=imag=0.0;
    }
    complex::complex(float a,float b)
    {
    real=a;
    imag=b;
    }
    ostream& operator<< ( ostream& s, complex& dt )
    {
    s << dt.real << dt.imag;
    return s;
    }
    istream& operator>> ( istream& s, complex& dt )
    {
    s >> dt.real >> dt.imag;
    return s;
    }






    #include "complex.h"
    #include <fstream.h>
    #include <conio.h>

    void main()
    {

    char c;

    cout <<"Press s for save "<<endl;
    cin >>c;

    if (c=='S' || c=='s')
    {
    complex a=complex(1.2,3.4);
    ofstream os("c:\\abc.txt");
    os<<a;
    cout << a;
    }
    else
    {

    complex a;
    ifstream is("c:\\abc.txt");
    is >> a;
    cout << a;
    }

    while (!kbhit());

    }


    why this program different result in bc++ and vc++?
    Purushottam

  2. #2
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    What different result?

    BTW:
    #include <iostream>
    #include <fstream>
    using namespace std;

    And the C++ standard library already defines a complex class:
    #include <complex>
    using namespace std;

    complex<double> c;
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Nov 2001
    Posts
    163
    bc++ gives the result 1.230.4 and cv++ gives no result. And there is result in bc++ that it should give 1.23.4
    Purushottam

  4. #4
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Weird. Have you stepped through it with a debugger?
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Nov 2001
    Posts
    163
    It is working with bc++ fine when i change?

    ostream& operator<< ( ostream& s, complex& dt )
    {
    s << dt.real << dt.imag;
    return s;
    }

    into

    ostream& operator<< ( ostream& s, complex& dt )
    {
    s << dt.real << ' ' << dt.imag;
    return s;
    }

    but it is not working with vc++
    Purushottam

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Nov 2001
    Posts
    163
    In the above example .What to do if we use pointer.

    complex *a=new complex(1.2,3.4);
    it gives the address or somethin like that.
    Purushottam

  7. #7
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Dereference it:

    complex *pc = new complex(1.2, 3.4);
    cout << *pc << endl;
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width