/* The user is to enter a 9-digit social security number and check whether the number entered by the user is a valid SSN from a list of SSNs in a text file. */
Code:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int snn_num, user_ssn, ssn3, sum = 0;
FILE * ssn;
ssn = fopen("ssn_in.txt", "r");
if (ssn == NULL ) { // Checking if the file ssn_in.txt exists
printf("The file ssn_in.txt does not exist\n");
fclose(ssn);
}
else {
printf("The Social Security Numbers are:\n");
while (fscanf(ssn, "%d", &snn_num) != EOF) {
sum = sum + 1;
printf("%d\n",snn_num);
}
}
printf("Enter a 9-digit social security number: "); // part 2
scanf("%d", &user_ssn);
if ((user_ssn < 100000000) || (user_ssn >= 1000000000)) // the input has to be greater than and equal to 100000000 and less than 1000000000 or else exit program
{
printf("The number (%d) you entered is not a valid 9-digit social security number.\n", user_ssn);
return 0;
}
while (fscanf(ssn,"%d", &ssn3) != EOF) { // if the input is within the range, the program will scan
if (user_ssn == ssn3 )
printf("The SSN you entered (%d) is among the (%d) SSNs in the file ssn_in.txt", user_ssn, sum);
else if (user_ssn != ssn3 )
printf("The SSN you entered (%d) is NOT among the (%d) SSNs in the file ssn_in.txt", user_ssn, sum);
}
fclose(ssn);
}