Advertisement
Guest User

Untitled

a guest
Dec 14th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.78 KB | None | 0 0
  1. #define NAME_SIZE 40
  2. #define MAX_ASSIGNMENT_SIZE 4096
  3. #define FILENAME_SIZE 44
  4. #include <stdio.h>
  5. #include <string.h>
  6. #include <stdlib.h>
  7. #include <ctype.h>
  8. #include <stdbool.h>
  9.  
  10.  
  11. typedef struct {
  12. char name[NAME_SIZE];
  13. int zNumber;
  14. char assignment[MAX_ASSIGNMENT_SIZE];
  15. double score;
  16. } Student;
  17.  
  18. /******
  19. Following functions will be implemented:
  20. For the Student:
  21. submitAssignment();
  22. getScore();
  23. studentPrint();
  24. For the Teacher:
  25. getAssignment();
  26. assignScore();
  27. ********/
  28.  
  29. void welcomeScreen(Student* pStudent);
  30. void submitAssignment(Student* pStudent);
  31. void print(Student* pStudent);
  32. void intialize(Student* pStudent, char name[], int zNumber);
  33. double getScore(Student* pStudent);
  34. void getAssignment(Student* pStudent);
  35. void assignScore(Student* pStudent, double score);
  36.  
  37.  
  38. void submitAssignment(Student* pStudent) {
  39. char studentFilename[FILENAME_SIZE];
  40.  
  41. char assignment[MAX_ASSIGNMENT_SIZE] = "";
  42. printf("What is your assignment name?\n");
  43. scanf(" %4096s", assignment);
  44. strcpy(pStudent->assignment, assignment);
  45.  
  46.  
  47. FILE *outfile;
  48.  
  49. // open file for writing
  50. outfile = fopen("class.txt", "w");
  51. if (outfile == NULL) {
  52. fprintf(stderr, "\nError opening file.\n");
  53. exit(1);
  54. }
  55.  
  56. fprintf(outfile, " %s", pStudent->name);
  57. fprintf(outfile, " %d", pStudent->zNumber);
  58. fprintf(outfile, " %s", pStudent->assignment);
  59. fprintf(outfile, " %lf", pStudent->score);
  60. fprintf(outfile, "%s\n", "zzzzzz");
  61. fclose(outfile);
  62. }
  63.  
  64. double getScore(Student* pStudent) {
  65.  
  66. return(pStudent->score);
  67.  
  68. }
  69.  
  70. void getAssignment(Student* pStudent) {
  71. double studentScore;
  72.  
  73. //Check if student has submitted assignment
  74. if (!strcmp(pStudent->assignment, "None")) {
  75. printf("Student has not submitted assignment. \n");
  76. }
  77. else {
  78. printf("What is this assignments grade?\n");
  79. scanf(" %lf", &studentScore);
  80.  
  81. assignScore(pStudent, studentScore);
  82. }
  83. }
  84.  
  85. void assignScore(Student* pStudent, double score) {
  86. char studentFilename[FILENAME_SIZE];
  87.  
  88. pStudent->score = score;
  89.  
  90. strcpy(studentFilename, pStudent->name);
  91. strcat(studentFilename, ".txt");
  92.  
  93. FILE *outfile;
  94.  
  95. // open file for writing
  96. outfile = fopen(studentFilename, "w");
  97. if (outfile == NULL) {
  98. fprintf(stderr, "\nError opening file.\n");
  99. exit(1);
  100. }
  101.  
  102. fprintf(outfile, " %s", pStudent->name);
  103. fprintf(outfile, " %d", pStudent->zNumber);
  104. fprintf(outfile, " %s", pStudent->assignment);
  105. fprintf(outfile, " %lf", pStudent->score);
  106. fclose(outfile);
  107.  
  108. }
  109.  
  110. void print(Student* pStudent) {
  111. printf("%s\n", pStudent->name);
  112. printf("%d\n", pStudent->zNumber);
  113. printf("%s\n", pStudent->assignment);
  114. printf("%lf\n", pStudent->score);
  115. printf("\n");
  116. }
  117.  
  118. void intialize(Student* pStudent, char name[], int zNumber) {
  119.  
  120. strcpy(pStudent->name, name);
  121. pStudent->zNumber = zNumber;
  122. pStudent->score = -1.0; // -1 => assignment not graded yet.
  123. }
  124.  
  125. /*void test(Student* pStudent) {
  126. //strcpy(pStudent->name,"Peter");
  127. intialize(pStudent, "Peter", 12345678);
  128. //pStudent->zNumber=12345678;
  129. submitAssignment(pStudent);
  130. //strcpy(pStudent->assignment,"This is a sample assignment");
  131. //pStudent->score=0.0;
  132. print(pStudent);
  133. } */
  134.  
  135. int main(int argc, char **argv)
  136. {
  137. Student aStudent;
  138.  
  139. welcomeScreen(&aStudent);
  140. // test(&aStudent);
  141.  
  142. return(0);
  143. }
  144.  
  145. void welcomeScreen(Student *pStudent)
  146. {
  147. char personType;
  148. char studentName[NAME_SIZE];
  149. char studentFilename[FILENAME_SIZE];
  150. int newNumber;
  151. int selection;
  152. char studentAssignment;
  153. char string[50];
  154.  
  155. //Check to see if this is a Student or Teacher using the program.
  156. printf("Hello! Are you a student (s) or a teacher (t)? \n");
  157. scanf(" %c", &personType);
  158.  
  159. //If it's a student.
  160. if ((personType == 's') || (personType == 'S')) {
  161. bool studentExists = false;
  162.  
  163. printf("What is your name? \n");
  164. scanf(" %s", studentName);
  165.  
  166. FILE *fp;
  167.  
  168. fp = fopen("class.txt", "r");
  169.  
  170. if ((fp = fopen("class.txt", "r")) == NULL) {
  171. printf("Error! opening file");
  172. // Program exits if file pointer returns NULL.
  173. exit(1);
  174. }
  175.  
  176. while ((!studentExists) && (fp == EOF)) {
  177. // reads text until newline
  178. fscanf(fp, "%[^\n]", string);
  179. //debugging only broo
  180. printf("Data from the file:\n%s", string);
  181.  
  182. if (string == studentName) {
  183. studentExists = true;
  184. }
  185. }
  186.  
  187. if (!studentExists) {
  188. fclose(fp);
  189. }
  190.  
  191. //Student doesnt exits
  192. if (!studentExists) {
  193. //Give greetings to the new student and check if they have an assignment to add or just registering.
  194. printf("Welcome new student! Let's create a record file for you.\n");
  195. printf("What is your zNumber?\n");
  196. scanf(" %d", &newNumber);
  197.  
  198. intialize(pStudent, studentName, newNumber);
  199.  
  200. printf("Here is your information:\n");
  201. print(pStudent);
  202.  
  203. printf("Do you have an assignment to turn in? (Y or N)\n");
  204. scanf(" %c", &studentAssignment);
  205.  
  206. if ((studentAssignment == 'y') || (studentAssignment == 'Y')) {
  207. submitAssignment(pStudent);
  208.  
  209. }
  210.  
  211. if ((studentAssignment == 'n') || (studentAssignment == 'N')) {
  212. printf("Thank you! Your student file has been created. You can come back later to submit your assignment.\n");
  213. strcpy(pStudent->assignment, "None");
  214. }
  215.  
  216. FILE *outfile;
  217.  
  218. // open file for writing
  219. outfile = fopen("class.txt", "w");
  220. if (outfile == NULL) {
  221. fprintf(stderr, "\nError opening file.\n");
  222. exit(1);
  223. }
  224.  
  225. fprintf(outfile, " %s", pStudent->name);
  226. fprintf(outfile, " %d", pStudent->zNumber);
  227. fprintf(outfile, " %s", pStudent->assignment);
  228. fprintf(outfile, " %lf", pStudent->score);
  229. fprintf(outfile, "%s\n", "zzzzzz");
  230.  
  231. fclose(fp);
  232. }
  233. else {
  234. //Student exists
  235. printf("Welcome back %s!\n", studentName);
  236.  
  237. fscanf(fp, "%s %d %s %lf", pStudent->name, &pStudent->zNumber, pStudent->assignment, &pStudent->score);
  238. fclose(fp);
  239.  
  240. printf("Would you like to check on a grade (1), print your information (2), or submit an assignment (3)?");
  241. scanf(" %d", &selection);
  242.  
  243. if (selection == 1) {
  244.  
  245. if (pStudent->score > -1) {
  246.  
  247. printf("Your grade is %lf", getScore(pStudent));
  248.  
  249. }
  250. else {
  251. printf("Your assignment has not yet been graded!\n");
  252. }
  253. }
  254. else if (selection == 2) {
  255.  
  256. print(pStudent);
  257.  
  258. }
  259. else if (selection == 3) {
  260. submitAssignment(pStudent);
  261. }
  262. }
  263. }
  264. else if ((personType == 't') || (personType == 'T')) {
  265.  
  266. printf("Greetings Professor!\n");
  267. printf("Enter a students name to grade their assignment:\n");
  268.  
  269. scanf(" %s", studentName);
  270.  
  271. FILE *fp;
  272.  
  273. strcpy(studentFilename, studentName);
  274. strcat(studentFilename, ".txt");
  275.  
  276. fp = fopen(studentFilename, "r");
  277.  
  278. fscanf(fp, "%s %d %s %lf", pStudent->name, &pStudent->zNumber, pStudent->assignment, &pStudent->score);
  279. fclose(fp);
  280.  
  281. getAssignment(pStudent);
  282. print(pStudent);
  283.  
  284.  
  285. }
  286. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement