Advertisement
Guest User

markprogram

a guest
Nov 21st, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 7.15 KB | None | 0 0
  1. /***********************************************************************
  2.  * moduleMarks.c
  3.  * Program to handle assessment marks.
  4.  * Gerda Simkute
  5.  * November 2017
  6. ***********************************************************************/
  7.  
  8. #include <stdio.h>
  9. #include <string.h>
  10.  
  11. #define MAX_NAME_LENGTH 20
  12.  
  13. void printMenu();
  14.  
  15. void emptybuffer();
  16.  
  17. void displayTitle();
  18.  
  19. int numberStudents();
  20.  
  21. void promptNames(char pString[][MAX_NAME_LENGTH], int num);
  22.  
  23. void
  24. promptMarksFor1(char names[][20], int marks[][3], int numOfstudents);
  25.  
  26. void
  27. promptMarksFor2(char names[][20], int marks[][3], int numOfstudents);
  28.  
  29. void
  30. promptMarksFor3(char names[][20], int marks[][3], int numOfstudents);
  31.  
  32. int findByName(char names[][20], int marks[][3], int length);
  33.  
  34.  
  35. int main() {
  36.     displayTitle();
  37.     int numberOfStudents = numberStudents();
  38.     char names[numberOfStudents][MAX_NAME_LENGTH];
  39.     promptNames(names, numberOfStudents);
  40.     int marks[numberOfStudents][3];
  41. //    promptMarksFor1(names, marks, numberOfStudents);
  42. //    promptMarksFor2(names, marks, numberOfStudents);
  43. //    promptMarksFor3(names, marks, numberOfStudents);
  44.     while(startMenu(names, marks, numberOfStudents)){;}
  45.     findByName(names, marks, numberOfStudents);
  46. }
  47.  
  48.  
  49. /* display title function */
  50. void displayTitle() {
  51.     printf
  52.             ("===================================================="
  53.                      "=====\n");
  54.     printf("              ASSESSMENT MARKS PROGRAM\n");
  55.     printf
  56.             ("===================================================="
  57.                      "=====\n");
  58. }
  59.  
  60. /* number of students function */
  61. int numberStudents() {
  62.     int numStudents;
  63.     puts("Please enter the number of students taking the module: (Max"
  64.                  " 75)");
  65.     while (scanf("%d", &numStudents) != 1 || numStudents < 1 ||
  66.            numStudents > 75)
  67.     {
  68.         puts(" ERROR! the number must be in the range of 1 and 75. "
  69.                      "Please re-enter the value");
  70.         emptybuffer();
  71.     }
  72.     printf("You have chosen %d students\n\n", numStudents);
  73.     getchar();
  74.     return numStudents;
  75. }
  76.  
  77. /* student names function */
  78. void promptNames(char names[][MAX_NAME_LENGTH], int num) {
  79.     int i;
  80.     printf("Please enter the names of %d students:\n", num);
  81.  
  82.     for (i = 0; i < num; i++) {
  83.         printf("Student Name:\n");
  84.         scanf("%[^\n]", names[i]);
  85.         emptybuffer();
  86.     }
  87. }
  88.  
  89. /* display the menu list */
  90.  void printMenu() {
  91.     printf
  92.             ("==================================================="
  93.                      "======\n");
  94.     printf("                       MAIN MENU\n");
  95.     printf
  96.             ("==================================================="
  97.                      "======\n");
  98.  
  99.     printf("1. Enter marks for coursework 1\n");
  100.     printf("2. Enter marks for coursework 2\n");
  101.     printf("3. Enter marks for coursework 3\n");
  102.     printf("4. Display a particular students mark\n");
  103.     printf("5. Supervisor mode\n");
  104.     printf("6. Exit program\n\n");
  105.     printf("---------------------------------------------------\n");
  106. }
  107.  
  108. /* select from menu */
  109. int startMenu (char names [][MAX_NAME_LENGTH], int marks [][3], int
  110. number)
  111. {
  112.     int entry;
  113.     printMenu();
  114.     puts("Please enter option number:");
  115.     while (scanf("%d", &entry) != 1)
  116.     {
  117.         puts("Error, please enter 1-6");
  118.         emptybuffer();
  119.     }
  120.     emptybuffer();
  121.  
  122.     switch(entry)
  123.     {
  124.         case 1 : promptMarksFor1(names, marks, number);
  125.             break;
  126.  
  127.         case 2 : promptMarksFor2(names, marks, number);
  128.             break;
  129.  
  130.         case 3 : promptMarksFor3(names, marks, number);
  131.             break;
  132.  
  133.         case 4:
  134.             findByName(names, marks, number);
  135.             break;
  136.  
  137.  
  138.         case 6:
  139.             return 0;
  140.  
  141.         default:
  142.             puts("Incorrect number. Enter 1-6");
  143.  
  144.  
  145.     }
  146.     return 1;
  147. }
  148.  
  149.  
  150. /* enter the marks for coursework 1 */
  151. void
  152. promptMarksFor1(char names[][20], int marks[][3], int numOfstudents) {
  153.     char charChoice;
  154.     do {
  155.         int i;
  156.         puts("-------------------------------");
  157.         puts("Enter marks for coursework 1:\n");
  158.         for (i = 0; i < numOfstudents; i++) {
  159.             char *currentName = names[i];
  160.             printf("Enter marks for %s:\n", currentName);
  161.  
  162.             while (scanf("%d", &marks[i][0]) != 1) {
  163.                 puts("Try again. Enter a number");
  164.                 emptybuffer();
  165.             }
  166.             emptybuffer();
  167.         }
  168.  
  169.  
  170.         puts("Are you okay with these marks? Please enter Y or N:");
  171.         scanf("%c", &charChoice);
  172.         emptybuffer();
  173.     } while (charChoice != 'y' && charChoice != 'Y');
  174. }
  175.  
  176.  
  177. /* Enter marks for coursework 2 */
  178. void
  179. promptMarksFor2(char names[][20], int marks[][3], int numOfstudents) {
  180.     char charChoice;
  181.     do {
  182.         int i;
  183.         puts("-------------------------------");
  184.         puts("Enter marks for coursework 2:\n");
  185.         for (i = 0; i < numOfstudents; i++) {
  186.             char *currentName = names[i];
  187.             printf("Enter marks for %s:\n", currentName);
  188.  
  189.             while (scanf("%d", &marks[i][1]) != 1) {
  190.                 puts("Try again. Enter a number");
  191.                 emptybuffer();
  192.             }
  193.             emptybuffer();
  194.         }
  195.  
  196.  
  197.         puts("Are you okay with these marks? Please enter Y or N:");
  198.         scanf("%c", &charChoice);
  199.         emptybuffer();
  200.     } while (charChoice != 'y' && charChoice != 'Y');
  201. }
  202.  
  203. /* Enter marks for coursework 3 */
  204. void
  205. promptMarksFor3(char names[][20], int marks[][3], int numOfstudents) {
  206.     char charChoice;
  207.     do {
  208.         int i;
  209.         puts("-------------------------------");
  210.         puts("Enter marks for coursework 3:\n");
  211.         for (i = 0; i < numOfstudents; i++) {
  212.             char *currentName = names[i];
  213.             printf("Enter marks for %s:\n", currentName);
  214.  
  215.             while (scanf("%d", &marks[i][2]) != 1) {
  216.                 puts("Try again. Enter a number");
  217.                 emptybuffer();
  218.             }
  219.             emptybuffer();
  220.         }
  221.  
  222.  
  223.         puts("Are you okay with these marks? Please enter Y or N:");
  224.         scanf("%c", &charChoice);
  225.         emptybuffer();
  226.     } while (charChoice != 'y' && charChoice != 'Y');
  227. }
  228.  
  229. /* selecting a student and displaying their mark */
  230. int findByName(char names[][20], int marks [][3], int studentNum)
  231.  
  232. {
  233.     int t;
  234.     for(t=0;t<studentNum;t++)printf("We have a name: %s \n\n",names[t]);
  235.  
  236.     char selectedStudent[20];
  237.     int i;
  238.     puts("Enter the name of the student whose mark you wish to see:");
  239.     scanf("%[^\n]", selectedStudent);
  240.     emptybuffer();
  241.  
  242.     for( i = 0; i < studentNum; i ++)
  243.     {
  244.         int matching = !strcmp(selectedStudent, names[i]);
  245.         if (matching) break;
  246.     }
  247.     printf("Index is %d\n",i);
  248.     if(i==studentNum)
  249.     {
  250.         printf("Error, no name found! \n");
  251.     }
  252.  
  253.     printf("Here are the marks from %s's coursework:\n", names[i]);
  254.     printf("%d \n", marks[i][0]);
  255.     printf("%d \n", marks[i][1]);
  256.     printf("%d \n\n", marks[i][2]);
  257.  
  258.     return 0;
  259.  
  260. }
  261.  
  262.  
  263.  
  264.  
  265.  
  266. /* empty buffer function */
  267. void emptybuffer() {
  268.     while (getchar() != '\n') { ;
  269.     }
  270. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement