Advertisement
mehedi1

Untitled

Sep 9th, 2019
281
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.85 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<conio.h>
  3. #include<string.h>
  4. #include<stdlib.h>
  5. #include<math.h>
  6. #define Student struct Stud
  7. void add(FILE * fp); //to add to list
  8. void display(FILE * fp);//display whole list
  9. void searchRecord(FILE *fp);//find a particular record
  10. void command(char ch,int n);//printing a character ch n times
  11. void title();//printing head line for each screen
  12. struct Stud
  13. {
  14.     int class;
  15.     char name[100];
  16.     int id;
  17.     char fname[100];
  18.     char mname[100];
  19.     char mobile[13];
  20.     float sgpa[3];
  21.     float cgpa;
  22. };
  23.  
  24.  
  25. int main()
  26. {
  27.     FILE * fp;
  28.  
  29.     int option;
  30.     char another;
  31.  
  32.     if((fp=fopen("studentInfo.txt","rb++"))==NULL)
  33.     {
  34.         if((fp=fopen("studentInfo.txt","wb++"))==NULL)
  35.         {
  36.             printf("can't open file");
  37.             return 0;
  38.         }
  39.     }
  40.     title();
  41.     printf("\n\t\tpress any key to continue");
  42.     getch();
  43.  
  44.     while(1)
  45.     {
  46.         title();
  47.         printf("\n\t");
  48.         command('-',64);
  49.         printf("\n\n\t\t\t1. ADD Student");
  50.         printf("\n\n\t\t\t2. DISPLAY Student LIST");
  51.         printf("\n\n\t\t\t3. Search Record");
  52.         printf("\n\n\t\t\t0. EXIT");
  53.         printf("\n\n\t\tEnter Your Option :--> ");
  54.         scanf("%d",&option);
  55.         switch(option)
  56.         {
  57.         case 0:
  58.             return 1;
  59.             break;
  60.         case 1:
  61.             add(fp);
  62.             break;
  63.         case 2:
  64.             display(fp);
  65.             break;
  66.         case 3:
  67.             searchRecord(fp);
  68.             break;
  69.         default:
  70.             printf("\n\t\tYou Pressed wrong key");
  71.             printf("\n\t\tProgram terminated");
  72.             getch();
  73.             exit(0);
  74.  
  75.         }
  76.     }
  77.     return 1;
  78.  
  79. }
  80. //----printing character ch at n times ------
  81.  
  82. void command(char ch,int n)
  83. {
  84.     while(n--)
  85.     {
  86.         putchar(ch);
  87.     }
  88. }
  89.  
  90. //-----Printing Head Line of the program -----
  91.  
  92. void title()
  93. {
  94.     system("cls");
  95.  
  96.     printf("\n\n\t");
  97.     command('*',16);
  98.     printf("[STUDENT] [INFORMATION] [SYSTEM]");
  99.     command('*',16);
  100.     printf("\n");
  101. }
  102.  
  103.  
  104. // ==========ADDING NEW RECORD==========================
  105.  
  106. void add(FILE * fp)
  107. {
  108.     title();
  109.  
  110.     char another='y';
  111.     Student s;
  112.     int i;
  113.     float cgpa;
  114.  
  115.     fseek(fp,0,SEEK_END);
  116.     while(another=='y'||another=='Y')
  117.     {
  118.         printf("\n\n\t\tEnter class number \t");
  119.         scanf("%d",&s.class);
  120.  
  121.         printf("\n\n\t\tEnter id number \t");
  122.         scanf("%d",&s.id);
  123.  
  124.         printf("\n\n\t\tEnter Full Name of Student\t");
  125.         fflush(stdin);
  126.         fgets(s.name,100,stdin);
  127.         s.name[strlen(s.name)-1]='\0';
  128.  
  129.         printf("\n\n\t\tEnter Full Name of Student's Fathers's name\t");
  130.         fflush(stdin);
  131.         fgets(s.fname,100,stdin);
  132.         s.fname[strlen(s.fname)-1]='\0';
  133.  
  134.         printf("\n\n\t\tEnter Full Name of Student's Mother's name\t");
  135.         fflush(stdin);
  136.         fgets(s.mname,100,stdin);
  137.         s.mname[strlen(s.mname)-1]='\0';
  138.  
  139.         printf("\n\n\t\tEnter Student's Mobile number\t");
  140.         fflush(stdin);
  141.         fgets(s.mobile,13,stdin); //fgets takes an extra \n character as input
  142.         s.mobile[strlen(s.mobile)-1]='\0';
  143.  
  144.         printf("\n\n\tEnter SGPA for 3 semesters\n\t");
  145.         for(i=0,cgpa=0; i<3; i++)
  146.         {
  147.             scanf("%f",&s.sgpa[i]);
  148.             cgpa+=s.sgpa[i];
  149.  
  150.         }
  151.  
  152.         cgpa/=3.0;
  153.         s.cgpa=cgpa;
  154.  
  155.         fwrite(&s,sizeof(s),1,fp);
  156.  
  157.         printf("\n\n\t\tWant to enter another student info (Y/N)\t");
  158.         fflush(stdin);
  159.         another=getchar();
  160.     }
  161. }
  162.  
  163. //====================DISPLAY THE LIST =================
  164. void display(FILE * fp)
  165. {
  166.     title();
  167.     Student s;
  168.     int i,siz=sizeof(s);
  169.  
  170.     rewind(fp);
  171.  
  172.     while((fread(&s,siz,1,fp))==1)
  173.     {
  174.         printf("\n\n\t\tCLASS : %d",s.class);
  175.         printf("\n\n\t\tID : %d",s.id);
  176.         printf("\n\t\tNAME : %s",s.name);
  177.         printf("\n\n\t\tFather's Name : %s",s.fname);
  178.         printf("\n\n\t\tMOther's Name : %s",s.mname);
  179.         printf("\n\n\t\tMOBILE : %s",s.mobile);
  180.         printf("\n\n\tSGPA: ");
  181.  
  182.         for(i=0; i<3; i++)
  183.             printf("| %.2f |",s.sgpa[i]);
  184.         printf("\n\n\t\tCGPA : %.2f\n\t",s.cgpa);
  185.         command('-',65);
  186.     }
  187.     printf("\n\n\n\t");
  188.     command('*',65);
  189.     printf("\n\n\t");
  190.     system("pause");
  191. }
  192.  
  193. void searchRecord(FILE *fp)
  194. {
  195.     title();
  196.  
  197.     int tempId,flag,siz,i,tempclass;
  198.     Student s;
  199.     char another='y';
  200.  
  201.     siz=sizeof(s);
  202.  
  203.     while(another=='y'||another=='Y')
  204.     {
  205.         printf("\n\n\tEnter Class Number of a Student to search the record : ");
  206.         scanf("%d",&tempclass);
  207.  
  208.         rewind(fp);
  209.  
  210.         while((fread(&s,siz,1,fp))==1)
  211.         {
  212.             if(s.class==tempclass)
  213.             {
  214.                 printf("\n\n\tEnter ID Number of a Student to search the record : ");
  215.                 scanf("%d",&tempId);
  216.  
  217.                 rewind(fp);
  218.                 while((fread(&s,siz,1,fp))==1)
  219.                 {
  220.                    if(s.id==tempId)
  221.                       {
  222.                           flag=1;
  223.                           break;
  224.                       }
  225.                 }
  226.                 break;
  227.             }
  228.         }
  229.  
  230.         if(flag==1)
  231.         {
  232.             printf("\n\t\tNAME : %s",s.name);
  233.             printf("\n\n\t\tFather's name : %s",s.fname);
  234.             printf("\n\n\t\tMother's name : %s",s.mname);
  235.             printf("\n\n\t\tMobile : %s",s.mobile);
  236.             printf("\n\n\tSGPA: ");
  237.  
  238.             for(i=0; i<3; i++)
  239.                 printf("| %.2f |",s.sgpa[i]);
  240.             printf("\n\n\t\tCGPA : %.2f\n\t",s.cgpa);
  241.             command('-',65);
  242.  
  243.         }
  244.         else
  245.             printf("\n\n\t\t!!!! ERROR RECORD NOT FOUND !!!!");
  246.  
  247.  
  248.         printf("\n\n\t\tWant to enter another search (Y/N)");
  249.         fflush(stdin);
  250.         another=getchar();
  251.     }
  252. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement