Saturday, October 15, 2016

Student Grading Database System in C

A simple program that I wrote to show database development using Binary File in C programming language. I called this application Student Grading Database System in C which enables the user to add, edit, delete, view and quit student grade records it stores the record of the student in the binary file. I am using Dev C++ as my C compiler but the I code can also run in a typical Borland Turbo C or Turbo C++ compiler in DOS. I hope you will find my work useful. Thanks a lot.

Add me at Facebook my addressis jakerpomperada@gmail.com and jakerpomperada@yahoo.com. 

My email address are the following jakerpomperada@gmail.com and jakerpomperada@yahoo.com. 

My mobile number here in the Philippines is 09173084360.











Sample Program Output


Program Listing

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
#include <ctype.h>

main( )
{
FILE  *fp, *ft ;
char  another, choice ;
struct student
{
char  stud_id_no[300];
char  name[300];
char  course[300];
char  subject[300];
float prelim,midterm,endterm,final_grade;
} ;
struct student grade;
char student_id[300];
long int  recsize;
    int flag=0;

fp = fopen ("GRADE_DB.DAT", "rb+" ) ;

if ( fp == NULL )
{
fp = fopen ( "GRADE_DB.DAT", "wb+" ) ;

if ( fp == NULL )
{
puts ( "Cannot open file" ) ;
exit(0) ;
}
}

recsize = sizeof ( grade ) ;

while ( 1 )
{
        system("CLS");
        printf("\n");
printf("\n======================================");
printf("\n");
printf("\nSTUDENT GRADING DATABASE SYSTEM ");
printf("\n");
printf("\n======================================");
printf("\n\n");
printf ( "1. INSERT STUDENT RECORD" ) ;
printf("\n");
printf ( "2. BROWSE STUDENT RECORD" ) ;
printf("\n");
printf ( "3. EDIT STUDENT RECORDS" ) ;
printf("\n");
printf ( "4. FIND STUDENT RECORDS" ) ;
printf("\n");
printf ( "5. REMOVE STUDENT RECORDS" ) ;
printf("\n");
printf ( "6. QUIT PROGRAM" ) ;
printf("\n\n");
printf ("SELECT YOUR OPTION :=> " ) ;
fflush (stdin) ;
choice = getche() ;
switch ( choice )
{
case '1' :

fseek ( fp, 0 , SEEK_END ) ;
another = 'Y' ;

while ( another == 'Y' )
{
system("cls");
printf("\n\n");
printf("=== INSERT NEW STUDENT GRADE RECORD ===");
printf("\n\n");
printf("Enter Student ID Number      : ");
                    scanf("%s",&grade.stud_id_no);
printf("Enter Student Name           : ");
fflush (stdin) ;
gets(grade.name);
printf("Enter Course                 : ");
fflush (stdin) ;
gets(grade.course);
                    printf("Enter Subject                  : ");
fflush (stdin) ;
gets(grade.subject);
printf("Enter Prelim Grade           : ");
scanf("%f",&grade.prelim);
printf("Enter Midtem Grade           : ");
scanf("%f",&grade.midterm);
printf("Enter Endterm Grade           : ");
scanf("%f",&grade.endterm);
grade.final_grade = (grade.prelim * 0.30) + (grade.midterm * 0.30) + (grade.endterm * 0.40);
printf("\n");
                    printf("\n Student Final Grade         : %2.0f",grade.final_grade);
fwrite (&grade, recsize, 1, fp ) ;
printf("\n\n");
printf ( "\nAdd New Student Record (Y/N)  : " ) ;
fflush (stdin) ;
another = toupper(getche()) ;
}

break ;

case '2' :
           system("cls");
rewind ( fp );
printf("\n\n");
                printf("=== VIEW STUDENT GRADE RECORD ===");
                printf("\n\n");
while ( fread ( &grade, recsize, 1, fp ) == 1 )
            {
                printf("\n");
                printf("\n ID  Number          : %s",grade.stud_id_no);
                printf("\n Name                : %s",grade.name);
                printf("\n Course              : %s",grade.course);
                printf("\n Subject             : %s",grade.subject);
                printf("\n Prelim Grade        : %2.0f",grade.prelim);
                printf("\n Midterm Grade       : %2.0f",grade.midterm);
                printf("\n Endterm Grade       : %2.0f",grade.endterm);
                printf("\n");
                grade.final_grade = (grade.prelim * 0.30) + (grade.midterm * 0.30) + (grade.endterm * 0.40);
                printf("\n Student Final Grade         : %2.0f",grade.final_grade);
     }
                printf("\n\n");
                system("pause");
break ;

case '3' :
   another = 'Y' ;
while ( another == 'Y' )
{
   system("cls");
           printf("=== EDIT STUDENT GRADE RECORD ===");
           printf("\n\n");
printf("Enter Student ID Number      : ");
                    scanf("%s",&student_id);
rewind ( fp ) ;
printf("\n");

while ( fread ( &grade, recsize, 1, fp ) == 1 )
{
if ( strcmp ( grade.stud_id_no, student_id) == 0 )
{
printf("Enter Student ID Number      : ");
                            fflush (stdin) ;
                            gets(grade.stud_id_no);
printf("Enter Student Name     : ");
fflush ( stdin ) ;
gets(grade.name);
       printf("Enter Course           : ");
fflush ( stdin ) ;
gets(grade.course);
printf("Enter Subject          : ");
fflush ( stdin ) ;
gets(grade.subject);
printf("Enter Prelim Grade     : ");
scanf("%f",&grade.prelim);
printf("Enter Midtem Grade     : ");
scanf("%f",&grade.midterm);
printf("Enter Endterm Grade    : ");
scanf("%f",&grade.endterm);
printf("\n");
grade.final_grade = (grade.prelim * 0.20) + (grade.midterm * 0.30) + (grade.endterm * 0.50);
printf("\n Student Final Grade         : %2.0f",grade.final_grade);
fseek ( fp, - recsize, SEEK_CUR ) ;
fwrite ( &grade, recsize, 1, fp ) ;
break ;
}

}
if (strcmp(grade.stud_id_no,student_id) != 0 )
                    {
                        printf("\n\n");
                        printf("No Student Record in the Database.");
                        printf("\n");
                        system("pause");
                        break;
                    }
                    printf("\n\n");
printf ( "\nEdit Another Student Record (Y/N) : " ) ;
fflush ( stdin ) ;
another = toupper(getche());
}

break ;

case '4' :
          rewind ( fp ) ;
          another = 'Y' ;
while ( another == 'Y' )
{
   system("cls");
   printf("=== Find Student Records ===");
   printf("\n\n");
printf("Enter Student ID Number      : ");
                    scanf("%s",&student_id);
printf("\n");
rewind ( fp ) ;
while ( fread ( &grade, recsize, 1, fp ) == 1 )
{
if ( strcmp (grade.stud_id_no,student_id) == 0 )
{
                    printf("\n");
printf("\n ID Number           : %s",grade.stud_id_no);
printf("\n Name                : %s",grade.name);
printf("\n Course              : %s",grade.course);
           printf("\n Subject             : %s",grade.subject);
                    printf("\n Prelim Grade        : %2.0f",grade.prelim);
                    printf("\n Midterm Grade       : %2.0f",grade.midterm);
                    printf("\n Endterm Grade       : %2.0f",grade.endterm);
                    printf("\n");
                    grade.final_grade = (grade.prelim * 0.30) + (grade.midterm * 0.30) + (grade.endterm * 0.40);
                    printf("\n Student Final Grade         : %2.0f",grade.final_grade);
                    printf("\n\n");
                    system("pause");
                     break;
}
                    }

                    if (strcmp(grade.stud_id_no,student_id) != 0 )
                    {
                        printf("\n\n");
                        printf("No Student Record found in the Database.");
                        printf("\n");
                        system("pause");
                        break;
                    }
                    printf("\n\n");
printf ( "\n Find Another Student Record (Y/N) : " ) ;
fflush ( stdin ) ;
another = toupper(getche());
}

break ;

case '5' :
another = 'Y' ;
while ( another == 'Y' )
{
system("cls");
                    printf("=== REMOVE STUDENT GRADE RECORD ===");
                    printf("\n\n");
printf("Enter Student ID Number      : ");
                    scanf("%s",&student_id);
printf("\n");
ft = fopen ( "TEMP.DAT", "wb" ) ;
rewind ( fp ) ;
while ( fread ( &grade, recsize, 1, fp ) == 1 )
{
if ( strcmp (grade.stud_id_no, student_id) != 0 )
fwrite ( &grade, recsize, 1, ft ) ;
                        else
                            flag=1;
                    }
fclose ( fp ) ;
fclose ( ft ) ;
remove ( "GRADE_DB.DAT" ) ;
rename ( "TEMP.DAT", "GRADE_DB.DAT" ) ;
   fp = fopen ( "GRADE_DB.DAT", "rb+" ) ;

                  if(flag==1) {
                        printf("\n\n");
                        printf("Record Successfully Deleted From the Database.");
                         printf("\n");
                       system("pause");
                    }
else if (flag!=1) {
                        printf("\n\n");
                        printf("Record Not Found in the Database.");
                        printf("\n");
                        system("pause");

                    }
                    printf("\n\n");
printf( "Remove Another Student Record (Y/N) : " ) ;
fflush (stdin) ;
another = toupper(getche());
}
break ;

case '6' :
fclose ( fp ) ;
printf("\n\n");
printf("   END OF PROGRAM    ");
printf("\n\n");
system("PAUSE");
exit(0);

}
}
}




No comments:

Post a Comment