Thread: Why can I not enter the last while loop?

  1. #1
    Registered User
    Join Date
    Jun 2016
    Posts
    16

    Why can I not enter the last while loop?

    /* 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);
    }

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    I think your biggest problem is that this loop generates so much output. For each number that doesn't match in the file it will say it's not okay, except for the one that matches. Just change where and how you report the results.

    Code:
    int user_ssn;
    int ssn3;
    int matchExists = 0;
    
    while (fscanf(ssn, "%d", &ssn3) != 1) {
       if (user_ssn == ssn3) {
          matchExists = 1;
          break;
       }
    }
    printf("The SSN you entered (%d) %s among the %d SSNs in the file\n", user_ssn, matchExists ? "is" : "is NOT", sum);
    Something like this works.
    Last edited by whiteflags; 01-27-2017 at 03:13 AM.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Now is a good time to learn how to indent code.
    Indent style - Wikipedia

    > printf("The file ssn_in.txt does not exist\n");
    > fclose(ssn);
    If the file didn't open, there is nothing to close.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Also you might want to call rewind(ssn); before your last loop, if you're really going to read the file multiple times. You shouldn't need to do that, but whatever.
    Last edited by whiteflags; 01-27-2017 at 06:07 AM.

  5. #5
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by whiteflags View Post
    Code:
    while (fscanf(ssn, "%d", &ssn3) != 1) {
    ...
    }
    Something like this works.
    You mean == 1 ?
    Otherwise I do not see how it could work
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to end loop by enter a negative number?
    By maymay2016 in forum C Programming
    Replies: 18
    Last Post: 04-05-2016, 05:16 PM
  2. Segmentation fault - won't enter for loop!
    By tomh16 in forum C Programming
    Replies: 19
    Last Post: 11-30-2011, 04:19 PM
  3. Replies: 4
    Last Post: 04-27-2010, 10:25 AM
  4. Replies: 9
    Last Post: 02-22-2009, 11:50 PM
  5. Loop until enter correct value
    By jchanwh in forum C Programming
    Replies: 2
    Last Post: 11-27-2001, 01:23 AM

Tags for this Thread