Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > trouble on simple overloading >>

Reply
Thread Tools

trouble on simple overloading >>

 
 
eric
Guest
Posts: n/a
 
      03-20-2011
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:
(71 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: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;
}
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
RE: Overloading __init__ & Function overloading Iyer, Prasad C Python 4 09-30-2005 08:01 PM
Re: Overloading __init__ & Function overloading Fredrik Lundh Python 0 09-30-2005 03:59 PM
Overloading __init__ & Function overloading Iyer, Prasad C Python 3 09-30-2005 02:17 PM
Re: Overloading __init__ & Function overloading Steve Holden Python 0 09-30-2005 01:58 PM
Re: Overloading __init__ & Function overloading Fredrik Lundh Python 0 09-30-2005 01:53 PM



Advertisments