-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path13.14_PhoneNumber.cpp
More file actions
109 lines (101 loc) · 2.52 KB
/
13.14_PhoneNumber.cpp
File metadata and controls
109 lines (101 loc) · 2.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
// 12191706 ±èÁ¤Áø
// Fig. 10.4: PhoneNumber.cpp
// Overloaded stream insertion and stream extraction operators
// for class PhoneNumber.
#include <iomanip>
#include "PhoneNumber.h"
using namespace std;
// Overloaded stream insertion operaotr; cannot be a member function
// if we would like to invoke it with cout << somePhoneNumber;
ostream& operator<< (ostream& output, const PhoneNumber& number) {
output << "Area code: " << number.areaCode
<< "\nExchange: " << number.exchange
<< "\nLine: " << number.line
<< "\n(" << number.areaCode << ") " << number.exchange << "-" << number.line << "\n";
return output; // enables cout << a << b << c;
}
// Overloaded stream extraction operator; cannot be a member function
// if we would like to invoke it with cin >> somePhoneNumber;
istream& operator>> (istream& input, PhoneNumber& number) {
number.areaCode = "";
number.exchange = "";
number.line = "";
char chararray[15] = {};
int i = 0;
int character;
while ( i < 14) {
switch (i) {
case 0:
if ((character = cin.get()) == '(') {
chararray[i] = character;
}
else {
cin.clear(ios::failbit);
}
break;
case 4:
if ((character = cin.get()) == ')') {
chararray[i] = character;
}
else {
cin.clear(ios::failbit);
}
break;
case 5:
if ((character = cin.get()) == ' ') {
chararray[i] = character;
}
else {
cin.clear(ios::failbit);
}
break;
case 9:
if ((character = cin.get()) == '-') {
chararray[i] = character;
}
else {
cin.clear(ios::failbit);
}
break;
default:
if ((character = cin.get())) {
if(character >= '0' && character <= '9')
chararray[i] = character;
}
else {
cin.clear(ios::failbit);
}
break;
}
i++;
}
if (chararray[1] == '1' || chararray[1] == '0')
cin.clear(ios::failbit);
if (chararray[5] == '1' || chararray[5] == '0')
cin.clear(ios::failbit);
if (chararray[2] != '0' && chararray[2] != '1')
cin.clear(ios::failbit);
if (cin.fail() == 0) {
for (int j{ 1 }; j < 4; j++) {
number.areaCode += chararray[j];
}
for (int j{ 6 }; j < 9; j++) {
number.exchange += chararray[j];
}
for (int j{ 10 }; j < 13; j++) {
number.line += chararray[j];
}
}
else {
cout << "Wrong input input.\n";
}
return input; // enables cin >> a >> b >> c;
}
/*
input.ignore(); // skip (
input >> setw(3) >> number.areaCode; // input area code
input.ignore(2); // skip ) and space
input >> setw(3) >> number.exchange; // input exchange
input.ignore(); // skip dash(-)
input >> setw(4) >> number.line; // input line
*/