Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8335495
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T03:41:40+00:00 2026-06-09T03:41:40+00:00

I declare some variables: int area_code; int telephone_number; when I get the input from

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T03:41:41+00:00Added an answer on June 9, 2026 at 3:41 am

    You can use math to calculate the area code and the local exchange code for your number:

    • 6152222222 / 10000 drops the last four digits, giving you 615222
    • Further dividing by 1000 gives you 615; taking the remainder of the division by 1000 gives you 222.

    Please note that using an int to represent a telephone number limits your ability to store some more exotic phone numbers, such as 1-800-SOMETHING. Using a class encapsulating a string with additional validations may prove to be a better alternative:

    class phone_number {
    string phone;
    public:
        phone_number(const string& p) {
            // validate p...
            if (p.size() != 10) {
                // Do something violent here...
                cerr << "The phone number is incorrect." << endl;
            }
            // Validate more things about the number before the assignment...
            phone = p;
        }
        friend ostream& operator<<(ostream &os, const phone_number& p);
    };
    
    ostream& operator<<(ostream &os, const phone_number& pn) {
        const string &p(pn.phone);
        os << "(" << p.substr(0, 3) << ")" << p.substr(3, 3) << "-" << p.substr(6);
        return os;
    }
    
    int main() {
        phone_number p = phone_number("6152784567");
        cout << p << endl;
        return 0;
    }
    

    This produces the expected output on ideone.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Why do some people declare make their variables static, like so: char baa(int x)
In my method, I declare some variables, including int blockCount; . I call this
At some places I have seen variables declared like this const int &var1; extern
I want to declare some global variables and global constants. Normally, I would put
I want to declare some application level variables. These variables changes with values on
I'd like to declare some members of a module accessible to an entire namespace,
I would like to declare some integer constants in PowerShell. Is there any good
I want to declare two types of variables in for's init-statement. like the following
I'm writing some quick code to try and extract data from an mp3 file
#include <stdio.h> double seed=0.579832467; main(ac, av) int ac; char *av[]; { /* declare variables

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.