Hello I am currently making a program for a database which includes an SSN. The user can input the ssn but I want it in xxx-xx-xxxx and to make sure the user ddoes this have the user input 3 numbers then 2 numbers then 4 numbers in seperate cin statements:

cout<<"Please input the new SSN for the employee: ";     
		cout<<endl<<"Input the first 3 digits: ";
		cin>>SSN[1]; 
		cout<<endl<<"Input the next 2 digits: ";
		cin>>SSN[1];
		cout<<endl<<"Input the last 4 digits: ";
		cin>>SSN[1];
		cout<<endl<<"Social Security Number successfully set to "<<SSN[1];

But I need each cin to add to the end of the SSN string. I know that the ios::ate can be done for fie io however I have never seen this used on strings in cin satements before. Is this possible? Thanks

ios::ate is (primarily) for file writing, it truncates---overwrites previous file contents. ios::app appends to previous file contents.

What type is SSN? If its a STL string then you can do something like this, where the += operator will append input to end of current string once it is validated:

string dash = "-";
string SSN;
string temp;
bool invalidInput = true;
while(invalidInput)
{
  cout << "enter first three digits of SSN" << endl;
  cin >> temp;
  if(3 == temp.length())
    invalidInput = false;
}
invalidInput = true;
SSN = temp + dash;
while(invalidInput)
{
  cout << "enter middle 2 digits of SSN" << endl;
  cin >> temp;
  if(2 == temp.length())
    invalidInput = false;
}
invalidInput = true;
SSN += temp + dash;
while(invalidInput)
{
  cout << "enter last three digits of SSN" << endl;
  cin >> temp;
  if(3 == temp.length())
    invalidInput = false;
}
SSN += temp;
cout << SSN << endl;

Keep it simpler ;)

bool getSSN(string& ssn)
{
    while (cout << "Enter SSN as ddd-dd-dddd: ", cin >> ssn) {
        if (ssn.size() == 11 && ssn[3] == '-' && ssn[6] == '-') {
            int digits = 0;
            for (int i = 0; i < 11; ++i)
                if (isdigit(ssn[i])) digits++;
            if (digits == 9)
                return true;
        }
        cout << "Wrong SSN. Please try again...\n";
        cin.ignore(1000,'\n');
    }    
    return false;
}
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.