I declare some variables:
int area_code;
int telephone_number;
when I get the input from user like:
cout << "Enter the area code";
cin >> area_code;
cout << "Enter your local telephone number";
cin >> telephone_number;
now when I want to display them, if their phone number is 6152222222 it should be displayed like:
615-222-2222
for the first part I can do like:
cout << area_code << "-";
but I don’t know how can I separate them with the dash for that one variable?
You can use math to calculate the area code and the local exchange code for your number:
6152222222 / 10000drops the last four digits, giving you6152221000gives you615; taking the remainder of the division by1000gives you222.Please note that using an
intto represent a telephone number limits your ability to store some more exotic phone numbers, such as1-800-SOMETHING. Using a class encapsulating astringwith additional validations may prove to be a better alternative:This produces the expected output on ideone.