Guest User

Source Code: Struct Arrays

a guest
Nov 4th, 2013
481
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 9.68 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <ctype.h>
  5.  
  6.  
  7. // maximum number of students in the database; can be adjusted as needed
  8. #define MAX 100
  9.  
  10. // define the student structure type 'student'
  11. typedef struct{
  12.     long id;
  13.     char first_name[50];
  14.     char last_name[50];
  15.     int age;
  16.     char department[5];
  17.     int grade;
  18. }student;
  19.  
  20. // define functions to be used later
  21. void add_student();
  22. void mod_student();
  23. void rmv_student();
  24. void lst_dpts();
  25. int prnt_avgs();
  26. void menu_options();
  27. void menu_test_input();
  28.  
  29. student st_array[MAX];  // define array of structures of type 'student'
  30. int number = 0;     // count of student body
  31.  
  32. int main(void) // Working correctly
  33. {
  34. /*
  35.     Function 'main' displays the menu and prompts for user selection of given options.
  36. */
  37.  
  38.     int item;
  39.  
  40.     // print menu items
  41.     printf("Main Menu:\nType the number of desired menu item\n\n");
  42.     puts("1. Add Student");
  43.     puts("2. Modify Student");
  44.     puts("3. Delete Student");
  45.     puts("4. List Students by Department");
  46.     puts("5. Print Student Averages");
  47.     puts("6. Exit\n");
  48.  
  49.     // retrieve an integer value from the user, assign to variable 'item'
  50.     scanf("%d", &item);
  51.     // send 'item' to test validity and then execute desired function
  52.     menu_test_input(item);
  53.  
  54.     return 0;
  55. }
  56.  
  57. void menu_test_input(int x) // Working correctly
  58. {
  59. /*
  60.     Function 'menu_test_input' receives an integer value from function 'main'
  61.     and tests that it is within the range 1-6. If this is true, the function
  62.     passes the same value to function 'menu_options'. If this is false, the
  63.     function gets an new integer value from the user and sends it to itself to
  64.     be retested.
  65. */
  66.  
  67.     // send value to function 'menu_options'
  68.     if (0 < x && x < 7){
  69.         menu_options(x);
  70.     }
  71.  
  72.     // retest if invalid input
  73.     else{
  74.         printf("Sorry, that input is invalid. Please enter a number that indicates a menu item.\n");
  75.         scanf("%d", &x);
  76.         menu_test_input(x);
  77.     }
  78. }
  79.  
  80. void menu_options(int x) // Working correctly
  81. {
  82. /*
  83.     Function 'menu_options' uses a switch statement and user input value from
  84.     function 'main' to determine which function to enter or action to take
  85. */
  86.  
  87.     long id;
  88.     char dpt[5];
  89.  
  90.     switch (x){
  91.  
  92.         case 1: // user inputs '1' indicating 'add student'
  93.             puts("\nTo add a student, enter the ID number of the New Student: ");
  94.             scanf("%ld", &id);
  95.             add_student(id);
  96.         break;
  97.  
  98.         case 2: // user inputs '2' indicating 'modify student'
  99.             puts("\nTo modify a student's information, enter the ID number of the student: ");
  100.             scanf("%ld", &id);
  101.             mod_student(id);
  102.             break;
  103.  
  104.         case 3: // user inputs '3' indicating 'delete student'
  105.             puts("\nTo remove a student's information, enter the ID number of the student: ");
  106.             scanf("%ld", &id);
  107.             rmv_student(id);
  108.  
  109.         case 4: // user inputs '4' indicating 'list students by department'
  110.             puts("\nEnter which department to list: \n");
  111.             scanf("%s", &dpt);
  112.             lst_dpts(dpt);
  113.             break;
  114.  
  115.         case 5: // user inputs '5' indicating 'print student averages'
  116.             // average = prnt_avgs();
  117.             // printf("The average grade of the students is %d", average);
  118.             break;
  119.  
  120.         case 6: // user inputs '6' indicating 'exit'
  121.             puts("Thanks! Bye!");
  122.             exit(1);
  123.             break;
  124.  
  125.         default: // shouldn't happen, tested in prior function 'menu_test_input'
  126.             break;
  127.    }
  128. }
  129.  
  130. void add_student(long id) // Working correctly
  131. {
  132. /*
  133.     Function 'add_student' receives a long int value from function 'menu_options' that
  134.     represents an ID number of a student and uses it to create a new structure of student
  135.     information to be added directly to array 'st_array'
  136. */
  137.  
  138.     // add one student to the total count (to be used as index value of new array element)
  139.     number++;
  140.  
  141.     // make sure that user doesn't exceed the length of the array
  142.     if ( number > MAX){
  143.         puts("Error, Memory Exhausted\n");
  144.         main();
  145.     }
  146.  
  147.     st_array[number].id = id;
  148.  
  149.     // Get data from user
  150.     printf("\nGreat! Now enter the student's last name: ");
  151.     scanf("%s", &st_array[number].last_name);
  152.  
  153.     printf("\nGreat! Now enter the student's first name: ");
  154.     scanf("%s", &st_array[number].first_name);
  155.  
  156.     printf("\nGreat! Now enter the student's age: ");
  157.     scanf("%d", &st_array[number].age);
  158.  
  159.     printf("\nGreat! Now enter the student's department: ");
  160.     scanf("%s", &st_array[number].department);
  161.  
  162.     printf("\nGreat! One more. Enter the student's grade: ");
  163.     scanf("%d", &st_array[number].grade);
  164.  
  165.     puts("\nStudent Added.\n");
  166.     main();
  167. }
  168.  
  169. void mod_student(long id) // Working correctly
  170. {
  171. /*
  172.     Function 'mod_student' receives a long int value from function 'menu options' that
  173.     represents an ID number of a student. If there exists an element within 'st_array'
  174.     with that ID number, the program prompts the user for the element of the field that
  175.     needs to be changed and changes that field after getting new data from the user.
  176. */
  177.  
  178.     int i; // iterator
  179.     int mod_choice; // used for menu
  180.  
  181.     for( i = 0; i < MAX; i++){
  182.         if ( st_array[i].id == id){
  183.             printf("%s %s, %d\n", st_array[i].first_name, st_array[i].last_name, st_array[i].id);
  184.  
  185.             // prompt user for which field needs to be changed
  186.             puts("\nWhich field would you like to change?\n");
  187.             puts("1. ID Number");
  188.             puts("2. First Name");
  189.             puts("3. Last Name");
  190.             puts("4. Age");
  191.             puts("5. Department");
  192.             puts("6. Grade");
  193.             puts("7. Cancel");
  194.             scanf("%d", &mod_choice);
  195.  
  196.             if ( 0 < mod_choice && mod_choice < 8){
  197.                 switch(mod_choice){
  198.  
  199.                     case 1:
  200.                         puts("\nEnter the new ID Number: ");
  201.                         scanf("%ld", &st_array[i].id);
  202.                         puts("\nDone\n");
  203.                         main();
  204.  
  205.                     case 2:
  206.                         puts("\nEnter the new First Name: ");
  207.                         scanf("%s", &st_array[i].first_name);
  208.                         puts("\nDone\n");
  209.                         main();
  210.  
  211.                     case 3:
  212.                         puts("\n Enter the new Last Name: ");
  213.                         scanf("%s", &st_array[i].last_name);
  214.                         puts("\nDone\n");
  215.                         main();
  216.  
  217.                     case 4:
  218.                         puts("\nEnter the new Age: ");
  219.                         scanf("%d", &st_array[i].age);
  220.                         puts("\nDone\n");
  221.                         main();
  222.  
  223.                     case 5:
  224.                         puts("\nEnter the new Department: ");
  225.                         scanf("%s", &st_array[i].department);
  226.                         puts("\nDone\n");
  227.                         main();
  228.  
  229.                     case 6:
  230.                         puts("\nEnter the new Grade: ");
  231.                         scanf("%d", &st_array[i].grade);
  232.                         puts("\nDone\n\n");
  233.                         main();
  234.  
  235.                     case 7:
  236.                         main();
  237.                 }
  238.             }
  239.             else if ( mod_choice < 1 || mod_choice > 6){
  240.                 puts("\nSorry, that input is invalid. Try again.");
  241.                 mod_student(id);
  242.             }
  243.         }
  244.     }
  245.     if ( i == MAX ){
  246.         printf("\nThere are no students with the ID %d\n\n", id);
  247.         main();
  248.     }
  249. }
  250.  
  251. void rmv_student(long id) // BROKEN
  252. {
  253. /*
  254.     Function 'rmv_student' receives a long int value from function 'menu_options' that
  255.     represents an ID number of a student. If there exists an element within 'st_array'
  256.     with that ID number, the program prompts the user to confirm the deletion after
  257.     displaying the element to the user.
  258. */
  259.  
  260.     int i; // iterator
  261.     char response; // used to confirm deletion
  262.  
  263.     for( i = 0; i < MAX; i++){
  264.         if ( st_array[i].id == id){
  265.             printf("Are you sure you want to delete %s %s, %d?\n", st_array[i].first_name, st_array[i].last_name, st_array[i].id);
  266.             puts("You will not be able to undo the deletion.");
  267.             puts("Enter 'y' to delete or 'n' to return to the main menu.");
  268.             response = getchar();
  269.  
  270.             switch (response){
  271.  
  272.                 case 'y':
  273.                     // delete
  274.  
  275.                 case 'Y':
  276.                     // delete
  277.  
  278.                 case 'n':
  279.                     main();
  280.  
  281.                 case 'N':
  282.                     main();
  283.  
  284.                 default:
  285.                     puts("Please enter 'y' or 'n'.");
  286.                     rmv_student(id);
  287.             }
  288.         }
  289.     }
  290.     if ( i == MAX ){
  291.         printf("\nThere are no students with ID %d.\n\n", id);
  292.         main();
  293.     }
  294. }
  295.  
  296. void lst_dpts(char dpt[5]) // Working correctly
  297. {
  298. /*
  299.     Function 'lst_dpts' searches the array 'st_array' for the string that it receives from
  300.     function 'menu_options', and lists all elements for which the field 'department' matches.
  301. */
  302.  
  303.     int i; // iterators
  304.     int cnt = 0;
  305.  
  306.     for (i = 0; i < MAX; i++){
  307.         if ( strcmp( st_array[i].department, dpt ) == 0){
  308.             printf("%s %s, %d\n", st_array[i].first_name, st_array[i].last_name, st_array[i].id);
  309.             cnt++;
  310.             break;
  311.         }
  312.     }
  313.     if (cnt == 0){
  314.         printf("There are no students in department %s.\n", dpt);
  315.     }
  316.     else{
  317.         printf("There are &d students in department %s.\n", cnt, dpt);
  318.     }
  319.  
  320.     main();
  321. }
Advertisement
Add Comment
Please, Sign In to add comment