trouble on simple overloading >>

Discussion in 'C++' started by eric, Mar 20, 2011.

  1. eric

    eric Guest

    Dear Deitel or c++/g++ expert:

    I copy every thing on your program and try to run on my g++ 4.4.3
    I found, if I copy paste the main()'s cout << "Enter---
    cin >> phone;
    cout << "The---

    twice or thrice
    output is not correct(not what be expected)

    ---------
    root@eric-laptop:/home/eric/Deitel_C/ThirdEd# g++ Fig18_03.cpp
    root@eric-laptop:/home/eric/Deitel_C/ThirdEd# ./a.out
    Enter phone number in the form (123) 456-7890:
    (213) 310-3421
    The phone number entered was: (213) 310-3421
    Enter phone number in the form (123) 456-7890:
    (310) 345-9087
    The phone number entered was: ((31) 345-9087
    Enter phone number in the form (123) 456-7890:
    (718) 212-8765
    The phone number entered was: ((71) 212-8765
    root@eric-laptop:/home/eric/Deitel_C/ThirdEd#
    --------------------------------------------------------
    so , is that your program have problem or my compiler have problem?
    looking to hear from you again soon, Eric Lin, US citizen
    ----------------------------------------------------------


    // Fig. 18.3: fig18_03.cpp
    // Overloading the stream-insertion and
    // stream-extraction operatiors

    #include <iostream>

    using std::cout;
    using std::cin;
    using std::endl;
    using std::eek:stream;
    using std::istream;

    #include <iomanip>

    using std::setw;

    class PhoneNumber {
    friend ostream &operator<<( ostream&, const PhoneNumber & );
    friend istream &operator>>( istream&, PhoneNumber & );

    private:
    char areaCode[ 4 ]; // 3-digit area code and null
    char exchange[ 4 ]; // 3-digit exchange and null
    char line[ 5 ]; // 4-digit line and null
    };

    // Overloaded stream-insertion operator (cannot be
    // a member function if we would like to invoke it with
    // cout << somePhoneNumber;).
    ostream &operator<<( ostream &output, const PhoneNumber &num)
    {
    output << "(" << num.areaCode << ") "
    << num.exchange << "-" << num.line;

    return output; // enables cout << a << b << c;
    }

    istream &operator>>( istream &input, PhoneNumber &num )
    {
    input.ignore(); // skip (
    input >> setw( 4 ) >> num.areaCode; // input area code
    input.ignore( 2 ); // skip ) and space
    input >> setw( 4 ) >> num.exchange; // input exchange
    input.ignore(); // skip dash (-)
    input >> setw( 5 ) >> num.line; // input line

    return input; // enables cin >> a >> b >> c;
    }

    int main()
    {
    PhoneNumber phone; // create object phone

    cout << "Enter phone number in the form (123) 456-7890:\n";

    // cin >> phone invokes operator>> function by
    // issuing the call operator>>( cin, phone ).
    cin >> phone;

    // cout << phone invokes operator<< function by
    // issuing the call operator<<( cout, phone ).
    cout << "The phone number entered was: " << phone << endl;


    ////////////////

    cout << "Enter phone number in the form (123) 456-7890:\n";

    // cin >> phone invokes operator>> function by
    // issuing the call operator>>( cin, phone ).
    cin >> phone;

    // cout << phone invokes operator<< function by
    // issuing the call operator<<( cout, phone ).
    cout << "The phone number entered was: " << phone << endl;

    ////////////////
    cout << "Enter phone number in the form (123) 456-7890:\n";

    // cin >> phone invokes operator>> function by
    // issuing the call operator>>( cin, phone ).
    cin >> phone;

    // cout << phone invokes operator<< function by
    // issuing the call operator<<( cout, phone ).
    cout << "The phone number entered was: " << phone << endl;


    return 0;
    }
     
    eric, Mar 20, 2011
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Iyer, Prasad C

    Overloading __init__ & Function overloading

    Iyer, Prasad C, Sep 30, 2005, in forum: Python
    Replies:
    3
    Views:
    6,428
    Fredrik Lundh
    Sep 30, 2005
  2. Fredrik Lundh
    Replies:
    0
    Views:
    454
    Fredrik Lundh
    Sep 30, 2005
  3. Steve Holden
    Replies:
    0
    Views:
    437
    Steve Holden
    Sep 30, 2005
  4. Iyer, Prasad C
    Replies:
    4
    Views:
    585
    John J. Lee
    Sep 30, 2005
  5. Fredrik Lundh
    Replies:
    0
    Views:
    410
    Fredrik Lundh
    Sep 30, 2005
Loading...

Share This Page