write a program that prompts the user to enter a social security number in the f
ID: 3758651 • Letter: W
Question
write a program that prompts the user to enter a social security number in the format ddd-dd-dddd, where d is a digit. here are sample example runs:
Enter a SSS: 232-23-5435
232-23-5435 is a valid S.S number.
Enter a SSS: 23-23-5435
23-23-5435 is an Invalid SS number.
Check SSN: Write a program for Programming Exercise 4.23 on p. 154 in the textbook. Additional notes or specifications:
Read the input as a single string.
Check the string to be sure that it is of the correct length (11).Display an appropriate error message if the string is not the correct length.
Check each symbol in the string for the appropriate ASCII value or range.Display an appropriate error message if the string contains any invalid symbols.
If the SSN is valid display an appropriate message.
Testing:Run the program for:
123-45-6789
Another SSN of your choice (do not use yours!)
An input with the incorrect length
An input with an incorrect symbol.
Explanation / Answer
#include<iostream>
#include<string.h>
using namespace std;
int main(){
string ssn;
cout<<"Enter a SSS: ";
cin>>ssn;
int flag = 0;
if(ssn.length() == 11) {
if(ssn[3] != '-' || ssn[6] != '-') {
flag=1;
}
for (int i=0; i<ssn.length(); i++) {
if (i!=3 && i!=6 && !(ssn[i]>='0' && ssn[i] <= '9')) {
flag =1;
break;
}
}
} else {
flag =1;
}
if(flag == 0) {
cout<<ssn<<" is a valid SS number ";
}
else {
cout<<ssn<<" is invalid SSN number";
}
cout<<" ";
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.