Need help finishing this program

Dec 3, 2013 at 4:53am
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

Dec 3, 2013 at 8:02am
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] != '-')
Dec 3, 2013 at 6:45pm
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;
}


Dec 4, 2013 at 6:27am
What do you mean by:
can't get either if statement to work properly
Dec 4, 2013 at 2:07pm
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
Dec 4, 2013 at 2:38pm
Please use code tags when posting code, to make it readable.
Dec 4, 2013 at 3:19pm
+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.
Dec 4, 2013 at 4:05pm
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
Dec 4, 2013 at 4:13pm
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
Dec 7, 2013 at 5:17am
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]
Dec 7, 2013 at 3:06pm
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 Dec 7, 2013 at 3:06pm
Topic archived. No new replies allowed.