int x;
cin >> x;
//If user writes 05 in x. Then I want that 05 should be printed when we cout << x;
//If user writes 005 when prompted for x then printing x should print 005, because 005 may be a telephone extension.
Is there any way?

Take the input in as a string.

#include <string> // to use the string class
#include <iostream>

using namespace std;

int main()
{
    string input;
    cout << "Enter number: ";
    getline(cin, input);
    cout << "You entered: " << input << endl;
    cin.get();  // pause program
    return 0;
}

if your using a telephone #, it sounds like you should be using a string instead of an int for this situation.

std::string strExt;

std::cin >> strExt << std::endl;

Well, thanks but is there any other alternative?

as far as i know no. Why do you want the extension as an integer?

Because it's required in the Assignment.

Because it's required in the Assignment.

Then I suspect you've misinterpreted the assignment. Could you post it?

    int x;

    cin >> x;
    cout << setw(3) << setfill('0') << x;



This might work for you.

Then I suspect you've misinterpreted the assignment. Could you post it?

That's what i'm thinking. The professor is probably meaning the Area code should be integers only, yet storage to be used will be a string :P

This might work for you.

But then again, maybe not. You don't know beforehand how many leading 0s there are. And there is no way to find out unless it's a string.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.