Advertisement
mehedi1

Project - SP

Sep 9th, 2019
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.29 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. // ==========ADDING NEW RECORD==========================
  104.  
  105. void add(FILE * fp)
  106. {
  107.     title();
  108.  
  109.     char another='y';
  110.     Student s;
  111.     int i;
  112.     float cgpa;
  113.  
  114.     while(another=='y'||another=='Y')
  115.     {
  116.         fseek(fp,0,SEEK_END);
  117.         printf("\n\n\t\tEnter Class number \t");
  118.         scanf("%d",&s.Class);
  119.         if(s.Class==1) {
  120.             if((fp=fopen("studentInfo1.txt","rb++"))==NULL)
  121.             {
  122.                 if((fp=fopen("studentInfo1.txt","wb++"))==NULL)
  123.                 {
  124.                     printf("can't open file");
  125.                     return;
  126.                 }
  127.             }
  128.         }
  129.         else if(s.Class==2) {
  130.             if((fp=fopen("studentInfo2.txt","rb++"))==NULL)
  131.             {
  132.                 if((fp=fopen("studentInfo2.txt","wb++"))==NULL)
  133.                 {
  134.                     printf("can't open file");
  135.                     return;
  136.                 }
  137.             }
  138.         }
  139.         else if(s.Class==3) {
  140.             if((fp=fopen("studentInfo3.txt","rb++"))==NULL)
  141.             {
  142.                 if((fp=fopen("studentInfo3.txt","wb++"))==NULL)
  143.                 {
  144.                     printf("can't open file");
  145.                     return;
  146.                 }
  147.             }
  148.         }
  149.  
  150.         printf("\n\n\t\tEnter id number \t");
  151.         scanf("%d",&s.id);
  152.  
  153.         printf("\n\n\t\tEnter Full Name of Student\t");
  154.         fflush(stdin);
  155.         fgets(s.name,100,stdin);
  156.         s.name[strlen(s.name)-1]='\0';
  157.  
  158.         printf("\n\n\t\tEnter Full Name of Student's Fathers's name\t");
  159.         fflush(stdin);
  160.         fgets(s.fname,100,stdin);
  161.         s.fname[strlen(s.fname)-1]='\0';
  162.  
  163.         printf("\n\n\t\tEnter Full Name of Student's Mother's name\t");
  164.         fflush(stdin);
  165.         fgets(s.mname,100,stdin);
  166.         s.mname[strlen(s.mname)-1]='\0';
  167.  
  168.         printf("\n\n\t\tEnter Student's Mobile number\t");
  169.         fflush(stdin);
  170.         fgets(s.mobile,13,stdin); //fgets takes an extra \n character as input
  171.         s.mobile[strlen(s.mobile)-1]='\0';
  172.  
  173.         printf("\n\n\tEnter SGPA for 3 semesters\n\t");
  174.         for(i=0,cgpa=0; i<3; i++)
  175.         {
  176.             scanf("%f",&s.sgpa[i]);
  177.             cgpa+=s.sgpa[i];
  178.  
  179.         }
  180.  
  181.         cgpa/=3.0;
  182.         s.cgpa=cgpa;
  183.  
  184.         fwrite(&s,sizeof(s),1,fp);
  185.  
  186.         printf("\n\n\t\tWant to enter another student info (Y/N)\t");
  187.         fflush(stdin);
  188.         another=getchar();
  189.     }
  190. }
  191.  
  192. //====================DISPLAY THE LIST =================
  193.  
  194. void display(FILE * fp) {
  195.     title();
  196.  
  197.     Student s;
  198.     int i,siz=sizeof(s);
  199.  
  200.  
  201.     int cls;
  202.     printf("Enter a Class From 1 to 10: ");
  203.     scanf("%d",&cls);
  204.  
  205.     if(cls==1) {
  206.         if((fp=fopen("studentInfo1.txt","rb++"))==NULL)
  207.         {
  208.             if((fp=fopen("studentInfo1.txt","wb++"))==NULL)
  209.             {
  210.                 printf("can't open file");
  211.                 return;
  212.             }
  213.         }
  214.     }
  215.     else if(cls==2) {
  216.         if((fp=fopen("studentInfo2.txt","rb++"))==NULL)
  217.         {
  218.             if((fp=fopen("studentInfo2.txt","wb++"))==NULL)
  219.             {
  220.                 printf("can't open file");
  221.                 return;
  222.             }
  223.         }
  224.     }
  225.     else if(cls==3) {
  226.         if((fp=fopen("studentInfo3.txt","rb++"))==NULL)
  227.         {
  228.             if((fp=fopen("studentInfo3.txt","wb++"))==NULL)
  229.             {
  230.                 printf("can't open file");
  231.                 return;
  232.             }
  233.         }
  234.     }
  235.    
  236.     rewind(fp);
  237.  
  238.     while((fread(&s,siz,1,fp))==1)
  239.     {
  240.         printf("\n\n\t\tCLASS : %d",s.Class);
  241.         printf("\n\n\t\tID : %d",s.id);
  242.         printf("\n\t\tNAME : %s",s.name);
  243.         printf("\n\n\t\tFather's Name : %s",s.fname);
  244.         printf("\n\n\t\tMOther's Name : %s",s.mname);
  245.         printf("\n\n\t\tMOBILE : %s",s.mobile);
  246.         printf("\n\n\tSGPA: ");
  247.  
  248.         for(i=0; i<3; i++)
  249.             printf("| %.2f |",s.sgpa[i]);
  250.         printf("\n\n\t\tCGPA : %.2f\n\t",s.cgpa);
  251.         command('-',65);
  252.     }
  253.     printf("\n\n\n\t");
  254.     command('*',65);
  255.     printf("\n\n\t");
  256.     system("pause");
  257. }
  258.  
  259. //=================Search Record================
  260.  
  261. void searchRecord(FILE *fp)
  262. {
  263.     title();
  264.  
  265.     int tempId,flag,siz,i,tempClass;
  266.     Student s;
  267.     char another='y';
  268.  
  269.     siz=sizeof(s);
  270.  
  271.     while(another=='y'||another=='Y')
  272.     {
  273.         printf("\n\n\tEnter Class Number of a Student to search the record : ");
  274.         scanf("%d",&tempClass);
  275.  
  276.         if(tempClass==1) {
  277.             if((fp=fopen("studentInfo1.txt","rb++"))==NULL)
  278.             {
  279.                 if((fp=fopen("studentInfo1.txt","wb++"))==NULL)
  280.                 {
  281.                     printf("can't open file");
  282.                     return;
  283.                 }
  284.             }
  285.         }
  286.         else if(tempClass==2) {
  287.             if((fp=fopen("studentInfo2.txt","rb++"))==NULL)
  288.             {
  289.                 if((fp=fopen("studentInfo2.txt","wb++"))==NULL)
  290.                 {
  291.                     printf("can't open file");
  292.                     return;
  293.                 }
  294.             }
  295.         }
  296.         else if(tempClass==3) {
  297.             if((fp=fopen("studentInfo3.txt","rb++"))==NULL)
  298.             {
  299.                 if((fp=fopen("studentInfo3.txt","wb++"))==NULL)
  300.                 {
  301.                     printf("can't open file");
  302.                     return;
  303.                 }
  304.             }
  305.         }
  306.  
  307.         rewind(fp);
  308.  
  309.         while((fread(&s,siz,1,fp))==1)
  310.         {
  311.             if(s.Class==tempClass)
  312.             {
  313.                 printf("\n\n\tEnter ID Number of a Student to search the record : ");
  314.                 scanf("%d",&tempId);
  315.  
  316.                 rewind(fp);
  317.                 while((fread(&s,siz,1,fp))==1)
  318.                 {
  319.                    if(s.id==tempId)
  320.                       {
  321.                           flag=1;
  322.                           break;
  323.                       }
  324.                 }
  325.                 break;
  326.             }
  327.         }
  328.  
  329.         if(flag==1)
  330.         {
  331.             printf("\n\t\tNAME : %s",s.name);
  332.             printf("\n\n\t\tFather's name : %s",s.fname);
  333.             printf("\n\n\t\tMother's name : %s",s.mname);
  334.             printf("\n\n\t\tMobile : %s",s.mobile);
  335.             printf("\n\n\tSGPA: ");
  336.  
  337.             for(i=0; i<3; i++)
  338.                 printf("| %.2f |",s.sgpa[i]);
  339.             printf("\n\n\t\tCGPA : %.2f\n\t",s.cgpa);
  340.             command('-',65);
  341.  
  342.         }
  343.         else
  344.             printf("\n\n\t\t!!!! ERROR RECORD NOT FOUND !!!!");
  345.  
  346.  
  347.         printf("\n\n\t\tWant to enter another search (Y/N)");
  348.         fflush(stdin);
  349.         another=getchar();
  350.     }
  351. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement