0

How to validate the User Input based on the Student Number.

Scenario:

When the user adds the Student with the number 2015. The program will not accept another student id number 2015.

 void add_student(){
        int isFound = 0;
            FILE *fp;  // to open a file, fopen, fclose.
            fp = fopen("record.txt","ab+");  //fopen() function creates the file if it does not exist. --ab+ Open a binary file in append mode for writing at the end of the file.
            fflush(stdin);
                printf("\nStudent Name: ");gets(students.name);
                printf("\nStudent ID Number: ");scanf("%s",students.ID);
                printf("\nCourse: ");scanf("%s",students.course);
                printf("\nYear Level: ");scanf("%d",&students.yrlvl);
                printf("\nAge: ");scanf("%d",&students.age);
    
            FILE *fa;
            fa = fopen("record.txt","r");
            while(fread(&students,sizeof(students),1,fa) == 1) // 
            {
                if(strcmp(students.ID,students.ID) == 0) // Check if the user already in the file
                {
                    isFound = 1;
                    break;
                }
            }
            if(isFound == 1)
            {
                printf("\nStudent ID Number: %s already in the System. Please select Edit to modify same Student ID", students.ID);
    
            }
            else
            {
                fwrite(&students, sizeof(students), 1, fp); // Add Student 
printf("\nAdded Successfully");
            }
            fclose(fa);
    
                
            fclose(fp);// close the file
    
        return; 
    }

This is the all data from the file. enter image description here

When I add another student with the number 2016 this is the error

enter image description here

anna
  • 41
  • 5
  • 1
    First of all, passing an input-only stream (like `stdin`) to `fflush` is explicitly mentioned in the C specification as leading to *undefined behavior*. Secondly, `gets` is so [dangerous](https://stackoverflow.com/questions/1694036/why-is-the-gets-function-so-dangerous-that-it-should-not-be-used) that it has even been removed from the C language. Thirdly, almost never mix `scanf` with `fgets(..., stdin)`, better stick with *only* `fgets` (and almost forget that`scanf` exists), then use e.g. `sscanf` if you need to parse the contents of the input you just read. – Some programmer dude Sep 30 '22 at 12:18
  • 1
    Please provide a [mre], read [ask], take the [tour], provide textual data as text, not as picture of text, explain what file handling has to do with this. Clarify whether the screenshots are file content (which I doubt) or a screenshot of the program. Also whether it is about the program running as expected or as you observe it with your code. – Yunnosch Sep 30 '22 at 12:19
  • Also always check that functions that can fail (like `fopen`) doesn't actually fail. Be consistent with your code indentation, to make the code easier to read and understand. – Some programmer dude Sep 30 '22 at 12:20

1 Answers1

0

You are using the same single struct buffer to gather information about a new student to also scan through the file of existing student records.

Maybe you can to use a different buffer for each operation.

Some problems are too-o-o-o simple.

The error message should have made you think, "Where did 2015 come from?"

Fe2O3
  • 6,077
  • 2
  • 4
  • 20