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;
}

Edited 3 Years Ago by NathanOliver

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;

Because it's required in the Assignment.

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

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.

This article has been dead for over six months. Start a new discussion instead.