Wednesday, March 14, 2012

RESULT MANAGEMENT SYSTEM


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

# define EMP_FILE "empdata.txt"
# define TMP_FILE "tmpdata.txt"

# define PWD "nitz\0"

int main(int, char**);
void welcome_message(void);
void get_password(void);
void main_menu(void);
void manage_student(void);
void add_student(void);
void delete_student(void);
void edit_student(void);
void search_student(void);
void student_menu(void);
void exit_message(void);

int main(int argc, char *argv[])
{
            char menuOpt;
            int isExit = 0;
            welcome_message();
            if(argc > 1)
            {
                        clrscr();
                        printf("%s : Start Up Error >>> ", argv[0]);
                        fflush(stdin);
                        getch();
                        return (EXIT_FAILURE);
            }
            get_password();
            do
            {
                        main_menu();
                        fflush(stdin);
                        menuOpt = (char)getch();
                        switch(menuOpt)
                        {
                                    case '1':
                                    {
                                                manage_student();
                                                break;
                                    }
                                    case '2':
                                    {
                                                isExit = 1;
                                                break;
                                    }
                                    default:
                                    {
                                                clrscr();
                                                printf("Invalid Option!");
                                                fflush(stdin);
                                                getch();
                                                break;
                                    }
                        }
            }while(isExit == 0);
            exit_message();
            fflush(stdin);
            getch();
    return (EXIT_SUCCESS);
}

void welcome_message(void)
{
            clrscr();
            printf("\t\t\t\t****WELCOME****\n\n");
            printf("\t\t\t\t**TO**\n\n");
            printf("\t\t    **NITZ RESULT MANAGEMENT SYSTEM**\n\n");
            printf("\n\n\n\n\n\n\n\nPlease press any key to continue");
            fflush(stdin);
            getch();
}

void main_menu(void)
{
            clrscr();
            printf("                MENU\n");
            printf("                ----\n");
            printf("     [1] : Manage Students Result\n");
            printf("     [2] : Exit\n\n");
            printf(" >>> ");
}

void manage_student(void)
{
            char menuOpt;
            int isExit = 0;
            do
            {
                        student_menu();
                        fflush(stdin);
                        menuOpt = (char)getch();
                        switch(menuOpt)
                        {
                                    case '1':
                                    {
                                                add_student();
                                                break;
                                    }
                                    case '2':
                                    {
                                                delete_student();
                                                break;
                                    }
                                    case '3':
                                    {
                                                edit_student();
                                                break;
                                    }
                                    case '4':
                                    {
                                                search_student();
                                                break;
                                    }
                                    case '5':
                                    {
                                                isExit = 1;
                                                fflush(stdin);
                                                break;
                                    }
                                    default:
                                    {
                                                clrscr();
                                                printf("Invalid Option!");
                                                getch();
                                    }
                        }
            }while(isExit == 0);
}

void add_student(void)
{
            char name[50];
            char branch[100];
            char grade[11];
            char id[5];
            float tmarks;
            float percentage;
            FILE *fp;
            fp = fopen(EMP_FILE, "a+");
            do
            {
                        fscanf(fp, "%[^/]%*c%[^/]%*c%[^/]%*c%[^/]%*c%f %f%*c", name, branch, grade, id, &tmarks, &percentage);
                        if(feof(fp))
                        {
                                    break;
                        }
            }while(!(feof(fp)));
            clrscr();
            printf("Name          : ");
            fflush(stdin);
            gets(name);
            printf("Branch        : ");
            fflush(stdin);
            gets(branch);
            printf("Grade         : ");
            fflush(stdin);
            gets(grade);
            printf("ID            : ");
            fflush(stdin);
            gets(id);
            printf("Total Marks   : ");
            fflush(stdin);
            scanf("%f", &tmarks);
            printf("Percentage    : ");
            fflush(stdin);
            scanf("%f", &percentage);
            fprintf(fp, "%s/%s/%s/%s/%.2f %.2f\n", name, branch, grade, id, tmarks, percentage);
            fflush(fp);
            fclose(fp);
            clrscr();
            printf("Done Entering New Record >>> ");
            getch();
}

