if check SSN Write a program that prompts the user to enter

if (check SSN) Write a program that prompts the user to enter a social security number in the format ddd- dd- dddd, where d is a digit using C++

number is valid SSN number or not valid

Solution

#include <iostream>
#include <string>

using namespace std;

const int SSN_LENGTH = 11;
int main()
{
bool check = true;
string userSocial;


cout << \"Enter Social Security Number (ddd-dd-dddd): \";
cin >> userSocial;

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

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

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

if (!check)
{
cout << \"Invalid SSN\" << endl;
}
else
{
cout << \"Valid SSN\" << endl;
}

return 0;
}

OUTPUT:
Enter Social Security Number (ddd-dd-dddd): 123554
Invalid SSN

Enter Social Security Number (ddd-dd-dddd): 123-12-1234
Valid SSN

if (check SSN) Write a program that prompts the user to enter a social security number in the format ddd- dd- dddd, where d is a digit using C++ number is valid
if (check SSN) Write a program that prompts the user to enter a social security number in the format ddd- dd- dddd, where d is a digit using C++ number is valid

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site