[Fwd: C:How to program 3rdEd, fig18_03.cpp](g++ 4.4.3)(trouble on simple overload)

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



--- Begin Message ---
Dear Deitel:

  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::ostream;
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;
}


--- End Message ---

[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux