Share Pastebin
Guest
Public paste!

Untitled

By: a guest | Mar 21st, 2010 | Syntax: C | Size: 7.58 KB | Hits: 68 | Expires: Never
Copy text to clipboard
  1. // Kamberly Training Courses V 0.1
  2.  
  3. #include <stdio.h>
  4. #include <string.h>
  5.  
  6. typedef struct
  7. {
  8.         int number;
  9.         char cName[20];
  10.         char courseCode[6];
  11. } Course;
  12.  
  13. /* After Execution - Course details are fetched from file and the last index
  14.                                          that was used it loaded (if the file exsist). If the file
  15.                                          does not exist return -1 */
  16. int populateCourse(char filename[], Course courses[]);
  17.  
  18. /* After Execution - Menu option is obtained from the user */
  19. int mainMenu();
  20.  
  21. /* Before Execution - lastIndex is the total number of courses */
  22. void printCourses(Course courses[], int lastIndex);
  23.  
  24. /*  */
  25. int findCourse(Course courses[], int lastIndex, int cName);
  26.  
  27. /*  */
  28. void getNumber(int *number);
  29.  
  30. /* */
  31. void getString(char s[], int size);
  32.  
  33. /* */
  34. void removeNewline(char s[]);
  35.  
  36. /* */
  37. void saveAll(char fileName[], Course courses[], int lastIndex);
  38.  
  39.  
  40. int main()
  41. {
  42.         int numOfCourses = 5;
  43.         Course courses[numOfCourses]; //A structure to hold 5 courses' details
  44.         int lastIndex = -1; // Used to store the last index of the courses structure
  45.                             // that was used
  46.        
  47.         char fileName[] = "course.dat"; // Filename to be used to save and load data
  48.        
  49.         lastIndex = populateCourses(fileName, courses);
  50.        
  51.         int option; /* used to store the menu option
  52.                                    and returns until the user enters the
  53.                                    exit option */
  54.                                    
  55.         do
  56.         {
  57.                 option = mainMenu();
  58.                 /* Used if the user enters the option for adding a new course */
  59.                 if (option == 1)
  60.                 {
  61.                         system("cls");
  62.                         printf("Add new course\n");
  63.                         /* Check that the structure is full */
  64.                         if (lastIndex == numOfCourses -1)
  65.                         {
  66.                                 printf("Unable to add any more students\n");
  67.                                 system("pause");
  68.                         }
  69.                         /* If array is not full */
  70.                         else
  71.                         {
  72.                                 lastIndex++; /* Adds 1 to the contents of lastIndex */
  73.                                 getNumber(&courses[lastIndex].number);
  74.                                
  75.                                 printf("Enter course name: ");
  76.                                 getString(courses[lastIndex].cName,
  77.                                         sizeof(courses[lastIndex].cName));
  78.                                
  79.                                 printf("Enter course code: ");
  80.                                 getString(courses[lastIndex].courseCode,
  81.                                         sizeof(courses[lastIndex].courseCode));
  82.                                
  83.                         }
  84.                 }
  85.                
  86.                 /* Used if the user enters the option for displaying courses' details */
  87.                 else if (option == 3)
  88.                 {
  89.                         /*prompts the user to enter a course code */
  90.                         printCourses(courses, lastIndex);
  91.                         printf("Enter course code to change the cost: ");
  92.                         int cCode; // Used to store the course code
  93.                         scanf("%i", &cCode);
  94.                         _flushall();
  95.                        
  96.                         /* Checks Course Code exsists */
  97.                         int found = findCourse(courses, lastIndex, cCode);
  98.                         /* Performed if the course code doesn't exsist */
  99.                         if (found == -1)
  100.                         {
  101.                                 printf("Course was not found\n");
  102.                                 system("pause");
  103.                         }
  104.                        
  105.                         /* Performed if the course was found */
  106.                         else
  107.                         {
  108.                                 printf("Course found\n");
  109.                                
  110.                                 /* Display option choices for update */
  111.                                 printf("1 - Course Name\n 2- Course Code\n");
  112.                                 int updateOption; /* Used to store the option
  113.                                                                          the user selected */
  114.                                 scanf("%i", &updateOption);
  115.                                 _flushall();
  116.                                 /* Performed if the user wants to update the name of the
  117.                                 course */
  118.                                 if (updateOption == 1)
  119.                                 {
  120.                                         /* Get new Course name */
  121.                                         printf("Enter new course name: ");
  122.                                         getString(courses[found].cName,
  123.                                                 sizeof(courses[found].cName));
  124.                                 }
  125.                                
  126.                                 /* Performed if the user wants to modify the course code
  127.                                 for the course */
  128.                                 else if (updateOption == 2)
  129.                                 {
  130.                                         /* Get new course code */
  131.                                         printf("Enter new course code: ");
  132.                                         getString(courses[found].courseCode,
  133.                                                 sizeof (courses[found].courseCode));
  134.                                 }
  135.                         }
  136.                 }
  137.                
  138.                 /* Perfomed if the user enters the option for exiting the program */
  139.                 else if (option == 4)
  140.                 {
  141.                         saveAll(fileName, courses, lastIndex);
  142.                 }
  143.         } while (option != 4);
  144.        
  145.         system("pause");
  146.         return 0;
  147. }
  148.  
  149. int mainMenu()
  150. {
  151.         int option;
  152.         system("cls");
  153.         printf("Kambury IT Training\n\n");
  154.         printf("1: Add new course\n");
  155.         printf("2: Display courses' details\n");
  156.         printf("3: Edit course code\n");
  157.         printf("4: Exit\n");
  158.         printf("Enter options; ");
  159.         scanf("%i", &option);
  160.         _flushall();
  161.         return option;
  162. }
  163.  
  164. int populateCourses(char fileName[], Course courses[])
  165. {
  166.    FILE *courseFile;
  167.    // try to open the file in read binary mode
  168.    courseFile = fopen(fileName, "rb"); // open the file in read binary file
  169.    
  170.    int lastIndex = -1;      
  171.    // checks if the file exists or not.
  172.    // if the file exists, retrieve the information from the file      
  173.    if (courseFile != NULL)
  174.    {  
  175.       int i = -1;
  176.       // obtain the information relating to the number of students' details
  177.       // that have been saved
  178.       fread(&lastIndex, sizeof(int), 1, courseFile);
  179.       // retrieve each student's details
  180.       while (i < lastIndex)
  181.       {  
  182.          i = i + 1;
  183.          fread(&courses[i], sizeof(Course), 1, courseFile);
  184.       }
  185.       fclose(courseFile);  
  186.    }
  187.    return lastIndex;
  188. }
  189.  
  190. // displays all the courses' details on the screen
  191. void printCourses(Course courses[], int lastIndex)
  192. {
  193.    int i;
  194.    system("cls");
  195.    printf("Courses' details\n\n");
  196.    // performed if the course structure contains at least one record
  197.    if (lastIndex >= 0)
  198.    {            
  199.       printf("ID\tName\t\tCourse Code\n");
  200.       // repeats until all of the courses' details have been displayed
  201.       for (i = 0; i <= lastIndex; i++)
  202.       {
  203.          printf("%i\t%s\t\t%s\n", courses[i].number, courses[i].cName,
  204.                            courses[i].courseCode);
  205.       }
  206.    }
  207. }
  208.  
  209. // searches through the structure until a match for the course code is found, or
  210. // until the end of the structure has been reached
  211. // returns the index where the course code was found, otherwise returns -1
  212. int findCourse(Course courses[], int lastIndex, int cCode)
  213. {  int i;
  214.    // repeats while the contents of i is less than or equal to the contents of
  215.    // lastIndex
  216.    for (i = 0; i <= lastIndex; i++)
  217.    {        
  218.       // checks if the contents of cCode is the same as the student's id.  A match
  219.       // has been made
  220.       if (cCode == courses[i].number)
  221.       {
  222.          return i; // returns the position where the student id as found
  223.       }
  224.    }
  225.    system("pause");
  226.    return -1; // returns a non-index value, the id was not found in the structure
  227. }
  228.  
  229. void saveAll(char fileName[], Course courses[], int lastIndex)
  230. {
  231.    // checks that the structure has at least one record to save
  232.    if (lastIndex >= 0)
  233.    {
  234.       FILE *courseFile = fopen(fileName, "wb"); // open the file in write
  235.                                                  // binary mode
  236.       int i;
  237.    
  238.       // save the contents of lastIndex so the program will know how many
  239.       // records need to be read back from the file
  240.       fwrite(&lastIndex, sizeof(int), 1, courseFile);
  241.       // repeat until all the students' details have been saved to file
  242.       for (i = 0; i <= lastIndex; i++)
  243.       {    
  244.          fwrite(&courses[i], sizeof(Course), 1, courseFile);
  245.       }
  246.       fclose(courseFile); // close the file
  247.    }
  248. }
  249.  
  250. void getNumber(int *number)
  251. {
  252.      printf("Enter student number ");
  253.      scanf("%i", number);
  254.      _flushall();
  255. }
  256.  
  257. void getString(char s[], int size)
  258. {
  259.      fgets(s, size, stdin);
  260.      removeNewline(s);
  261. }
  262.  
  263. // function to remove \n for the end of the string, if \n exists
  264. void removeNewline(char s[])
  265. {
  266.     // find the length of the string (this will include \n if
  267.     // it exists) and then subtract 1 from the length
  268.     int i = strlen(s) - 1;
  269.     // check if the character at position 1 is \n
  270.     if (s[i] == '\n')
  271.     {              
  272.         // replace \n with \0          
  273.         s[i] = '\0';
  274.     }
  275. }