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 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
|
#include <iostream>
#include <iomanip>
#include <string>
#include <limits>
#include <type_traits>
template<typename T = int>
typename std::enable_if_t< std::is_integral_v<T>, T>
getInp(const std::string& prm)
{
const auto notsp{ [&]() {while (std::isspace(static_cast<unsigned char>(std::cin.peek())) && std::cin.peek() != '\n') std::cin.ignore(); return std::cin.peek() != '\n'; } };
T n{};
while ((std::cout << prm) && (!(std::cin >> n) || notsp())) {
std::cout << "Invalid input. Not a(n) " << typeid(T).name() << '\n';
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
return n;
}
class Record {
private:
std::string name;
std::string email_address;
unsigned phone_no {};
public:
Record() {};
Record(const std::string& nam, const std::string& email, unsigned phone) : name(nam) {
setEmailAddress(email);
setPhoneNo(phone);
}
void setName(const std::string& aName) { name = aName; }
std::string getName() const { return name; }
bool setEmailAddress(const std::string& aEmail_address) {
if (aEmail_address.find('@') != std::string::npos) {
email_address = aEmail_address;
return true;
}
return false;
}
std::string getEmailAddress() const { return email_address; }
bool setPhoneNo(unsigned aphone_no) {
if (aphone_no >= 10'000'000 && aphone_no <= 99'999'999) {
phone_no = aphone_no;
return true;
}
return false;
}
unsigned getPhoneNo() const { return phone_no; }
};
void create(Record& rec) {
std::string name;
std::cout << "Enter name: ";
getline(std::cin, name);
rec.setName(name);
std::string email_addr;
do {
std::cout << "Enter email address: ";
std::cin >> email_addr;
} while (!rec.setEmailAddress(email_addr) && (std::cout << "Invalid email address\n"));
while (!rec.setPhoneNo(getInp<unsigned>("Enter phone number: ")) && (std::cout << "Invalid phone number\n"));
}
int main()
{
constexpr size_t MaxAddr {100};
bool quit{ };
Record AddressBook[MaxAddr]{ {"Larwarnce cheung", "enccl@eie.polyu.edu.hk", 27666131},
{"Helen Wong", "helenwong@yahoo.com", 94665888},
{"Simon Su", "ss123@gmail.com", 64441234},
{"Mary Ho", "ho.mary1@@navigator.com", 21111112}};
size_t noRecs {4};
while (!quit) {
std::cout << "\n1. Initialise the address book\n"
<< "2. Create person contact information\n"
<< "3. Lookup person contact information\n"
<< "4. Lookup all person contact information\n"
<< "5. Quit\n";
switch (getInp<unsigned>("\nEnter option: ")) {
case 1:
noRecs = 4;
break;
case 2:
if (noRecs < MaxAddr)
create(AddressBook[noRecs++]);
else
std::cout << "Maximum number of records reached\n";
break;
case 3:
std::cout << "Not yet implemented\n";
break;
case 4:
std::cout << '\n';
for (size_t i = 0; i < noRecs; ++i)
std::cout << std::left << std::setw(25) << AddressBook[i].getName() <<
std::left << std::setw(25) << AddressBook[i].getPhoneNo() << AddressBook[i].getEmailAddress() << '\n';
break;
case 5:
quit = true;
break;
default:
std::cout << "Invalid option\n";
break;
}
}
}
|