Code:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
FILE *grading;
float sci,math,eng,ave;
char name[50];
main()
{
int choice;
mainmenu:
{
system("CLS");
fflush(stdin);
printf("\n\n\n\n\n\n\t\t\t************************ ****");
printf("\n\t\t\t* Student Grading System *");
printf("\n\t\t\t****************************");
printf("\n\t\t\t [1] Add");
printf("\n\t\t\t [2] View");
printf("\n\t\t\t [3] Edit");
printf("\n\t\t\t [4] Search");
printf("\n\t\t\t [5] Delete");
printf("\n\t\t\t [6] Empty Record");
printf("\n\t\t\t [7] Exit");
printf("\n\t\t\t****************************");
printf("\n\t\t\tPlease Enter Your Choice : ");
scanf("%d",&choice);
switch (choice)
{
case 1 : goto add;
case 2 : goto view;
case 3 : goto edit;
case 4 : goto search;
case 5 : goto dele;
case 6 : goto empty;
case 7 :
printf("\n\n\n\t\t******************************** *****");
printf("\n\n\n\n\n\t\t\t Thank you!!!\n\n\n\n\n");
printf("\n\t\t************************************ *");
printf("\n\n\n\n\t\t press any key to exit...");
getch();
return 0;
default:
printf("\n\t\t\tInvalid Choice!!!\n\t\t\tPlease refer to the menu!!!");
system("PAUSE");
goto repeat;
}
}
add:
{
char choice;
system("CLS");
fflush(stdin);
printf("Enter Full Name : ");
scanf("%[^\t\n0-9]",&name);
printf("Enter grade in English : ");
scanf("%f",&eng);
printf("Enter grade in Math : ");
scanf("%f",&math);
printf("Enter grade in Science : ");
scanf("%f",&sci);
ave = (eng+math+sci)/3;
printf("\n-------------------------------\n");
printf("Your average is %.2f",ave);
/* SAVE FILE */
grading=fopen("record.txt","a+");
fprintf(grading,"%s %.2f %.2f %.2f %.2f\n",name,eng,math,sci,ave);
fflush(grading);
fclose(grading);
/*SHOW MESSAGE*/
printf("\n-------------------------------");
printf("\nRecord Added!");
printf("\n-------------------------------");
fflush(stdin);
repeat:
printf("\n\nDo you want to add another record?\n\n");
printf("[Y] for new a record or [N] to go back to main menu : ");
scanf("%c",&choice);
if((choice=='Y')||(choice=='y'))
{
goto add;
}
else if((choice=='N')||(choice=='n'))
{
goto mainmenu;
}
else
{
system("CLS");
printf("\nInvalid choice!!\n");
goto repeat;
}
view:
{
int ctr=0;
grading=fopen("record.txt","a+");
while(!feof(grading))
{
fscanf(grading,"%s %f %f %f %f",&name,&eng,&math,&sci,&ave);
if(feof(grading))
{
break;
}
printf("-------------------------------");
printf("\nName : %s\n",name);
printf("English : %.2f\n",eng);
printf("Math : %.2f\n",math);
printf("Science : %.2f\n",sci);
printf("Average : %.2f\n",ave);
ctr++;
}
fclose(grading);
printf("\n-------------------------------");
printf("\nPress any key to continue...");
getch();
goto mainmenu;
}
edit:
{
char tempdata[100],search[50],nname[50];
float nsci,nmath,neng;
int test,test2;
FILE *temp;
system("CLS");
temp=fopen("temprecord.txt","a+");
grading=fopen("record.txt","r");
printf("Enter the full name : ");
scanf("%s",&search);
do{
fscanf(grading,"%s %f %f %f %f",&name,&eng,&math,&sci,&ave);
test=stricmp(name,search);
if(feof(grading))
{
break;
}
else if(test!=0)
{
fprintf(temp,"%s %f %f %f %f\n",name,eng,math,sci,ave);
}
else
{
printf("%s has been found\n\n",name);
strcpy(nname,name);
neng=eng;
nmath=math;
nsci=sci;
test2=test;
}
}
while(!feof(grading));
if(test2==0)
{
printf("Current Name is %s , Enter New Name : ",nname);
scanf("%s",&name);
printf("Current grade is %.2f, Enter new grade in English : ",neng);
scanf("%f",&eng);
printf("Current grade is %.2f, Enter new grade in Math : ",nmath);
scanf("%f",&math);
printf("Current grade is %.2f, Enter new grade in Science : ",nsci);
scanf("%f",&sci);
ave = (eng+math+sci)/3;
printf("\n-------------------------------\n");
printf("Your average is %.2f\n",ave);
system("PAUSE");
fprintf(temp,"%s %.2f %.2f %.2f %.2f\n",name,eng,math,sci,ave);
printf("\n-------------------------------");
printf("\nRecord is updated!\n");
system("PAUSE");
}
else
{
printf("\n-------------------------------");
printf("Record not found!!");
}
remove("record.txt");
rename("tempreco.txt","record.txt");
printf("\n-------------------------------");
printf("\nPress any key to continue...");
goto mainmenu;
}
search:
{
char choice,search[50];
int ctr=0;
system("CLS");
fflush(stdin);
grading=fopen("record.txt","r+");
printf("Enter the full name : ");
scanf("%s",&search);
while(!feof(grading))
{
fscanf(grading,"%s %f %f %f %f",&name,&eng,&math,&sci,&ave);
}
if(feof(grading))
getch();
else if(strcmp(search,name)==1)
{
printf("-------------------------------");
printf("\nName : %s\n",name);
printf("English : %.2f\n",eng);
printf("Math : %.2f\n",math);
printf("Science : %.2f\n",sci);
printf("Average : %.2f\n",ave);
ctr++;
}
else
{
printf("-------------------------------\n");
printf("The name doesn't exist!!!");
}
fflush(stdin);
repeat2:
printf("\n-------------------------------");
printf("\nDo you want to search again?\n\n");
printf("[Y] for yes or [N] to go back to main menu: ");
scanf("%c",&choice);
if((choice=='Y')||(choice=='y'))
{
goto search;
}
else if((choice=='N')||(choice=='n'))
{
goto mainmenu;
}
else
system("CLS");
printf("\nInvalid choice!!\n");
goto repeat2;
}
dele:
{
char choice,tempdata[100],search[50],nname[50];
int test,ctr=0;
FILE *temp;
system("CLS");
temp=fopen("temprecord.txt","a+");
grading=fopen("record.txt","a+");
printf("-------------------------------\n");
printf("Name(s) :\n");
printf("-------------------------------\n");
while(!feof(grading))
{
fscanf(grading,"%s %f %f %f %f",&nname,&eng,&math,&sci,&ave);
}
if(feof(grading))
{
printf("%s\n",nname);
}
printf("\n-------------------------------\n");
grading=fopen("record.txt","r");
fflush(stdin);
printf("Enter the full name : ");
scanf("%s",&search);
printf("\n-------------------------------\n");
do
{
fscanf(grading,"%s %f %f %f %f",&name,&eng,&math,&sci,&ave);
test=stricmp(name,search);
if(feof(grading))
{
break;
}
else if(test!=0)
{
fprintf(temp,"%s %f %f %f %f\n",name,eng,math,sci,ave);
}
else
{
printf("%s has been deleted\n",name);
ctr++;
}
}
while(!feof(grading));
if(ctr==0)
{
printf("-------------------------------\n");
printf("The name doesn't exist!!!\n");
}
fclose(temp);
fclose(grading);
remove("record.txt");
rename("tempreco.txt","record.txt");
printf("-------------------------------");
fflush(stdin);
repeat4:
printf("\n\nDo you want to delete another record?\n\n");
printf("[Y] for Yes or [N] to go back to main menu: ");
scanf("%c",&choice);
if((choice=='Y')||(choice=='y'))
{
goto mainmenu;
}
else if((choice=='N')||(choice=='n'))
{
goto mainmenu;
}
else
{
system("CLS");
printf("\nInvalid choice!!\n");
goto repeat4;
}
}
empty:
{
system("CLS");
printf("-------------------------------\n");
printf("Record Emptied!!");
grading=fopen("record.txt","w+");
fclose(grading);
printf("\n-------------------------------");
system("Pause");
goto mainmenu;
}
}
getch();
}