Good evening, basically I have a program that requires the following:
- Prompt the user to enter a SSN.
- Verify that it is in the correct format. xxx-xx-xxxx
- Verify that only digits are entered (no letters) for example a20-66-xx22 would give an error.
- Verify that the dashes are in the correct place.
I've been able to get pretty much everything done with the exception of the verification that there are no letters entered, no matter what I do it gives me an error. I have the input and length of the string, checked the number of digits and dash location, no problem, it's only the validation so there's only numbers and letters where I'm breaking my head trying to figure out.
#include <iostream>
#include <iomanip>
#include <string>
usingnamespace std;
constint SSN_LENGTH = 11;
int main ()
{
string Social (string userSocial);
string userSocial;
bool check = false;
cout << "Enter your SSN in this format: XXX-XX-XXXX" << endl;
cin >> userSocial;
if (userSocial.length () != SSN_LENGTH)
{
cout << "Problem: You must type exactly 11 characters." << endl;
return 0;
}
/* my problem is here, tried loops, ifs, string comparison and I still
can't get it done. Everything else works the way I want it to work.
{
cout << "Problem: Only digits are allowed in a SSN" << endl;
return 0;
}
*/
if (userSocial.length() == SSN_LENGTH)
{
if (userSocial[3] == '-' && userSocial[6] == '-')
{
for (int i = 0; i < userSocial.length(); i++)
{
if (isdigit (userSocial [i]))
{
check = true;
}
}
}
}
if (check == true)
{
cout << "That is valid." << endl;
}
if (check == false)
{
cout << "Problem: The dashes are missing or are in the wrong spot" << endl;
}
return 0;
}
I'm trusting the user on the other thread to not rip off this code.
By your logic if someone types w34-45-5678 then it will not error out. In first pass the check wont become true, but second pass it will and this SSN will be accepted which is wrong. Above code should fix this. Also, this way the loop breaks the moment it finds a non-digit.
#include <iostream>
//#include <iomanip>
#include <string>
#include <cctype> // for std::isdigit()
//using namespace std;
constint SSN_LENGTH = 11;
int main ()
{
std::string ssn;
std::cout << "Enter your SSN in this format: XXX-XX-XXXX: " ;
std::cin >> ssn;
if (ssn.length () != SSN_LENGTH)
{
std::cout << "Problem: You must type exactly 11 characters.\n" ;
return 1; // return 1 on error
}
if( ssn[3] != '-' || ssn[6] != '-' )
{
std::cout << "Problem: inorrect placement of dash.\n" ;
return 1 ; // return 1 on error
}
for( int i = 0 ; i < SSN_LENGTH ; ++i )
{
if( i==3 || i==6 ) continue ; // position for dash, skip this character
if( !std::isdigit( ssn[i] ) ) // if this is not a decimal digit
{
std::cout << "Problem: non-digit character in number.\n" ;
return 1 ; // return 1 on error
}
}
std::cout << "No problem: " << ssn << " is in the right format\n" ;
// implicit return 0 on no error
}
if(ssn[i]<'0' || ssn[i]>'9') // if this is not a decimal digit
{
std::cout << "Problem: non-digit character in number.\n" ;
return 1 ; // return 1 on error
}