need help finishing

this is the program that i have so far. I am trying to finish it but i have some alternate test cases that i need to put into this program and im not sure how to do them properly.

if a user inputs
aaa-aa-aaaa

then i need to output

Problem: Only digits are allowed in a SSN

same goes for if they put a dash in the wrong spot or an underscore is used at the end this is the output i need.

Problem: The dashes are missing or are in the wrong spot.

#include <iostream>
#include <iomanip>
#include <string>
using namespace std;

const int SSN_LENGTH = 11;

string ReadAndValidateUserSocial (string userSocial);

int main ()
{
string userSocial;

ReadAndValidateUserSocial (userSocial);

return 0;
}

string ReadAndValidateUserSocial (string user_social)
{
bool check = false;

while (!check)
{
check = true;

cout << "Enter Social Security Number (###-##-####): ";
cin >> user_social;

if (user_social.length () != SSN_LENGTH)
{
check = false;
}

if (user_social [3] != '-' && user_social [6] != '-')
{
check = false;
}
else
{
user_social = user_social.replace (3, 1, "");
user_social = user_social.replace (5, 1, "");

for (int i = 0; i < user_social.length(); i++)
{
if (!isdigit (user_social [i]))
{
check = false;
}
}
}

if (!check)
{
cout << "Problem: You must type exactly 11 characters. << endl;
}
}

return user_social;
Please use code tags, so one can comment on your code. It's the "<>" button next to your edit box.
Quick guess user_social = user_social.replace (5, 1, ""); It should be 6 instead of 5
You can also use string::erase to erase the characters instead of just replacing them with empty strings:
1
2
user_social.erase(3, 1);
user_social.erase(5, 1); // 5 instead of 6 because of the last erase 


In terms of outputting the error messages, you can do those right on the spot:
1
2
3
4
5
6
if (user_social.length () != SSN_LENGTH)
{
    check = false;
    cout << "Not 11 characters";
    continue; // Back to the top of the loop
}

and similarly for the other one.
this helps alot in the program working properly but i still have one if statement that is giving me a hard time.

I need to create an if statement to tell the user if the user inputs a letter of any sort that it will give the user a error and return to the top of the loop.

Everything else is working properly
Topic archived. No new replies allowed.