void delete_student(void)
{
            char name[50];
            char branch[100];
            char grade[11];
            char id[5];
            char idDel[5];
            float tmarks;
            float percentage;
            FILE *fp1;
            FILE *fp2;
            fp1 = fopen(EMP_FILE, "r");
            fp2 = fopen(TMP_FILE, "w");
            clrscr();
            printf("Enter Student ID to Delete : ");
            fflush(stdin);
            gets(idDel);
            do
            {
                        fscanf(fp1, "%[^/]%*c%[^/]%*c%[^/]%*c%[^/]%*c%f %f%*c", name, branch, grade, id, &tmarks, &percentage);
                        if(feof(fp1))
                        {
                                    break;
                        }
                        if((strcmp(id, idDel)) != 0)
                        {
                                    fprintf(fp2, "%s/%s/%s/%s/%.2f %.2f\n", name, branch, grade, id, tmarks, percentage);
                        }
            }while(!(feof(fp1)));
            fflush(fp2);
            fclose(fp1);
            fclose(fp2);
            remove(EMP_FILE);
            rename(TMP_FILE, EMP_FILE);
            clrscr();
            printf("Done Deleting >>> ");
            fflush(stdin);
            getch();
}

void edit_student(void)
{
            char name[50];
            char branch[100];
            char grade[11];
            char id[5];
            char idEdit[5];
            float tmarks;
            float percentage;
            FILE *fp1;
            FILE *fp2;
            fp1 = fopen(EMP_FILE, "r");
            fp2 = fopen(TMP_FILE, "w");
            clrscr();
            printf("Enter student ID to Edit : ");
            fflush(stdin);
            gets(idEdit);
            do
            {
                        fscanf(fp1,"%[^/]%*c%[^/]%*c%[^/]%*c%[^/]%*c%f %f%*c", name, branch, grade, id, &tmarks, &percentage );
                        if(feof(fp1))
                        {
                                    break;
                        }
                        if((strcmp(id, idEdit)) == 0)
                        {
                                    clrscr();
                                    printf("Enter new total marks : ");
                                    fflush(stdin);
                                    scanf("%f", &tmarks);
                                    printf("Enter the new number percentage : ");
                                    fflush(stdin);
                                    scanf("%f", &percentage);
                        }
            fprintf(fp2, "%s/%s/%s/%s/%.2f %.2f\n", name, branch, grade, id, tmarks, percentage);
            }while(!(feof(fp1)));
            fflush(fp2);
            fclose(fp1);
            fclose(fp2);
            remove(EMP_FILE);
            rename(TMP_FILE, EMP_FILE);
            clrscr();
            printf("Done Editing >>> ");
            fflush(stdin);
            getch();
}

void search_student(void)
{
            char name[50];
            char branch[100];
            char grade[11];
            char id[5];
            char idSearch[5];
            float tmarks;
            float percentage;
            FILE *fp;
            fp = fopen(EMP_FILE, "r");
            clrscr();
            printf("Enter the Student ID to Search : ");
            fflush(stdin);
            gets(idSearch);
            do
            {
                        fscanf(fp, "%[^/]%*c%[^/]%*c%[^/]%*c%[^/]%*c%f %f%*c", name, branch, grade, id, &tmarks, &percentage);
                        if(feof(fp))
                        {
                                    break;
                        }
                        if((strcmp(id, idSearch)) == 0)
                        {
                                    clrscr();
                                    printf("Name              : %s\n", name);
                                    printf("Branch            : %s\n", branch);
                                    printf("Grade             : %s\n", grade);
                                    printf("ID                : %s\n", id);
                                    printf("Total Marks       : %.2f\n", tmarks);
                                    printf("Percentage        : %.2f\n\n", percentage);
                                    printf("Press ENTER to continue >>> ");
                                    fflush(stdin);
                                    getch();
                        }
            }while(!(feof(fp)));
            fclose(fp);
}
void get_password(void)
{
            char password[7];
            do
            {
                        clrscr();
                        printf("Enter Password >>> ");
                        fflush(stdin);
                        gets(password);
            }while((strcmp(password, PWD)) != 0);
}

void student_menu(void)
{
            clrscr();
            printf("               MENU\n");
            printf("               ----\n");
            printf("    [1] : Add Student\n");
            printf("    [2] : Delete Student\n");
            printf("    [3] : Edit Student\n");
            printf("    [4] : Search Student\n");
            printf("    [5] : Go Back\n\n");
            printf(" >>> ");
}

void exit_message(void)
{
            clrscr();
            printf("THANK YOU!\n");
            printf(">>> ");
}

No comments:

Post a Comment