Find the answer to your Linux question:
Results 1 to 5 of 5
Dear Deitel or Linux c++ programers: I tried to do just a) Modify the class to enable input and output of complex numbers through the overloaded >> and << operator, ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Aug 2010
    Posts
    5

    operator overloading(in g++)(simple question)


    Dear Deitel or Linux c++ programers:

    I tried to do just a) Modify the class to enable input and output
    of complex numbers through the overloaded >> and << operator,
    respectively (you should remove the print function from the class)
    (in book of How to Program C, 3rd ed. pg679, 18.10)

    this is my 3 files I modify
    ---------
    plz see attachments
    ----------------------------
    /* and I tried, new y as (88.99,75.48)
    * new z as (2.9,9.2)
    */
    /************
    but my tested result as
    ************
    # g++ complex1.cpp
    fig18_05.cpp
    # ./a.out
    x: (0,0)

    y: (4.3,8.2)

    z: (3.3,1.1)


    x = y + z:
    (7.6,9.3)
    = (4.3,8.2)
    + (3.3,1.1)


    x = y - z:
    (1,7.1)
    = (4.3,8.2)
    - (3.3,1.1)

    Now input new y
    (88.99,75.48)

    Echo
    (88.99,75.48)

    Now input new z
    (2.9,9.2)

    Echo
    (0,1.1)


    This is new x = (88.99,76.58)

    #
    ***********************************************/
    the part need your help is why after I input new z, it be Echo by
    different complex number?
    Attached Files Attached Files

  2. #2
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, in Chicago, or in a galaxy far, far away.
    Posts
    11,639
    What happens if you flush cin before you take more input? Other issue, with your output, you may not be seeing the prompts when you think you are. IE, do this

    cout << "Now input new y" << endl;

    instead of this

    cout << "Now input new y\n";

    cout is buffered, just like (and is) stdout. With a printf(), you have to flush stdout in a C program. Similarly, you need the endl or flush iomanipulator to make sure the output goes to screen. This is especially important for on-screen prompts before you take input. So, use endl if you want the output flushed with a new line, and flush if you just want the output. Example:

    cout << "Input new z: " << flush;

    The cursor would be one space after the colon, waiting for input.

    Anyway, by flushing cin, do it like this:

    cin.clear();

    That will get rid of any left-over data in the input buffer.
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

  3. #3
    Just Joined!
    Join Date
    Aug 2010
    Posts
    5

    cin >> overloading

    thanks your fast reply:

    first of all:
    flush is not defined in main block(compile error, g++4.4.3, UbuntuLinux)

    then:
    I tried change \n to endl
    and
    add cin.clear() (before and after cin >> (some complex variables)

    it help a little bit
    first 2 cin >> (complex variable) work (but second one prabably did not work good)
    second 2 cin >> (complex variable) , only one work(second one work)
    third 2 cin >> (complex variable) , only one work(second one work)
    ----------------
    cout << "2 time cin to x" << endl;
    // int i;
    cin >> x;
    cin.clear();
    cout << endl ;
    cout << x << endl;
    cin >> x;
    cin.clear();
    cout << x << endl;
    cout << endl ;
    cout << "2 times cin to y" << endl;
    cin >> y;
    cin.clear();
    cout << endl ;
    cout << y << endl;
    cin >> y;
    cin.clear();
    cout << y << endl;
    cout << endl ;
    cout << "2 times cin to z" << endl;
    cin >> z;
    cin.clear();
    cout << endl ;
    cout << z << endl;
    cin >> z;
    cin.clear();
    cout << z << endl;
    cout << endl ;
    --------------------
    plz help

  4. #4
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, in Chicago, or in a galaxy far, far away.
    Posts
    11,639
    Well, I'm going to drop back to the standard question when I'm buffaloed: what Linux version+distribution+kernel, and what gcc/g++ version are you compiling with?
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

  5. #5
    Just Joined!
    Join Date
    Aug 2010
    Posts
    5
    g++ 4.4.3, 2.6.32-30
    cin >> (complex variable) is not work good, from second time call it.
    but cout << (complex variable) is working good
    plz download my attached 3 files, which is most same as Deitel's example, modify/run it
    in your linux, g++ system, to see how it work. if you get right one, plz post
    thanks a lot in advance, Eric

Posting Permissions

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