In Fig. 11.5, the stream extraction and stream insertion operators were overloaded for input and output of objects of the PhoneNumber class. Rewrite the stream extraction operator to perform the following error checking on input. The operator>> function will need to be reimplemented.

a) Input the entire phone number into an array. Test that the proper number of characters has been entered. There should be a total of 14 characters read for a phone number of the form (800) 555-1212. Use ios_base-member-function clear to set failbit for improper input.
b) The area code and exchange do not begin with 0 or 1. Test the first digit of the area-code and exchange portions of the phone number to be sure that neither begins with 0 or 1. Use ios_base-member-function clear to set failbit for improper input.
c) The middle digit of an area code used to be limited to 0 or 1 (although this has changed recently). Test the middle digit for a value of 0 or 1. Use the ios_base-member-function clear to set failbit for improper input. If none of the above operations results in failbit being set for improper input, copy the three parts of the telephone number into the areaCode, exchange and line members of the PhoneNumber object. If failbit has been set on the input, have the program print an error message and end, rather than print the phone number.

```
#ifndef PHONENUMBER_H
#define PHONENUMBER_H

#include
using namespace std;

class PhoneNumber
{
// overloaded input and output operators
friend ostream& operator<<( ostream&, const PhoneNumber& );
friend istream& operator>>( istream&, PhoneNumber& );

public:
PhoneNumber(); // default constructor
private:
char phone[ 15 ]; // holds phone number
char areaCode[ 4 ]; // holds area code
char exchange[ 4 ]; // holds exchange
char line[ 5 ]; // holds line
}; // end class PhoneNumber

#endif
```
```
// Member function definition of class PhoneNumber
#include
#include
#include
#include "PhoneNumber.h"
using namespace std;

// PhoneNumber default constructor
PhoneNumber::PhoneNumber()
{
// set all char arrays to null character
phone[ 0 ] = '\0';
areaCode[ 0 ] = '\0';
exchange[ 0 ] = '\0';
line[ 0 ] = '\0';
} // end PhoneNumber constructor

// overloaded << operator
ostream &operator<<( ostream &output, const PhoneNumber &number )
{
// display PhoneNumber
output << "(" << number.areaCode << ") " << number.exchange
<< "-" << number.line << '\n';
return output; // return ostream reference
} // end overloaded << operator

// overloaded >> operator
istream &operator>>( istream &input, PhoneNumber &number )
{
// get phone number from input stream
input.getline( number.phone, 15 );

// validate length of PhoneNumber
if ( strlen( number.phone ) != 14 )
input.clear( ios::failbit );

// validate ( ) and -
if ( number.phone[ 0 ] != '(' || number.phone[ 4 ] != ')' ||
number.phone[ 9 ] != '-' )
input.clear( ios::failbit );

// validate first digit of area code and exchange
if ( number.phone[ 1 ] == '0' || number.phone[ 6 ] == '0' ||
number.phone[ 1 ] == '1' || number.phone[ 6 ] == '1')
input.clear( ios::failbit );

// validate middle digit of area code
if ( number.phone[ 2 ] != '0' && number.phone[ 2 ] != '1' )
input.clear( ios::failbit );

// if phone number is valid, set exchange and area code members
if ( !input.fail() )
{
int loop;

for ( loop = 0; loop <= 2; loop++ )
{
number.areaCode[ loop ] = number.phone[ loop + 1 ];
number.exchange[ loop ] = number.phone[ loop + 6 ];
} // end for

// place null-terminating character at end of area code and exchange
number.areaCode[ loop ] = number.exchange[ loop ] = '\0';

// set line class member
for ( loop = 0; loop <= 3; loop++ )
number.line[ loop ] = number.phone[ loop + 10 ];

// place null-terminating character at end of line array
number.line[ loop ] = '\0';
} // end if

// if phone number is invalid, tell user and terminate application
else
{
cerr << "Invalid phone number entered.\n";
exit( 1 );
} // end else

return input; // return istream reference
} // end overloaded >> operator
```
```
// PhoneNumber test program
#include
#include "PhoneNumber.h"
using namespace std;

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

// ask user to enter valid telephone number and store it
cout << "Enter a phone number in the form (123) 456-7890:\n";
cin >> telephone;

// display phone number entered by user
cout << "The phone number entered was: " << telephone << endl;

// ask user for invalid phone number and store it
cout << "Now enter an invalid phone number:\n";
cin >> telephone;
} // end main
```
Enter a phone number in the form (123) 456-7890:
(800) 987-4567
The phone number entered was: (800) 987-4567
Now enter an invalid phone number:
(000) 000-0000
Invalid phone number entered.

Computer Science & Information Technology

You might also like to view...

Which of the following risks relates to negative public opinion?

A) Operational risk B) Strategic risk C) Financial risk D) Reputational risk

Computer Science & Information Technology

You can easily change the vertical order of InDesign objects using the ____ commands on the Object menu.

A. Align B. Modify C. Arrange D. Group

Computer Science & Information Technology