Need help finishing this program

This program is working almost perfect i just am having trouble wit the last output below.

if a user inputs a letter anywhere in the SSN

then i need to output

Problem: Only digits are allowed in a SSN

The problem i am getting is if i have it coded below. it runs properly but also outputs :

The dashes are missing or are in the wrong spot. and i can't have that. I need each case to be seperate.


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

const int SSN_LENGTH = 11;

string ReadAndValidateUserSocial (string userSocial);

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

return user_social;
}

int main ()
{
string userSocial;

ReadAndValidateUserSocial (userSocial);

return 0;
}



EditReport

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] != '-')
It worked for a little while but i still can't get either if statement to work properly

here is my updated code:

#include <iostream>
#include <iomanip>
#include <cstring>
#include <cctype>
using namespace std;

const int SSN_LENGTH = 11;

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

while (!check)
{
check = true;

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

}


}
if(check)
{
cout << "That is valid." << endl;
}

return user_social;
}

int main()
{
char user_social[SSN_LENGTH];

ReadandValidateUserSocial(user_social);

return 0;
}


What do you mean by:
can't get either if statement to work properly
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
Please use code tags when posting code, to make it readable.
+1 for code tags. Very helpful for those who want to comment on your code and give you help.

In your if statement, you are checking for dashes in location 3 and 4. You want to check in locations 3 and 6.

Also, you should be "or"ing your dash checks, not "and"ing them. Right now the check will only fail when both locations 3 and 4 are not dashes.
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.

#include <iostream>
#include <iomanip>
#include <cstring>
#include <cctype>
using namespace std;

const int SSN_LENGTH = 11;

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

while (!check)
{
check = true;

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;

}
if(check)
{
cout << "That is valid." << endl;
return user_social;
}
return user_social;

}

int main()
{
char user_social[SSN_LENGTH];

ReadandValidateUserSocial(user_social);

return 0;
}


[#include <iostream>
#include <iomanip>
#include <cstring>
#include <cctype>
using namespace std;

const int SSN_LENGTH = 11;

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

while (!check)
{
check = true;

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;

}
if(check)
{
cout << "That is valid." << endl;
return user_social;
}
return user_social;

}

int main()
{
char user_social[SSN_LENGTH];

ReadandValidateUserSocial(user_social);

return 0;
}][/code]
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?
Last edited on
Topic archived. No new replies allowed.