Code:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <conio.h>
struct qpi
{
int studid;
char name[100];
char sub[2];
char totqpi[2];
char rank[8];
char totunit[2];
};
void new_stud(void);
void view_stud(FILE *);
int main()
{
char choose1;
intro1:
printf("ACADEMIC HONORS COMPUTATION");
printf("\n\n");
{
FILE *open;
if ((open = fopen("fileqpi.txt", "r")) != NULL)
printf("\n");
else
{
printf("The file \"fileqpi.txt\" cannot be opened\n");
printf("It is either the file does not exist or there is something");
printf("\nwrong with the file");
}
fclose(open);
}
printf("\tCompute a student's QPI........c\n");
printf("\tView the list..................v\n");
printf("\tLeave..........................l\n");
printf("\tEnter the letter of your choice: ");
scanf("%c", &choose1);
fflush(stdin);
if (choose1 == 'c')
new_stud();
else if(choose1 == 'v')
{
printf("Viewing database...\n");
{
FILE *open;
if ((open=fopen("fileqpi.txt", "r")) != NULL)
printf("\n");
else
{
printf("The file \"fileqpi.txt\" cannot be opened\n");
printf("It is either the file does not exist or there is something");
printf("\nwrong with the file");
}
view_stud(open);
fclose(open);
}
}
else if (choose1 == 'l' )
{
system("cls");
exit(1);
}
else if((choose1 != 'a') || (choose1 != 'l') || (choose1 != 'v'))
{
printf("Error! The letter you inputted is not part of the choices");
printf("Please restart the program if you want to continue");
}
goto intro1;
}
void new_stud(void)
{
FILE *fileqpi;
int sub2, i;
float grde, unit, tot, mult, sum, qou;
struct qpi file={ 0, "", "", "", "", "" };
system("cls");
if ((fileqpi = fopen( "fileqpi.txt", "r+" ) ) == NULL )
{
printf( "Error! Something happened while accessing the file\n" );
main();
}
else
{
printf("\t\tNOTICE:\nBefore starting, ");
printf("\nmake sure there is no INC in any of the student's subjects.\n\n");
printf("Enter student's ID number (To quit, type 0): ");
scanf("%d", &file.studid);
fflush(stdin);
while(file.studid != 0)
{
printf("Starting QPI computation...\n Enter number of subjects: ");
scanf("%d", &sub2);
for(i=0; i<sub2; ++i)
{
printf("Grade of Subject %d: ", i+1);
scanf("%f", &grde);
printf("Number of Units for Subject %d: ", i+1);
scanf("%f", &unit);
mult = grde*unit;
sum += mult;
}
printf("Input total number of units: ");
scanf("%f", &tot);
qou = sum/tot;
printf("QPI is %.2f", qou);
if((qou>=3.70) && (qou<=4.00))
printf("\nStudent is qualified for First Honors");
else if((qou>=3.25) && (qou<3.70))
printf("\nStudent is qualified for Second Honors");
else
printf("\nStudent is not qualified to be an Honor");
printf("\n\nSaving to files...\n");
printf("Enter complete name of student:\n");
fscanf(stdin, "%s", file.name);
fflush(stdin);
printf("Enter total number of subjects:\n");
fscanf(stdin, "%s", file.sub);
fflush(stdin);
printf("Enter total number of units:\n");
fscanf(stdin, "%s", file.totunit);
fflush(stdin);
printf("Enter QPI of student:\n");
fscanf(stdin, "%s", file.totqpi);
fflush(stdin);
printf("Enter rank of student:\n");
fscanf(stdin, "%s", file.rank);
fflush(stdin);
fseek(fileqpi, (file.studid-1) * sizeof(struct qpi), SEEK_SET );
system("cls");
printf("Enter Enter student's ID number (To quit, type 0): ");
scanf("%d", &file.studid);
fflush(stdin);
}
fclose(fileqpi);
}
system("cls");
}
void view_stud(FILE *fileqpi)
{
FILE *viewtext;
struct qpi file = {0, "", "", "", "", ""};
system("cls");
if((viewtext = fopen("fileqpi.txt", "w"))==NULL)
{
printf("Error! Something happened while accessing the file");
fclose(viewtext);
main();
}
else
{
rewind(fileqpi);
fprintf(viewtext, "%-11s%-22s%-22s%-15s%-20s%-20s%\n", "ID No.", "Name", "No. of Subjects", "No. of Units", "QPI Average", "Rank");
while ( !feof( fileqpi ) )
{
fread( &file, sizeof( struct qpi ), 1, fileqpi );
if (file.studid != 0)
fprintf(viewtext, "%-11d%-22s%-22s%-15s%-20s%-20s%\n", file.studid, file.name, file.sub, file.totunit, file.totqpi, file.rank);
}
fclose(viewtext);
}
printf( "Creating database.txt file for viewing.....\n" );
system( "notepad fileqpi.txt" );
system( "cls" );
}
If you want to run it, make sure that there is a file named "fileqpi.txt" already saved in your documents or else it won't run normally... Thanks for helping (if someone will help.