Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Apr 25th, 2012  |  syntax: None  |  size: 3.98 KB  |  hits: 16  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1.  
  2. #include<stdio.h>
  3. #include<string.h>
  4. #include<conio.h>
  5. #include<stdlib.h>
  6.  
  7. #define MENU 100
  8.  
  9. struct
  10. {
  11.     int Total;
  12.     int Deleted;
  13.     char filler [16];
  14. }header;
  15.  
  16. struct
  17. {
  18.     char Student_number[6];
  19.     char Last_name [16];
  20.     char First_name [16];
  21.     char Address [26];
  22.     char Program_code [6];
  23.     char Program_year [1];
  24.    
  25. }student;
  26.  
  27. void addstudent(void);
  28. void list (void);
  29. void deletestudent (void);
  30.  
  31. main()
  32. {
  33.     //declaring variables
  34.     char menu,i;
  35.  
  36.    
  37.    
  38.     //entering a for loop so that the menu reappears after each function
  39.     for (i=0; i <MENU; i++)
  40.     {
  41.         //clears the screen
  42.         system("cls");
  43.        
  44.         //prints instructions  to screen
  45.         printf("Main Menu\n\n1)Add Student\n2)Delete Student\n3)List Students");
  46.         printf("\n4)Compact Records\n5)Exit program\n\n");
  47.        
  48.         //scans in what function the user wants to use
  49.         scanf("%1s",&menu);
  50.        
  51.         //switch statement to impliment different functions depending on the users choice
  52.         switch (menu)
  53.         {
  54.             //if 1 is chosen the screen will clear and the numbers will be read in, error checked and counted
  55.             case '1':
  56.                 addstudent();
  57.                 break;
  58.             case '2':
  59.                 deletestudent ();
  60.                 break;
  61.             case '3':
  62.                 list();
  63.                 break;
  64.             case '5':
  65.                 exit(EXIT_SUCCESS);
  66.             //if a character not in the switch is chosen a error will print to screen for barely a second and the loop will reset so the user can pick an approiate answer
  67.             default:
  68.                 printf("ERROR");
  69.            
  70.         }//End switch
  71.     }//End for
  72.  
  73.     //keeps program open
  74.     getch();
  75. }//End main
  76.  
  77. void addstudent()
  78. {
  79.     FILE *library;
  80.     char choice = 'y';
  81.     char Student_number_found = 'n';
  82.     char test_Student_number[6];
  83.     library = fopen("student.dat","ab+");
  84.     while (choice == 'y'||choice == 'Y')
  85.     {
  86.         do{
  87.             printf("\nEnter Student Number:");
  88.             scanf("%s",test_Student_number);
  89.             Student_number_found = 'n';
  90.             fseek(library,sizeof(header),SEEK_SET);
  91.             while(fread(&student,sizeof(student),1,library)==1)
  92.             {
  93.                 if(strcmp(student.Student_number,test_Student_number)==0)
  94.                 {
  95.                     Student_number_found = 'Y';
  96.                     fseek(library,0,SEEK_END);
  97.                     printf("\n Student Number already entered please try again....");
  98.                 }
  99.             }
  100.         }
  101.         while(Student_number_found == 'Y');
  102.         {
  103.             strcpy(student.Student_number,test_Student_number);
  104.             printf("\nEnter Last name:");
  105.             scanf("%s",student.Last_name);
  106.             printf("\nEnter First name:");
  107.             scanf("%s",student.First_name);
  108.             printf("\nEnter Address:");
  109.             scanf("%s",student.Address);
  110.             printf("\nEnter Program Code:");
  111.             scanf("%s",student.Program_code);
  112.             printf("\nEnter Program Year:");
  113.             scanf("%s",student.Program_year);
  114.             fwrite(&student,sizeof(student),1,library);
  115.             printf("\nAdd Another record (y/n):");
  116.             scanf("%1s",&choice);
  117.            
  118.         }
  119.         fclose(library);
  120.     }
  121. }
  122.  
  123. void list()
  124. {
  125.     FILE *library;
  126.     library = fopen("student.dat","rb");
  127.    
  128.     printf("\n Student No.\tLast Name\tFirst Name\tAddress\tP Code\tP year\n");
  129.     printf(" ===========================================================================\r");
  130.    
  131.     while(fread(&student,sizeof(student),1,library)==1)
  132.     {
  133.         printf("\n %s \t\t%s \t\t%s \t\t%s\t%s\t%s\n",
  134.         student.Student_number,student.Last_name,student.First_name,
  135.         student.Address,student.Program_code,student.Program_year
  136.         );
  137.     }
  138.     fclose(library);
  139.     getch();
  140. }
  141.  
  142. void deletestudent ()
  143. {
  144.    
  145. }