trouble on simple overloading >>

E

eric

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

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,989
Messages
2,570,207
Members
46,783
Latest member
RickeyDort

Latest Threads

Top