Question #67700

Aphone number,such as(212) 767-8900.can be thought of as having three parts: the area code (212) the exchange (767), and the number (8900).write aprogram that uses a structure to store these three parts of a phone number separately. Call the structure phone. Create two structure variables of type phone. Initialize one and have the user input the number for the ather one. Then display both numbers.The interchange might look like this.

Expert's answer

#include <iostream>
#include <string>
#include <regex>
using namespace std;

struct Phone {
string AreaCode;
string Exchange;
string Number;
};

int main() {
struct Phone origin = {"212", "767", "8900"};
struct Phone userInput;
string userPhone;

cout << "\nPlease enter phone number in format: (xxx) xxx-xxxx\n";
getline(cin, userPhone);

regex reg("[\\s()-]+");
userPhone = regex_replace(userPhone, reg, "");

userInput.AreaCode = userPhone.substr(0,3);
userInput.Exchange = userPhone.substr(3,3);
userInput.Number = userPhone.substr(6,4);

cout << "\nOrigin structure" << endl
<< "AreaCode: " << origin.AreaCode << endl
<< "Exchange: " << origin.Exchange << endl
<< "Number: " << origin.Number << endl;

cout << "\nUser Input structure" << endl
<< "AreaCode: " << userInput.AreaCode << endl
<< "Exchange: " << userInput.Exchange << endl
<< "Number: " << userInput.Number << endl;

return 0;
}
LATEST TUTORIALS
APPROVED BY CLIENTS