fork download
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <ctype.h>
  4. #include <time.h>
  5. #include <stdlib.h>
  6.  
  7. #define GREETING "\n Student Record System\n ---------------------\n"
  8.  
  9. #define MAX_NUM_RECORDS 25
  10. //struct NAME fields:
  11. #define MAX_LEN_NAME_FIRST 15
  12. #define MAX_LEN_NAME_LAST 15
  13. #define ID_MIN 001
  14. #define ID_MAX 999
  15. //struct MARK fields
  16. #define MIN_MARK 0
  17. #define MAX_MARK 100
  18. #define ASSIGN_W 0.60
  19. #define EXAM_W 0.40
  20. #define PASS 40
  21.  
  22.  
  23. //-----------------------
  24. //User-Define Data Types:
  25. //-----------------------
  26.  
  27. struct NAME
  28. {
  29. char FirstName[MAX_LEN_NAME_FIRST];
  30. char LastName[MAX_LEN_NAME_LAST];
  31. int StudentID;
  32.  
  33. };
  34.  
  35.  
  36. struct MARK
  37. {
  38. struct NAME Student;
  39. int AssignMark;
  40. int ExamMark;
  41. float ModMark;
  42. char ModGrade[5];
  43.  
  44. };
  45.  
  46.  
  47.  
  48. //--------------------
  49. //Function prototypes:
  50. //--------------------
  51.  
  52. //Menu:
  53. void ShowMainMenu();
  54.  
  55. //Array - Record handling:
  56. void DisplayRecordsAll(struct MARK AllRecords[], int NumRecords);
  57. void DisplayRecordsSelected(struct MARK AllRecords[], int NumRecords, int MaxNumRecords);
  58. void AddRecords(struct MARK AllRecords[], int *pNumRecords, int MaxNumRecords);
  59. void UpdateRecords(struct MARK AllRecords[], int *pNumRecords);
  60.  
  61.  
  62. //Generic Functions:
  63. int GetValidatedInteger(int Min, int Max);
  64. void Pause();
  65. char GetValidatedString(char ValidatedString[], int MinLength, int MaxLength);
  66. char GetValidatedYesNo();
  67. float GetValidatedFloat2(float Min, float Max, int Precision);
  68.  
  69. int main()
  70. {
  71. system("mode CON: COLS=150 ");
  72. int MenuChoice = 0;
  73. struct MARK AllRecords[MAX_NUM_RECORDS];
  74. int NumRecords = 0; //ACTUAL Number of Records
  75.  
  76.  
  77.  
  78. do
  79. {
  80. //Clear screen, display menu and get user-selected menu item
  81. ShowMainMenu();
  82. MenuChoice = GetValidatedInteger(0, 5);
  83.  
  84. //Action user-selected menu item
  85. switch (MenuChoice)
  86. {
  87. case 1:
  88. AddRecords(AllRecords,&NumRecords, MAX_NUM_RECORDS);
  89. break;
  90. case 2:
  91. DisplayRecordsAll(AllRecords, NumRecords);
  92. break;
  93. case 3:
  94. DisplayRecordsSelected(AllRecords, NumRecords, MAX_NUM_RECORDS);
  95. break;
  96. case 4:
  97. UpdateRecords(AllRecords, NumRecords);
  98. break;
  99. case 5:
  100.  
  101. break;
  102. case 0:
  103. printf("\nQuitting Program");
  104. printf("\n\n");
  105. Pause();
  106. break;
  107. }
  108. } while (MenuChoice != 0);
  109.  
  110. return(0);
  111.  
  112. }
  113.  
  114.  
  115.  
  116. void ShowMainMenu()
  117. {
  118. system("cls");
  119. printf("%s", GREETING);
  120. printf("\n MAIN MENU\n ----------");
  121. printf("\n 1. Add record(s) ");
  122. printf("\n 2. Display all records");
  123. printf("\n 3. Display selected record(s)");
  124. printf("\n 4. Update record(s)");
  125. printf("\n 5. Display Cohort Average(s)");
  126. printf("\n 0. Quit");
  127. printf("\n\n Enter your choice: ");
  128. }
  129.  
  130. //STUBS
  131.  
  132. void DisplayRecordsAll(struct MARK AllRecords[], int NumRecords)
  133. {
  134.  
  135.  
  136. int i = 0;
  137.  
  138.  
  139. if (NumRecords == 0)
  140. {
  141. printf("\nThere are no records to display. ");
  142. Pause();
  143. }
  144. else
  145. {
  146. system("cls");
  147. printf("\n ID number | Name | Assignment mark | Exam Mark | Module grade");
  148. for (i = 0; i < NumRecords; i++)
  149. {
  150.  
  151.  
  152. printf("\n%.3d %s %s %d %d %1.2f %s \n",
  153. AllRecords[i].Student.StudentID,
  154. AllRecords[i].Student.FirstName,
  155. AllRecords[i].Student.LastName,
  156. AllRecords[i].AssignMark,
  157. AllRecords[i].ExamMark,
  158. AllRecords[i].ModMark,
  159. AllRecords[i].ModGrade);
  160. }
  161.  
  162. Pause();
  163. }
  164. }
  165.  
  166.  
  167. void DisplayRecordsSelected(struct MARK AllRecords[], int NumRecords,int MaxNumRecords)
  168. {
  169. int i = 0;
  170. int IDSearch = 0;
  171. char AddAnother = 'Y';
  172.  
  173. do
  174. {
  175.  
  176.  
  177. if (NumRecords == 0)
  178. {
  179. printf("\nThere are no records to display. ");
  180. Pause();
  181. return;
  182. }
  183.  
  184. printf("\nEnter Student's ID: ");
  185. IDSearch = GetValidatedInteger(0, ID_MAX);
  186.  
  187.  
  188.  
  189. for (i = 0; i < NumRecords; i++)
  190. {
  191.  
  192.  
  193. if (IDSearch == AllRecords[i].Student.StudentID)
  194. {
  195. system("cls");
  196. printf("\n ID Number | Name ");
  197. printf("\n --------- | ---- ");
  198.  
  199. printf("\n %.3d%s %s", AllRecords[i].Student.StudentID,
  200. AllRecords[i].Student.FirstName,
  201. AllRecords[i].Student.LastName);
  202.  
  203.  
  204. }
  205.  
  206.  
  207. }
  208.  
  209. printf("\nDo you want to view another record?(Y/N): ");
  210. AddAnother = GetValidatedYesNo();
  211.  
  212.  
  213.  
  214. } while (AddAnother == 'Y');
  215.  
  216. }
  217.  
  218.  
  219.  
  220. void AddRecords(struct MARK AllRecords[], int *pNumRecords, int MaxNumRecords)
  221. {
  222. char AddAnother; // 'Y' or 'N'
  223. int ID = 0;
  224. float ModMark = 0;
  225.  
  226.  
  227. // ADD NEW RECORDS(S) TO ARRAY (until user finished or arry full)
  228. do
  229. {
  230. if (*pNumRecords == MaxNumRecords)
  231. {
  232. printf("\nThe system is full. You cannot add another record. Returning to main menu. ");
  233. Pause();
  234. ShowMainMenu();
  235. }
  236.  
  237.  
  238.  
  239.  
  240. AllRecords[*pNumRecords].Student.StudentID = *pNumRecords + 1;
  241.  
  242.  
  243. printf("\nEnter the Students's First Name: ");
  244. GetValidatedString(AllRecords[*pNumRecords].Student.FirstName, 1, MAX_LEN_NAME_FIRST);
  245.  
  246.  
  247. printf("\nEnter the Student's Last Name: ");
  248. GetValidatedString(AllRecords[*pNumRecords].Student.LastName, 1, MAX_LEN_NAME_LAST);
  249.  
  250.  
  251. printf("\nEnter the Exam mark (%d - %d): ", MIN_MARK, MAX_MARK);
  252. AllRecords[*pNumRecords].ExamMark = GetValidatedInteger(MIN_MARK, MAX_MARK);
  253.  
  254. printf("\nEnter the Assignment mark (%d - %d): ", MIN_MARK, MAX_MARK);
  255. AllRecords[*pNumRecords].AssignMark = GetValidatedInteger(MIN_MARK, MAX_MARK);
  256.  
  257.  
  258. ModMark = (AllRecords[*pNumRecords].ExamMark * EXAM_W) + (AllRecords[*pNumRecords].AssignMark * ASSIGN_W);
  259. AllRecords[*pNumRecords].ModMark = ModMark;
  260.  
  261.  
  262.  
  263. if (AllRecords[*pNumRecords].ModMark < PASS)
  264. {
  265. strcpy_s(AllRecords[*pNumRecords].ModGrade, sizeof(AllRecords[*pNumRecords].ModGrade), "FAIL");
  266. }
  267. else
  268. {
  269. strcpy_s(AllRecords[*pNumRecords].ModGrade, sizeof(AllRecords[*pNumRecords].ModGrade), "PASS");
  270.  
  271. }
  272.  
  273.  
  274.  
  275. // Update number of records (i.e. Add 1 to *pNumRecords)
  276. *pNumRecords = *pNumRecords + 1;
  277.  
  278.  
  279.  
  280. // Add another?
  281. AddAnother = 'N'; //Re-initialise
  282. if (*pNumRecords < MaxNumRecords)
  283. {
  284. printf("\n Do you want to add another records (Y/N) ? ");
  285. AddAnother = GetValidatedYesNo();
  286.  
  287. if (AddAnother == 'Y')
  288. {
  289. //Clear screen below [menu + user choice]
  290. //(Actually: delete + re-display menu + user choice).
  291. ShowMainMenu();
  292. printf("1\n");
  293. }
  294. }
  295. else
  296. {
  297. printf("\nThe system is storing the maximum number of records. Returning to main menu. ");
  298. Pause();
  299. ShowMainMenu();
  300. }
  301. } while (AddAnother == 'Y');
  302. }
  303.  
  304. void UpdateRecords(struct MARK AllRecords[], int *pNumRecords)
  305. {
  306. int i = 0;
  307. int NumChoice = 0;
  308. int NumChoice2 = 0;
  309. char AddAnother;
  310. float ModMark = 0;
  311.  
  312. if (pNumRecords == 0)
  313. {
  314. printf("\nThere are no records to display. ");
  315. Pause();
  316. return;
  317. }
  318.  
  319. system("cls");
  320. printf("\n ID number | Name | Assignment mark | Exam Mark | Module grade");
  321. for (i = 0; i < pNumRecords; i++)
  322. {
  323.  
  324.  
  325. printf("\n%.3d %s %s %d %d %1.0f %s \n",
  326. AllRecords[i].Student.StudentID,
  327. AllRecords[i].Student.FirstName,
  328. AllRecords[i].Student.LastName,
  329. AllRecords[i].AssignMark,
  330. AllRecords[i].ExamMark,
  331. AllRecords[i].ModMark,
  332. AllRecords[i].ModGrade);
  333.  
  334.  
  335. printf("\n\nEnter the record number you wish to update: ");
  336. NumChoice = GetValidatedInteger(1, MAX_NUM_RECORDS) - 1;
  337.  
  338. system("cls");
  339. printf("\n Number | Item");
  340. printf("\n 1. First Name");
  341. printf("\n 2. Last Name");
  342. printf("\n 3. Assignment Mark");
  343. printf("\n 4. Exam Mark");
  344.  
  345. printf("\n\nEnter the number of field you wish to edit: ");
  346. NumChoice2 = GetValidatedInteger(1, 4);
  347.  
  348.  
  349. switch (NumChoice2)
  350. {
  351. case 1:
  352. printf("\nEnter the Student's First Name: ");
  353. GetValidatedString(AllRecords[NumChoice].Student.LastName, 1, MAX_LEN_NAME_FIRST);
  354. break;
  355. case 2:
  356. printf("\nEnter the Sutdent's Surname: ");
  357. GetValidatedString(AllRecords[NumChoice].Student.FirstName, 1, MAX_LEN_NAME_LAST);
  358. break;
  359. case 3:
  360. printf("\nEnter the Assignemt Mark (0-100): ");
  361. AllRecords[NumChoice].AssignMark = GetValidatedInteger(MIN_MARK, MAX_MARK);
  362. break;
  363. case 4:
  364. printf("\nEnter the Exam Mark (0-100): ");
  365. AllRecords[NumChoice].ExamMark = GetValidatedInteger(MIN_MARK, MAX_MARK);
  366. break;
  367. }
  368.  
  369.  
  370. AddAnother = 'N';
  371.  
  372. printf("\nDo you want to update another field?(Y/N): ");
  373. AddAnother = GetValidatedYesNo();
  374.  
  375. if (AddAnother == 'Y')
  376. {
  377. UpdateRecords(AllRecords, pNumRecords);
  378.  
  379. }
  380.  
  381. }
  382. }
  383.  
  384.  
  385.  
  386. int GetValidatedInteger(int Min, int Max)
  387. {
  388. //DECLARATIONS + INITILISATION of values
  389.  
  390. // Local Constants:
  391. const int cTrue = 1, cFalse = 0;
  392.  
  393. // Input Variables:
  394. float Input = 0; // NB Input read into a FLOAT
  395.  
  396. // Intermediate Variables:
  397. int ItemRead = cFalse; //Flag
  398. int Valid = cFalse; //Flag - Mandatory initialisation to cFalse
  399.  
  400. // Output Variables:
  401. // None
  402. //-----------------------------------------------------------
  403.  
  404. do
  405. {
  406.  
  407. ItemRead = scanf_s("%f", &Input);
  408. fflush(stdin);
  409.  
  410.  
  411. if (ItemRead == cFalse)
  412. {
  413. printf("Invalid - must be a number! Please try again (%d-%d): ", Min, Max);
  414. }
  415.  
  416. /* Validate - Not whole number? */
  417. else if (Input != (int)Input)
  418. {
  419. printf("Invalid - must be an integer! Please try again (%d-%d): ", Min, Max);
  420. }
  421.  
  422.  
  423. else if ((Input < Min) || (Input > Max))
  424. {
  425. printf("Invalid - out-of-range! Please try again (%d-%d): ", Min, Max);
  426. }
  427. else
  428. {
  429. Valid = cTrue;
  430. }
  431.  
  432. } while (Valid == cFalse);
  433.  
  434. return((int)Input);
  435. }
  436.  
  437. void Pause()
  438. {
  439. printf("\n\n Press the ENTER key.");
  440. }
  441.  
  442. char GetValidatedString(char ValidatedString[], int MinLength, int MaxLength)
  443. {
  444.  
  445. //Initialising True and False variables
  446. const char cFalse = 0, cTrue = 1;
  447. char Success = cFalse;
  448. char YesorNo;
  449.  
  450.  
  451. char Input[255];
  452.  
  453.  
  454. size_t InputArrayMaxLen = sizeof(Input) - 2;
  455. size_t Length;
  456.  
  457.  
  458. if (MaxLength > (int)InputArrayMaxLen)
  459. {
  460. printf("\n\nRequired maximum lenfth for called GetValidatedString() function"
  461. " \nCannot exceed %d characters", (int)InputArrayMaxLen);
  462. printf("\nData not read. Please contact developer.");
  463. Pause();
  464. return(Success);
  465. }
  466.  
  467. do
  468. {
  469.  
  470. do
  471. {
  472.  
  473. YesorNo = 'Y';
  474. Input[InputArrayMaxLen + 1] = 'z';
  475. Input[InputArrayMaxLen] = 'z';
  476.  
  477. fgets(Input, sizeof(Input), stdin);
  478. fflush(stdin);
  479.  
  480.  
  481. if ((Input[InputArrayMaxLen + 1] == '\0') && (Input[InputArrayMaxLen] != '\n'))
  482. {
  483. printf("\n\nRequired maximum length for called GetValidatedString() function "
  484. "\ncannot exceed %d characters", InputArrayMaxLen);
  485.  
  486. printf("\n\nDo you wish to try again (Y?N)?: ");
  487. YesorNo = GetValidatedYesNo();
  488.  
  489. if (YesorNo == 'N')
  490. {
  491. printf("Data not read. Please contact developer.");
  492. Pause();
  493. return(Success); //returns cFalse
  494. }
  495. else
  496. {
  497. printf("\nRe-enter (%d - %d characters): ", MinLength, MaxLength);
  498. }
  499. }
  500.  
  501. else
  502. {
  503. YesorNo = 'N';
  504. }
  505.  
  506. } while (YesorNo == 'Y');
  507.  
  508.  
  509.  
  510. Length = strlen(Input) - 1; // length contains the correct updated string length
  511. Input[Length] = '\0';
  512.  
  513. //String length validation (MinLength -> Maxlength)
  514. if ((Length < (size_t)MinLength) || (Length >(size_t)MaxLength))
  515. {
  516. printf("Invalid! Please try again (Length %d-%d): ", MinLength, MaxLength);
  517. }
  518. else
  519. {
  520. Success = cTrue;
  521. }
  522. } while (Success == cFalse);
  523.  
  524. //Transfer validated string to ValidatedString
  525. strcpy_s(ValidatedString, MaxLength + 1, Input);
  526.  
  527. return(Success); // Returns cTrue
  528.  
  529. }
  530.  
  531. char GetValidatedYesNo()
  532. {
  533. //Local Constants:
  534. const int cTrue = 1, cFalse = 0;
  535.  
  536. //Input Variables:
  537. char Input = 0;
  538.  
  539. //Intermediate Variables:
  540. char ItemRead = cFalse;
  541. int Valid = cFalse;
  542.  
  543.  
  544. //CONDITIONAL Do loop for Validating 'Y' or 'N'
  545. do
  546. {
  547. ItemRead = scanf_s("%c", &Input);
  548. fflush(stdin);
  549.  
  550.  
  551. if ((Input == 'Y') || (Input == 'N') || (Input == 'y') || (Input == 'n'))
  552. {
  553. Valid = cTrue;
  554.  
  555. }
  556. else
  557. {
  558. printf("Invalid input! Please try again:");
  559. }
  560. } while (Valid == cFalse);
  561.  
  562.  
  563. // Switches lowercase to uppercase should they enter y/n
  564. return(toupper(Input));
  565.  
  566. }
  567.  
  568. float GetValidatedFloat2(float Min, float Max, int Precision)
  569. {
  570. //DECLARATIONS + INITILISATION of values
  571.  
  572. // Local Constants:
  573. const int cTrue = 1, cFalse = 0;
  574.  
  575. // Input Variables:
  576. float Input = 0; // NB Input read into a FLOAT
  577.  
  578. // Intermediate Variables:
  579. int ItemRead = cFalse; //Flag
  580. int Valid = cFalse; //Flag - Mandatory initialisation to cFalse
  581.  
  582. // Output Variables:
  583. // None
  584. //-----------------------------------------------------------
  585.  
  586. do
  587. {
  588.  
  589. ItemRead = scanf_s("%f", &Input);
  590. fflush(stdin);
  591.  
  592.  
  593. if (ItemRead == cFalse)
  594. {
  595. printf("Invalid - must be a number! Please try again (%f-%f): ", Min, Max);
  596. }
  597.  
  598. /* Validate - Not whole number? */
  599. else if (Input != (int)Input)
  600. {
  601. printf("Invalid - must be a float! Please try again (%f-%f): ", Min, Max);
  602. }
  603.  
  604.  
  605. else if ((Input < Min) || (Input > Max))
  606. {
  607. printf("Invalid - out-of-range! Please try again (%f-%f): ", Min, Max);
  608. }
  609. else
  610. {
  611. Valid = cTrue;
  612. }
  613.  
  614. } while (Valid == cFalse);
  615.  
  616. return((float)Input);
  617. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.c: In function 'main':
prog.c:97:30: warning: passing argument 2 of 'UpdateRecords' makes pointer from integer without a cast [-Wint-conversion]
    UpdateRecords(AllRecords, NumRecords);
                              ^
prog.c:59:6: note: expected 'int *' but argument is of type 'int'
 void UpdateRecords(struct MARK AllRecords[], int *pNumRecords);
      ^
prog.c: In function 'AddRecords':
prog.c:265:4: warning: implicit declaration of function 'strcpy_s' [-Wimplicit-function-declaration]
    strcpy_s(AllRecords[*pNumRecords].ModGrade, sizeof(AllRecords[*pNumRecords].ModGrade), "FAIL");
    ^
prog.c:223:6: warning: unused variable 'ID' [-Wunused-variable]
  int ID = 0;
      ^
prog.c: In function 'UpdateRecords':
prog.c:321:16: warning: comparison between pointer and integer
  for (i = 0; i < pNumRecords; i++)
                ^
prog.c:310:8: warning: unused variable 'ModMark' [-Wunused-variable]
  float ModMark = 0;
        ^
prog.c: In function 'GetValidatedInteger':
prog.c:407:14: warning: implicit declaration of function 'scanf_s' [-Wimplicit-function-declaration]
   ItemRead = scanf_s("%f", &Input);
              ^
prog.c: In function 'GetValidatedYesNo':
prog.c:541:7: warning: variable 'ItemRead' set but not used [-Wunused-but-set-variable]
  char ItemRead = cFalse;
       ^
/home/2xWhYU/ccnqg4kx.o: In function `GetValidatedInteger':
prog.c:(.text+0x192): undefined reference to `scanf_s'
/home/2xWhYU/ccnqg4kx.o: In function `GetValidatedYesNo':
prog.c:(.text+0x2ca): undefined reference to `scanf_s'
/home/2xWhYU/ccnqg4kx.o: In function `GetValidatedString':
prog.c:(.text+0x540): undefined reference to `strcpy_s'
/home/2xWhYU/ccnqg4kx.o: In function `AddRecords':
prog.c:(.text+0x675): undefined reference to `strcpy_s'
/home/2xWhYU/ccnqg4kx.o: In function `GetValidatedFloat2':
prog.c:(.text+0x9b2): undefined reference to `scanf_s'
collect2: error: ld returned 1 exit status
stdout
Standard output is empty