Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <ctype.h>
- // maximum number of students in the database; can be adjusted as needed
- #define MAX 100
- // define the student structure type 'student'
- typedef struct{
- long id;
- char first_name[50];
- char last_name[50];
- int age;
- char department[5];
- int grade;
- }student;
- // define functions to be used later
- void add_student();
- void mod_student();
- void rmv_student();
- void lst_dpts();
- int prnt_avgs();
- void menu_options();
- void menu_test_input();
- student st_array[MAX]; // define array of structures of type 'student'
- int number = 0; // count of student body
- int main(void) // Working correctly
- {
- /*
- Function 'main' displays the menu and prompts for user selection of given options.
- */
- int item;
- // print menu items
- printf("Main Menu:\nType the number of desired menu item\n\n");
- puts("1. Add Student");
- puts("2. Modify Student");
- puts("3. Delete Student");
- puts("4. List Students by Department");
- puts("5. Print Student Averages");
- puts("6. Exit\n");
- // retrieve an integer value from the user, assign to variable 'item'
- scanf("%d", &item);
- // send 'item' to test validity and then execute desired function
- menu_test_input(item);
- return 0;
- }
- void menu_test_input(int x) // Working correctly
- {
- /*
- Function 'menu_test_input' receives an integer value from function 'main'
- and tests that it is within the range 1-6. If this is true, the function
- passes the same value to function 'menu_options'. If this is false, the
- function gets an new integer value from the user and sends it to itself to
- be retested.
- */
- // send value to function 'menu_options'
- if (0 < x && x < 7){
- menu_options(x);
- }
- // retest if invalid input
- else{
- printf("Sorry, that input is invalid. Please enter a number that indicates a menu item.\n");
- scanf("%d", &x);
- menu_test_input(x);
- }
- }
- void menu_options(int x) // Working correctly
- {
- /*
- Function 'menu_options' uses a switch statement and user input value from
- function 'main' to determine which function to enter or action to take
- */
- long id;
- char dpt[5];
- switch (x){
- case 1: // user inputs '1' indicating 'add student'
- puts("\nTo add a student, enter the ID number of the New Student: ");
- scanf("%ld", &id);
- add_student(id);
- break;
- case 2: // user inputs '2' indicating 'modify student'
- puts("\nTo modify a student's information, enter the ID number of the student: ");
- scanf("%ld", &id);
- mod_student(id);
- break;
- case 3: // user inputs '3' indicating 'delete student'
- puts("\nTo remove a student's information, enter the ID number of the student: ");
- scanf("%ld", &id);
- rmv_student(id);
- case 4: // user inputs '4' indicating 'list students by department'
- puts("\nEnter which department to list: \n");
- scanf("%s", &dpt);
- lst_dpts(dpt);
- break;
- case 5: // user inputs '5' indicating 'print student averages'
- // average = prnt_avgs();
- // printf("The average grade of the students is %d", average);
- break;
- case 6: // user inputs '6' indicating 'exit'
- puts("Thanks! Bye!");
- exit(1);
- break;
- default: // shouldn't happen, tested in prior function 'menu_test_input'
- break;
- }
- }
- void add_student(long id) // Working correctly
- {
- /*
- Function 'add_student' receives a long int value from function 'menu_options' that
- represents an ID number of a student and uses it to create a new structure of student
- information to be added directly to array 'st_array'
- */
- // add one student to the total count (to be used as index value of new array element)
- number++;
- // make sure that user doesn't exceed the length of the array
- if ( number > MAX){
- puts("Error, Memory Exhausted\n");
- main();
- }
- st_array[number].id = id;
- // Get data from user
- printf("\nGreat! Now enter the student's last name: ");
- scanf("%s", &st_array[number].last_name);
- printf("\nGreat! Now enter the student's first name: ");
- scanf("%s", &st_array[number].first_name);
- printf("\nGreat! Now enter the student's age: ");
- scanf("%d", &st_array[number].age);
- printf("\nGreat! Now enter the student's department: ");
- scanf("%s", &st_array[number].department);
- printf("\nGreat! One more. Enter the student's grade: ");
- scanf("%d", &st_array[number].grade);
- puts("\nStudent Added.\n");
- main();
- }
- void mod_student(long id) // Working correctly
- {
- /*
- Function 'mod_student' receives a long int value from function 'menu options' that
- represents an ID number of a student. If there exists an element within 'st_array'
- with that ID number, the program prompts the user for the element of the field that
- needs to be changed and changes that field after getting new data from the user.
- */
- int i; // iterator
- int mod_choice; // used for menu
- for( i = 0; i < MAX; i++){
- if ( st_array[i].id == id){
- printf("%s %s, %d\n", st_array[i].first_name, st_array[i].last_name, st_array[i].id);
- // prompt user for which field needs to be changed
- puts("\nWhich field would you like to change?\n");
- puts("1. ID Number");
- puts("2. First Name");
- puts("3. Last Name");
- puts("4. Age");
- puts("5. Department");
- puts("6. Grade");
- puts("7. Cancel");
- scanf("%d", &mod_choice);
- if ( 0 < mod_choice && mod_choice < 8){
- switch(mod_choice){
- case 1:
- puts("\nEnter the new ID Number: ");
- scanf("%ld", &st_array[i].id);
- puts("\nDone\n");
- main();
- case 2:
- puts("\nEnter the new First Name: ");
- scanf("%s", &st_array[i].first_name);
- puts("\nDone\n");
- main();
- case 3:
- puts("\n Enter the new Last Name: ");
- scanf("%s", &st_array[i].last_name);
- puts("\nDone\n");
- main();
- case 4:
- puts("\nEnter the new Age: ");
- scanf("%d", &st_array[i].age);
- puts("\nDone\n");
- main();
- case 5:
- puts("\nEnter the new Department: ");
- scanf("%s", &st_array[i].department);
- puts("\nDone\n");
- main();
- case 6:
- puts("\nEnter the new Grade: ");
- scanf("%d", &st_array[i].grade);
- puts("\nDone\n\n");
- main();
- case 7:
- main();
- }
- }
- else if ( mod_choice < 1 || mod_choice > 6){
- puts("\nSorry, that input is invalid. Try again.");
- mod_student(id);
- }
- }
- }
- if ( i == MAX ){
- printf("\nThere are no students with the ID %d\n\n", id);
- main();
- }
- }
- void rmv_student(long id) // BROKEN
- {
- /*
- Function 'rmv_student' receives a long int value from function 'menu_options' that
- represents an ID number of a student. If there exists an element within 'st_array'
- with that ID number, the program prompts the user to confirm the deletion after
- displaying the element to the user.
- */
- int i; // iterator
- char response; // used to confirm deletion
- for( i = 0; i < MAX; i++){
- if ( st_array[i].id == id){
- 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);
- puts("You will not be able to undo the deletion.");
- puts("Enter 'y' to delete or 'n' to return to the main menu.");
- response = getchar();
- switch (response){
- case 'y':
- // delete
- case 'Y':
- // delete
- case 'n':
- main();
- case 'N':
- main();
- default:
- puts("Please enter 'y' or 'n'.");
- rmv_student(id);
- }
- }
- }
- if ( i == MAX ){
- printf("\nThere are no students with ID %d.\n\n", id);
- main();
- }
- }
- void lst_dpts(char dpt[5]) // Working correctly
- {
- /*
- Function 'lst_dpts' searches the array 'st_array' for the string that it receives from
- function 'menu_options', and lists all elements for which the field 'department' matches.
- */
- int i; // iterators
- int cnt = 0;
- for (i = 0; i < MAX; i++){
- if ( strcmp( st_array[i].department, dpt ) == 0){
- printf("%s %s, %d\n", st_array[i].first_name, st_array[i].last_name, st_array[i].id);
- cnt++;
- break;
- }
- }
- if (cnt == 0){
- printf("There are no students in department %s.\n", dpt);
- }
- else{
- printf("There are &d students in department %s.\n", cnt, dpt);
- }
- main();
- }
Advertisement
Add Comment
Please, Sign In to add comment