cout << "Enter Social Security Number (###-##-####): ";
cin >> user_social;
if (user_social.length () != SSN_LENGTH)
{
check = false;
cout << "Problem: You must type exactly 11 characters." << endl;
continue;
}
if (user_social [4] != '-' && user_social [7] != '-')
{
check = false;
cout << "Problem: The dashes are missing or are in the wrong spot." << endl;
}
else
{
user_social = user_social.erase (3, 1);
user_social = user_social.erase (5, 1);
for (int i = 0; i < user_social.length(); i++)
{
if (!isdigit (user_social [i]))
{
check = false; // this is the coding im having trouble with!!!!
cout << "Problem: Only digits are allowed in SSN." << endl; < >
}
}
}
if (check)
{
cout << "That is valid." << endl;
}
}
The check for dash is incorrect of the format [###-##-####]. Definatly it is at 4th and 7th possitions but string index starts from '0', so you should check at 3rd and 6th indices. if (user_social [3] != '-' && user_social [6] != '-')
cout << "Enter your SSN in this format: XXX-XX-XXXX";
cin >> user_social;
if (user_social.length() != SSN_LENGTH)
{
check = false;
cout << "Problem: You must type exacly 11 characters." << endl;
continue;
}
if (user_social[3] != '-' && user_social[4] != '-')
{
check = false;
cout << "Problem: The dashes are missing or are in the wrong spot." << endl;
continue;
}
else
{
user_social = user_social.erase (3, 1);
user_social = user_social.erase (5, 1);
}
for(int x = 0; x < user_social.length(); x++)
{
if (!isdigit (user_social[x]))
{
check = false;
cout << "Problem: Only digits allowed in SSN" << endl;
}
My if statement for the dashes are in the wrong place takes over and runs even if the dashes are in the right place and I can't get the if statement of if a user inputs a letter to tell them that only digits are allowed
Thanks that helps with the if statement for the dashes but I still can't get if a user inputs a letter anywhere it outputs that digits are only allowed in a Ssn but right now the dash problem statement comes up instead
I got it almost complete now I have to get it to only output one problem line right now it outputs the number of problem with Ssn for the letter on how many errors are wrong with the ssn
Here is my updated code but i have to adjust my for loop to only look at the characters of the string that involve digits but im unsure on how to do that.
cout << "Enter your SSN in this format: XXX-XX-XXXX";
cin >> user_social;
if (user_social.length() != SSN_LENGTH)
{
check = false;
cout << "Problem: You must type exacly 11 characters." << endl;
continue;
}
if (user_social[3] != '-' || user_social[6] != '-')
{
check = false;
cout << "Problem: The dashes are missing or are in the wrong spot." << endl;
continue;
}
for(int x = 0; x < user_social.length(); x++)
{
if (!isdigit(user_social[x]))
{
check = false;
continue;
}
}
cout << "Problem: Only Digits allowed in a SSN" << endl;
cout << "Enter your SSN in this format: XXX-XX-XXXX";
cin >> user_social;
if (user_social.length() != SSN_LENGTH)
{
check = false;
cout << "Problem: You must type exacly 11 characters." << endl;
continue;
}
if (user_social[3] != '-' || user_social[6] != '-')
{
check = false;
cout << "Problem: The dashes are missing or are in the wrong spot." << endl;
continue;
}
for(int x = 0; x < user_social.length(); x++)
{
if (!isdigit(user_social[x]))
{
check = false;
continue;
}
}
cout << "Problem: Only Digits allowed in a SSN" << endl;
Could you please edit that to use code tags. You've been asked several times already. Why do you want to make it harder for us to read your code? Why do you want to make it less likely that we'll take the time help you?