Register Now

Login

Lost Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Login

Register Now

Welcome to All Test Answers

Assignment 5 – Dynamic Linked Lists and File Manipulation

60-141 – Introduction to Programming II
Through this assignment, you are going to be maintaining a file of all students and their registered courses to learn the concepts of structures, pointers to structures, dynamic memory allocation and file manipulation. For this, a structure called “studentInfo” with the following member elements should be defined:
• StudentID – 9 char long
• First Name – 20 char long
• Last Name – 25 char long
• Number of Courses Attending – integer
• Array of CourseInfo – a 10 element array of courseInfo elements
• next – Pointer to the next student structure in the list
where the CourseInfo structure has been defined as follow:
struct CourseInfo{
int courseID;
char courseName[30];
};
Your task is to:
Write a complete, well documented C program that will be able to:
a. Add a new student
b. Delete a student and all information related to that student
c. Search for a student and their information
d. Display a list of current students
e. Save student information to file
f. Load student information from file
Assignment Description:
You are provided with a data file called “studentList.txt” that has the data for a number of students. You are required to read this data from the input file into a sorted linked list data structure. The linked list must be sorted based on the StudentID. The sample input file will end with a line that has three stars. An example of an input file with two fictional students is:
assignment 5
For each student, the data file is formatted line by line in the following manner:

After loading the student list, your program should be able to interactively ask the user for input. Your
interactive menu should have the following inputs:
1. Add new student
2. Delete a student
3. Search for a student
4. Display current students
5. Save student information to file
6. Exit
Hints:
1- The following points are to be considered:
a. Student ID should be unique and 9 characters of numbers (such as 123456789, 103449977)
b. First name and Last Name should be started with capital letters (upper case).
c. Student information should be sorted based on the student IDs both in the linked list and in the
input/output file.
2- Your program should implement at least the following functions:
a) addStudent () : To add a new student and his/her registered courses.
– Make sure the first letter of the first and last names is upper case (capital letter).
– The student ID should be unique; you cannot have two students with the same StudentID. So,
before adding a student, search for the studentID to be sure that you have not had it previously
entered.
– If the linked list is empty, the new student is the head of the list. If not, search through the
linked list to find the appropriate spot within the sorted linked list.
b) deleteStudent(): To delete a student information using its StudentID.
– Search the linked list for a student matching the studentID passed in. If it is found, delete it
from the linked list.
– Note that the linked list is sorted based on studentID!
c) searchStudentID(): To search for a student using studentID
– You do not have to search all the linked list as it is sorted based on studendIDs.
– This function can be called from addStudent() and deleteStudent() functions.
d) searchStudentlName(): To search for a student using his/her last name and display the related
information if the student exists.
– You have to search all the linked list as it is not sorted based on last names
– Before starting the search, make sure the name supplied by the user has a capital letter for the
first letter.
e) displayStudentInfo(): To display the current student information that exists in the linked list
f) saveStudentInfo(): To save student information from the sorted linked list to a file (inputStudents.txt)
g) loadStudentinfo(): To read all the student information from an input file (studentList.txt)
– This function should be called at the beginning of you program to load all previous student
information saved in the input file
– Student information should be formatted and stored in a sorted linked list.
h) exit(): To save student information in a file (inputStudents.txt) and exit from the program
Sample Run
A sample run of the program, with its interactive menu is shown below.
1. Add new student
2. Delete a student
3. Search for a student
4. Display current students
5. Save student information to file
6. Exit
> 1
Adding new student:
Student ID : 222222222
First name : john
Last name : Rezaei
Number of courses: 2
Course ID: 412
Course Name: CMSC
Course ID: 123
Course Name: MATH
1. Add new student
2. Delete a student
3. Search for a student
4. Display current students
5. Save student information to file
6. Exit
> 4
Student 1:
111111111
Lisa
Porter
3
ENEE 114
CMSC 412
ENME 515
Student 2:
222222222
John
Rezaei
2
CMSC 412
MATH 123
Student 3:
333333333
Alex
Simpson
1
CMSC 412
1. Add new student
2. Delete a student
3. Search for a student
4. Display current students
5. Save student information to file
6. Exit
> 3
What is the last name of student? porter
111111111
Lisa
Porter
3
ENEE 114
CMSC 412
ENME 515
1. Add new student
2. Delete a student
3. Search for a student
4. Display current students
5. Save student information to file
6. Exit
> 2
Student ID: 111111111
Student information deleted.
1. Add new student
2. Delete a student
3. Search for a student
4. Display current students
5. Save student information to file
6. Exit
> 4
Student 1:
222222222
John
Rezaei
2
CMSC 412
MATH 123
Student 2:
333333333
Alex
Simpson
1
CMSC 412
1. Add new student
2. Delete a student
3. Search for a student
4. Display current students
5. Save student information to file
6. Exit
> 6
Save student information to file before leaving? y
Student List saved successfully.
Bye!
Requirements:
– Write and document a complete C program that is capable of satisfying the requirements of this
assignment problem.

Answer:

/*
Title: Assignment #5: Dynamic Linked Lists and File Manipulation
Objective: Create a program with one main that can add a new student, delete a student and all information
related to that student, search for a students and their information, display a list of current students,
save student information to file and load student information from file.
*/

//Inclusions
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>

#define MAX 10

//Where we hold all our information
typedef struct{
    char studentID[10];
	char firstname[21];
    char lastname [26];
	char cNum [3];
    char courseID [10];
    char courseName[30];
	struct studentInfo *next;
} studentInfo;

studentInfo *first = NULL;
studentInfo *last = NULL;
studentInfo studentList[MAX];
//Global variable used in multiple functions
int counter = 0;

//Function Prototypes
void addStudent();//Adding studentInfos to our list
void DeleteStudent();//Removes a certain info from the info list.
void searchStudentID();//Searches for specific student's informations.
void searchStudentName();//Searches for specific student's informations.
void displayStudentInfo();//Display info list on the screen.
void saveStudentInfo();//Saving info info to a file.
void loadStudentInfo();//To load a previously saved info list.
void wordCap(char string[], int n);//To capitalize a name.
void Exit();//To exit the program


int main(){
    int choice;
	char save,temp[5];

    while(1)
    {
         printf("1: Add new student\n"
			   "2: Delete a student\n"
			   "3: Search for a student\n"
			   "4: Display current students\n"
			   "5: Save student information to file\n"
			   "6: Load student information from file\n"
			   "7: Exit\n"
			   "> ");
        scanf("%d", &choice);

        switch(choice)
        {
            case 1 : addStudent(); break;
			case 2 :DeleteStudent();break;
            case 3 : searchStudentName(); break;
			case 4 : displayStudentInfo(); break;
            case 5 : saveStudentInfo(); break;
            case 6 : loadStudentInfo(); break;
            case 7 : printf("Save students to file before leaving (y/n)? ");
					 scanf(" %s",temp);
					 sscanf(temp, "%c", &save);
				 	 if (save == 'y' || save == 'Y')
						 saveStudentInfo();
					 printf("Bye!\n");
					 break;
            default:printf("Invalid choice, choose again.\n");
        }
    }

    return 0;
}

/*
	Objective: Adding info to our list.
	Input:There are no parameters
	Output: Adds informations to our struct
*/
void addStudent(){
    char buffer[100];
    char ans = 'y';
	int i;
    while(ans == 'y' || ans == 'Y'){
        if(counter >= MAX){
            printf("The List is full, cannot add any more student\n");
            return;
        }
        printf("\nAdding new Student:\n");
		printf("studentID      : ");
        scanf(" %s", buffer);
        strcpy(studentList[counter].studentID, buffer);
		
        printf("First name : ");
        scanf(" %s", buffer);
        wordCap(buffer, 1);
        strcpy(studentList[counter].firstname, buffer);
		
        printf("Last name  : ");
        scanf(" %s", buffer);
        wordCap(buffer, 1);
        strcpy(studentList[counter].lastname, buffer);
		
		printf("Number of courses    : ");
        scanf(" %s", buffer);
        strcpy(studentList[counter].cNum, buffer);
		int result = atoi(studentList[counter].cNum);
		
		for(i=0;i<result;i++){
			printf("courseID    : ");
			scanf(" %s", buffer);
			strcpy(studentList[counter].courseID, buffer);
			
			printf("Course Name: ");
			scanf(" %s", buffer);
			wordCap(buffer, 2);
			strcpy(studentList[counter].courseName, buffer);
        }
        counter++;
        printf("\nDo you want to add another Student's Information?(y/n): ");
        scanf(" %c", &ans);
    }
}

/*
	Objective: To capitalize a name.
	Input:A word to capitalize
	Output: Same word with all letter but the first in lower case
*/
void wordCap(char string[], int n){
    int i = 0;

    while(string[i]){
        string[i] = toupper(string[i]);
        i++;

        if(n == 1){
            while(string[i] != ' '){
                string[i] = tolower(string[i]);
                i++;
            }

            if(string[i])
                i++;
        }
    }
}

/*
	Objective: Displayinfo list on the screen.
	Input:There are no parameters
	Output: Prints info list to the screen
*/
void displayStudentInfo()
{
	int i;

	if (counter == 0)
		printf("No Students to display\n");
	else
		for (i = 0; i < counter; i++)
		{
			printf("Student %d:\n"
				   "%s\n "
				   "%s\n "
			   	   "%s\n "
				   "%s\n "
				   "%s %s\n"
					 ,i+1,studentList[i].studentID, studentList[i].firstname, studentList[i].lastname,studentList[i].cNum,studentList[i].courseName, studentList[i].courseID);
		}
}

/*
	Objective: //Searches for specific student's informations.
	Input:There are no parameters
	Output: Prints informations on screen with last name matching input value
*/
void searchStudentName(){
	char name[15];
	int check, i, j = 0,index = 0;
	studentInfo *temp = first;
	if (counter == 0)
		printf("No info to search through\n");
	else{
		printf("Please enter last name of person you are searching for: ");
		 scanf("%s", name);
		 wordCap(name, 1);
		for (i = 0; i < counter; i++){
			if (strcmp(name,  studentList[index].lastname) == 0){
				j++;
				check = 1;
			}
			index++;
		}
		temp = first;
		//Prints out each info found
		for (i = 0; i < counter; i++){
			if (strcmp(name, studentList[index].lastname) == 0){
				if (check){
					printf("Student %d\n"
						   "%s\n "
						   "%s\n "
						   "%s\n "
						   "%s\n "
						   "%s %s\n"
					 ,i+1,studentList[i].studentID, studentList[i].firstname, studentList[i].lastname,studentList[i].cNum,studentList[i].courseName, studentList[i].courseID);
					check = 0;
				}
				else{
					printf("Student %d\n"
						   "%s\n "
						   "%s\n "
						   "%s\n "
						   "%s\n "
						   "%s %s\n"
						   ,i+1,studentList[i].studentID, studentList[i].firstname, studentList[i].lastname,studentList[i].cNum,studentList[i].courseName, studentList[i].courseID);
				}
			}
		}
	}
}

/*
	Objective: Saving info to a file.
	Input:There are no parameters
	Output: Saves address book to studentList.txt
*/
void saveStudentInfo(){
    FILE *out;
    int i;

    if((out = fopen("studentList.dat", "w")) == NULL){
        printf("Cannot open studentList.dat for writing\n");
    }

    fprintf(out, "%d\n", counter);

    for(i = 0; i < counter; i++){
		
         fprintf(out, "%s\n", studentList[i].studentID);
		fprintf(out, "%s\n", studentList[i].firstname);
        fprintf(out, "%s\n", studentList[i].lastname);
         fprintf(out, "%s\n", studentList[i].cNum);
		fprintf(out, "%s\n", studentList[i].courseID);
        fprintf(out, "%s\n", studentList[i].courseName);
       
    }

    fclose(out);

    printf("Student List saved successfully.\n");
}

/*
	Objective: To load a previously saved student list.
	Input:There are no parameters
	Output: Clears and in student list from studentList.txt
*/
void loadStudentInfo()
{
    FILE *fd;
    int i;
    char temp[100];

    if((fd = fopen("studentList.dat", "r")) == NULL){
        printf("Cannot open the file studentList.dat for reading\n");
    }

    fscanf(fd, "%d", &counter);

    for(i = 0; i < counter; i++){
		
		fscanf(fd, " %s", studentList[i].studentID);
        fscanf(fd, " %s", studentList[i].firstname);
        fscanf(fd, " %s", studentList[i].lastname);
		fscanf(fd, " %s", studentList[i].cNum);
        fscanf(fd, " %s", studentList[i].courseID);
        fscanf(fd, " %s", studentList[i].courseName);
   
    }
    fclose(fd);

    printf("Student List loaded successfully.\n");
}

/*
	Objective: Exit the program
	Input:There are no parameters
	Output: Exit the program
*/
void Exit(){
    char save;

    printf("Save your studentInfos to file before leaving? (y/n): ");
    fflush(stdin);
    scanf("%c", &save);
    if(save == 'y' || save == 'Y')
        saveStudentInfo();
    printf("Bye!\n");

}

/*
	Objective: Removes a certain info from the student's list.
	Input:There are no parameters
	Output: Takes a info out from the list
*/
void DeleteStudent(){

	//del is used to confirm deletion
	int n, m = 0;
	studentInfo *new, *old;
	char temp, del[5];
    int studentID[10];
	new = NULL;

	if (counter == 0)
		printf("Nothing to delete\n");
	else{
		printf(" please enter (Student ID Number) you want to delete? ");
		scanf("%s", studentID);

		//Finds info with same name and asks for deletion confirmation
			for (old = first; old != NULL; new = old){

				sscanf(del, "%c", &temp);

		}
	}
}

About

Leave a reply

Captcha Click on image to update the captcha .

error: Content is protected !!