Advertisement
Guest User

Untitled

a guest
Aug 29th, 2016
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.35 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<math.h>
  4.  
  5. void read_file(void);
  6. void add_student(void);
  7. void search_student_id(void);
  8. void delete_student_info(void);
  9. void edit_student_info(void);
  10. void display_info(void);
  11. void display_all_id(void);
  12. void save(void);
  13.  
  14. struct students {
  15.  
  16.     int student_id;
  17.     float score ;
  18. };
  19.  
  20. struct students info[100];
  21.  
  22.  
  23. int i;
  24.  
  25. void main (){
  26.  
  27.     int choice ;
  28.  
  29.     while (1){
  30.  
  31.  
  32.         printf("\t------MENU------\n\n");
  33.         printf("1.Read the student info\n");
  34.         printf("2.Add a student\n");
  35.         printf("3.Search based on students ID\n");
  36.         printf("4.Delete a students basic info by ID\n");
  37.         printf("5.Edit a students basic info\n");
  38.         printf("6.Display students info\n");
  39.         printf("7.Display all existing students ID\n");
  40.         printf("8.Save \n");
  41.  
  42.         printf("\n\nEnter your choice\n");
  43.         scanf("%d",&choice);
  44.  
  45.         switch (choice ){
  46.  
  47.         case 1 : read_file () ;
  48.             break;
  49.         case 2 :  add_student(); ;
  50.             break;
  51.         case 3 : search_student_id() ;
  52.             break;
  53.         case 4 : //delete_student_info() ;
  54.             break;
  55.         case 5 : edit_student_info() ;
  56.             break;
  57.         case 6 : display_info() ;
  58.             break;
  59.         case 7 : display_all_id() ;
  60.             break;
  61.         case 8 : save() ;
  62.             break;
  63.         case 9 :
  64.             //exit(0);
  65.         default :
  66.             printf(" \n Wrong Input\n");
  67.  
  68.    
  69.         }
  70.     }
  71. }
  72.  
  73.  
  74. void read_file( ){
  75.  
  76.     int NUM=5;
  77.     FILE*fp;
  78.     errno_t err= fopen_s(&fp,"studentinfo.txt","r");
  79.     if ( err !=0 ) {
  80.         printf("\nFile is not opened\n");
  81.     }else{
  82.  
  83.         for( i = 0; i < NUM ; i++){
  84.             fscanf_s(fp,"%d",&info[i].student_id);
  85.             fscanf_s(fp,"%f\n",&info[i].score);
  86.             printf("%d\t",info[i].student_id);
  87.             printf("%.2f\n",info[i].score);
  88.         }
  89.         fclose(fp);
  90.     }
  91.    
  92. }
  93.  
  94. void add_student( ){
  95.  
  96.     int j=i ;
  97.  
  98.  
  99.    
  100.             printf("\nEnter student id\n");
  101.             scanf("%d",&info[j].student_id);
  102.             printf("\nEnter students score\n");
  103.             scanf("%f",&info[j].score);
  104.             j++;
  105.         i=j;   
  106.    
  107. }
  108.  
  109. void display_all_id( ) {
  110.  
  111.     int l ;
  112.  
  113.     for(l=0;l<i;l++){
  114.  
  115.         printf("%d\t",info[l].student_id);
  116.             printf("%.2f\n",info[l].score);
  117.     }
  118.  
  119.    
  120.  
  121. }
  122.  
  123.  
  124. void display_info() {
  125.  
  126.     int l,id , p ;
  127.  
  128.     printf("\n Enter ID : ");
  129.     scanf("%d", &id);
  130.    
  131.     for ( l= 0 ; l< i ; l++ ){
  132.  
  133.         if ( id == info[l].student_id){
  134.             p=l;
  135.             break;
  136.         }
  137.     }
  138.  
  139.         printf("ID: %d score: %.2f", info[p].student_id , info[p].score);
  140.  
  141.  
  142. }
  143.  
  144. void search_student_id( ) {
  145.  
  146.     int l,id , p = 0;
  147.  
  148.     printf("\n Enter ID : ");
  149.     scanf("%d", &id);
  150.    
  151.     for ( l= 0 ; l< i ; l++ ){
  152.  
  153.         if ( id == info[l].student_id){
  154.             p=l;
  155.             printf("ID: %d score: %.2f", info[p].student_id , info[p].score);
  156.  
  157.             break;
  158.         }else if ( id != info[l].student_id){
  159.             printf("\nNot Found!!\n\n");
  160.             break;
  161.         }
  162.     }
  163.  
  164.        
  165. }
  166.  
  167. void save( ) {
  168.  
  169.     int e ;
  170.     FILE*fp;
  171.     errno_t err= fopen_s(&fp,"studentinfo.txt","w");
  172.     if ( err !=0 ) {
  173.         printf("\nFile is not opened\n");
  174.     }else{
  175.  
  176.         for( e = 0; e <=i ; e++){
  177.             fprintf(fp,"%d ",info[e].student_id);
  178.             fprintf(fp,"%.2f\n",info[e].score);
  179.         }
  180.         fclose(fp);
  181.     }
  182. }
  183.  
  184. void edit_student_info( ) {
  185.  
  186.     int p=0, l , id;
  187.     printf("\n Enter ID : ");
  188.     scanf("%d", &id);
  189.    
  190.     for ( l= 0 ; l< i ; l++ ){
  191.  
  192.         if ( id == info[l].student_id){
  193.             p=l;
  194.             printf("\nedit id : ");
  195.             scanf("%d" , &info[p].student_id);
  196.             printf("\nedit score : ");
  197.             scanf("%f" , &info[p].score);
  198.  
  199.             break;
  200.         }else if ( id != info[l].student_id){
  201.             printf("\nNot Found!!\n\n");
  202.             break;
  203.         }
  204.     }
  205. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement