linked list student database

making the student database to a linked list

Page 1 of 1

2 Replies - 5156 Views - Last Post: 07 August 2009 - 05:15 AM Rate Topic: -----

#1 autumn_storm2216  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 04-August 09

linked list student database

Posted 06 August 2009 - 08:05 PM

/*Create a simple student database program.
You are going to create just one class/subject. Prompt the user the following menu:
Menu:
1. Enroll
2. Drop
3. Sort by ID
4. Sort by Name
5. Display by Year
6. Display by Name
7. Display by ID
8. Display All
9. Exit
All these items, except of course for Exit, must be implemented using a function. 
You must also include a function that searches an ID, a name. Display functions 
should also be implemented, one for a particular student, and another for the entire 
class/subject.For enroll, you must first check if the student/ID number already exists
in the class. If that is the case, report a message that the student is already enrolled 
in the class. This is of course a separate check from the class size.For Drop, check first
if the student exists in the class. Report a no such student message if otherwise.
*/

#include <iostream.h>
#include <conio.h>										
#include <string.h>
#include <stdlib.h>

struct Student{											//a structure that will store the different information needed for a student to be enrolled
	char id[10];
	char fname[30];
	char mname[30];
	char lname[30];
	char course[50];
	int age;
	int year;
};
struct node{
	Student item;
	node *next;
};

struct subject{
	node *head;											//a structure that will store the capacity of the subject to store students and to store the name 	
	char name[20];
	int size;
};



bool enrol(subject&,Student);
bool drop(subject&,Student);
void sortID(subject&);
void sortName(subject&);
void displayYear(subject,int);
void displayName(subject,char[]);						// DECLARATION OF THE DIFFERENT FUNCTIONS
void displayID(subject,char[]);
int searchID(subject,char[]);
bool searchName(subject,char[]);
void editRecord(subject&,char[]);
void displaystudent(Student);
void displaysubject(subject);
int compare(char [],char []);
void copy(char [],char []);
bool deleteNode(subject&,int);
bool insertfront(subject&,node);
bool insert(subject&,int,int);
bool append(subject&,int);


void main(){
	
	int like;											
	
	subject g;
	g.size=0;											//initializing the size of the struct subject to 0	
	Student t;									
	strcpy(g.name,"PolSci");							//initializing the name of the subject as PolSci
	
	do{													//a loop that will stop if like is equal to 10 which is exit
	cout<<"Menu"<<endl;
	cout<<"1. Enroll"<<endl;
	cout<<"2. Drop"<<endl;
	cout<<"3. Sort by ID"<<endl;
	cout<<"4. Sort by Name"<<endl;						//the MENU for the user what he or she want to do
	cout<<"5. Display by Year"<<endl;
	cout<<"6. Display by Name"<<endl;
	cout<<"7. Display by ID"<<endl;
	cout<<"8. Display All"<<endl;
	cout<<"9. Edit the information"<<endl;
	cout<<"10. Exit"<<endl;

	cout<<"Enter your desire: ";
	cin>>like;
	cin.ignore();
	if(like == 1){										//if the user choose enroll
		cout<<"The subject is PolSci."<<endl;
		cout<<"Enter your ID No.: ";
		cin.getline (g.head->item.id,30);
		cout<<"Enter your first name: ";
		cin.getline(g.head->item.fname,30);
		cout<<"Enter your middle name: ";				//this is the menu if the user choose to enrol in the subject this is the information that will be needed 
		cin.getline(g.head->item.mname,30);
		cout<<"Enter your last name: ";
		cin.getline(g.head->item.lname,30);
		cout<<"Enter your course: ";
		cin.getline(g.head->item.course,50);
		cout<<"Enter your age: ";
		cin>>g.head->item.age;
		cout<<"Enter your year: ";
		cin>>g.head->item.year;
	
		
		bool flag = enrol(g,t);							//calling the function enrol and storing its output in bool flag
		
		if (flag == true)								//if flag return true then the user enrolled succesfully in the subject
			cout<<"Student Enrolled!"<<endl;
		else if(g.size == 40)							//this indicates that the subject is already full and the user cant enroll anymore
			cout<<" The Subject is already full."<<endl;
		else
			cout<<" The student is already enrolled"<<endl;
		getch();
		system("cls");
	}
	if(like == 2){										//for the choice 2
		cout<<"The subject is PolSci."<<endl;
		if(g.size == 0)								//this will come out if the size of the struct subject is 0 
		cout<<"There are no students enrolled in this subject."<<endl;
	
		else
		cout<<"Please enter your id no.: ";				//asking for the id no. only to see if the student exist in the subject and be drop out
		cin>>t.id;
		bool flag2 = drop(g,t);							//calling the function drop and storing it into flag2
		if (flag2)
			cout<<"Dropping is successful!"<<endl;
		else
			cout<<"Your not enrolled in the subject..."<<endl;

	getch();
	system("cls");
	}
	if(like == 3){										//for the choice 3 which is displaying the id nos. enrolled in the subject sorted
		cout<<"The subject is PolSci."<<endl;
		if(g.size == 0)
		cout<<"There are no students enrolled in this subject."<<endl;
		sortID(g);										//calling the void function sortID
		getch();
		system("cls");
	}
	if(like == 4){										// for the choice sort by name
		cout<<"The subject is PolSci."<<endl;
		if(g.size == 0)
		cout<<"There are no students enrolled in this subject."<<endl;
		sortName(g);									//calling the function sortName
		getch();
		system("cls");
	}
	if(like == 5){										//for the choice that asking the information of the different students by the year the user entered
		cout<<"The subject is PolSci."<<endl;
		if(g.size == 0)
		cout<<"There are no students enrolled in this subject."<<endl;
		cout<<"Please enter the year level: ";
		cin>>t.year;
		displayYear(g,t.year);
		getch();
		system("cls");
	}
	if(like == 6){										//posting out the information of the name that the user have entered
		cout<<"The subject is PolSci."<<endl;
		if(g.size == 0)
		cout<<"There are no students enrolled in this subject."<<endl;
		char input[30];
		cout<<"Please enter the name: ";
		cin.get(input,30);	
		displayName(g,input);
		cin.ignore();
		getch();
		system("cls");
	}
	if(like == 7){										//asking the user to enter an id no.then posting out the information
		cout<<"The subject is PolSci."<<endl;
		if(g.size == 0)
		cout<<"There are no students enrolled in this subject."<<endl;
		cout<<"Please enter the ID no.: ";
		cin>>t.id;
		displayID(g,t.id);
		getch();
		system("cls");
	}
	if(like == 8){										//for the option 8 that displays all the information about the students  enrolled in the subject
		cout<<"The subject is PolSci."<<endl;
		if(g.size == 0)
		cout<<"There areno students enrolled in this subject."<<endl;
		displaysubject(g);
		getch();
		system("cls");
	}	
	if(like == 9){										//editing the information that has given
		cout<<"The subject is PolSci."<<endl;
		if(g.size == 0)
		cout<<"There are no students enrolled in this subject."<<endl;
		else{
		cout<<"Please enter the ID no.: ";
		cin>>t.id;
		editRecord(g,t.id);
		}
		system("cls");
	}
	if(like == 10){										//exiting the program
		cout<<"Thank you"<<endl;
		cout<<"Bye"<<endl;
		getch();
		}
	}while(like!=10||like>10||like<10);
}

bool enrol(subject& g, Student t){						//the function for enroll
		
	int flag = searchID(g,g.head->item.id);
	
	insertfront(g,g) = t;								//storing the information in the struct subject(students)(size)

	if(searchID(g,g.head->item.id) != -1)							//if the id no. is already found in the students enrolled in the subject it will return false
		return false;
	else
		g.size++;										//else the size will be added by 1 and return true
	return true;
	
}

int searchID(subject g,char no[]){						//the function for searching the ID 

	int cmp;					
											
	for (int i = 0; i<g.size;i++){						//a loop for comparing the ID nos. already stored in the  g.students and the id no entered by the user
			cmp = strcmp(g.head->item.id,no);								
			if(cmp==0){									//if after comparing it returns 0 means that the id no. had found and return the place of where the id no.found 
				return i;
		}	
	}
			return -1;									//if the id no. does not found it will return -1(as we know in the array there are no negative values
}	
/*
bool drop(subject& g, Student t){						//the function for drop
	for(int i = 0;i<g.size;i++){						//a loop for comparing the previous id stored in the g.students and the id no that have entered by the user
		if(strcmp(g.students[i].id,t.id)==0){		
		g.size = g.size - 1;							//if the id nos.had been found the size must be subtracted by 1  
			return true;	
		}
	}
	int j = i;											//intializing the value of j as i(where the id no had found
		for(;j<g.size;j++){
			g.students[j] = g.students[j+1];			//replacing the last data by the next data stored in the array
	}
	return false;
}

void displayID (subject g, char t[]){					//function for displayID
	for(int i = 0;i<g.size;i++){		
		if(strcmp(g.students[i].id,t)==0){				
	int j = i;
		displaystudent(g.students[j]);					//calling the function displaystudent to display the information of the id no. given by the user
		}
	else
		cout<<"No Student is enrolled in the subject with that ID no. "<<endl;
	}
}
void displaystudent(Student t){							//function for displaystudent
	
	cout<<"ID: "<<t.id<<endl;							
	cout<<"Name: "<<t.lname<<", "<<t.fname<<" "<<t.mname<<endl;
	cout<<"Course: "<<t.course<<endl;
	cout<<"Year: "<<t.year<<endl;
	cout<<"Age: "<<t.age<<endl;
}

void displaysubject(subject g){							//function for displaysubject

	if(g.size == 0){
		cout<<"No students are enrolled in this subject."<<endl;
	}
														//this function will just display all the students that had enrolled in the subject
	else
		for (int i = 0; i<g.size; i++){

			displaystudent(g.students[i]);				//calling the function displaystudent

			cout<<"Please enter to continue"<<endl;
		getch();

		}
}
void displayYear(subject g,int j){						//a function for displayYear
	if(g.size == 0){
		cout<<"No students are enrolled in this subject."<<endl;
	}
	else
		for(int i = 0; i < g.size; i++){
		if(g.students[i].year == j){					//if the given year by the user match the year of the student in the g.students[i].year
			displaystudent(g.students[i]);				//then the information of the student will be display
		}
	}
}
void displayName(subject g,char name[]){				//function for displayName
	if(g.size == 0){
		cout<<"There are no students enrolled in this subject"<<endl;
	}
	for(int i = 0;i<g.size;i++){
		if(strcmp(g.students[i].fname,name)==0||strcmp(g.students[i].mname,name)==0||strcmp(g.students[i].lname,name)==0){//this compare the information given by the user and the information that had already been installed if there is even one the same then it will display all the information of that students
			displaystudent(g.students[i]);
		}
	}
		if(strcmp(g.students[i].fname,name)!=0&&strcmp(g.students[i].mname,name)!=0&&strcmp(g.students[i].lname,name)!=0){//if it doesn't matches any of the students name then the name is not enrolled yet in the subject 
			cout<<"There are no students enrolled in this subject with that name."<<endl;
	}
}
void editRecord(subject& g,char id[]){					//a function for editing the previous information given

	int j;

	for(int i = 0;i<g.size;i++){		
		if(strcmp(g.students[i].id,id)==0){				//searching
		 j = i;
			displaystudent(g.students[j]);				//displaying the information of the found id no.
		}
	}
	cout<<endl;

	int choice,got;
	do{													//a loop that will stop if the user choose 2 means that he/she doesn't have anything to change anymore
	cout<<"Please choose what do you want to change in your first given information"<<endl;
	cout<<"Menu"<<endl;
	cout<<"1. First name"<<endl;
	cout<<"2. Middle name"<<endl;						//Menu for what does the user want to change
	cout<<"3. Last name"<<endl;
	cout<<"4. Course"<<endl;
	cout<<"5. Age"<<endl;
	cout<<"6. Year"<<endl;

	cout<<"Please enter your choice: ";
	cin>>choice;
	cin.ignore();
	
	if(choice == 1) {									//for the choice 1
		cout<<"Enter the new first name: ";
		cin.getline(g.students[j].fname,30);
	}
	if(choice == 2){									//for the choice 2
		cout<<"Enter the new middle name: ";
		cin.getline(g.students[j].mname,30);
	}
	if(choice == 3){									//for the choice 3
		cout<<"Enter the new last name: ";
		cin.getline(g.students[j].lname,30);
	}
	if(choice == 4){									//for the choice 4
		cout<<"Enter the new course: ";
		cin.getline(g.students[j].course,50);
	}
	if(choice == 5){									//for the choice 5
		cout<<"Enter the new age: ";
		cin>>g.students[j].age;
	}
	if(choice == 6){									//for the choice 6
		cout<<"Enter the new year: ";
		cin>>g.students[j].year;
	}
	cout<<"Do you have anything else to change?? press 1 = yes and 2 = no...: ";//this will ask the user if he/she have something else to change   
	cin>>got;
	cout<<endl;
	}while(got!=2);
}
void sortID(subject& g){								//function for sortID
	for(int i = 0; i< g.size; i++){						//storing the first value as the min
		int min = i;
	Student minY;										
		for(int pow = min + 1; pow< g.size; pow++){		//another loop that will be use for comparison
			int minX = strcmp (g.students[min].id,g.students[pow].id);//comparing the two string
				min = pow;								//storing the value of pow to min and become the new lowest value 
		}
		minY = g.students[min];							//swaping the values
		g.students[min] = g.students[pow];
		g.students[pow] = minY;
	}

	for(int x = 0; x<g.size; x++){						//displaying the id no.in sort manner
		cout<<g.students[x].id<<endl;
	}
}
void sortName(subject& g){								//a function for sortName
	for(int i = 0; i< g.size; i++){						
		int min = i;
	Student minY;
		for(int pow = min + 1; pow< g.size; pow++){
			int minX = strcmp (g.students[min].lname,g.students[pow].lname);
				min = pow;
			}
				int yum = strcmp (g.students[min].fname,g.students[pow].fname);//if happens to be that the last name is the same the first name must be compared 
				min = pow;
				
				int max = strcmp (g.students[min].mname,g.students[pow].mname);//if happens to be too that the first name is the same then the middle name must be compared 
				min = pow;

		minY = g.students[min];
		g.students[min] = g.students[pow];				//swaping of values
		g.students[pow] = minY;
	}

	for(int x = 0; x<g.size; x++){						//displaying the name in sort manner
		cout<<g.students[x].lname<<", "<<g.students[x].fname<<" "<<g.students[x].mname<<endl;
	}
}


/*		/*My own strcmp*/
//int compare(char fname[],char sname[]){
//	for(int i = 0; fname[i] != '\0';){					//first loop for the first input	
//		if(fname[i]>=65&&fname[i]<=90){
//			fname[i] = fname[i] + 32;
//		}
//		for(int j = 0; sname[j] != '\0';){				//second loop for the second input
//			if(sname[j]>=65&&sname[i]<=90){
//				sname[j] = sname[j] +32;
//			}
//			if(fname[i] == sname[j]){					//comparing the values in the array one by one if the same then move to the next letter/number
//				i++;									
//				j++;
//			}
//			if(fname[i] != sname[j]){					
//				if(fname[i] > sname[j]){				//checks if the fname is greater than then return 1
//					return 1;
//				if(fname[i] < sname[j]){				//if the fname is less than then return -1 
//					return -1;
////				}
//			}
////		}
//	}
//		return 0;										//after checking everything if the two array is the same then it will return 0
//}
		/*My own strcpy*/
//void copy(char fname[],char sname[]){					
//	for(int i = 0; sname != '\0';i++){							
//		fname[i] = sname[i];							//storing the value of fname to sname
//	}		
//}
bool insertFront(subject& l,node x){
	node *n = new node;

	if(n == NULL){
		return false;
	}
	else{
		n -> item = x;
		n -> next = NULL;

		if(l.head == NULL)
				l.head = n;	
		else{
			n -> next = l.head;
			l.head = n;
		}
	}
		
	
	l.size++;
	return true;
}
bool append(subject& l, int x){
	node *n = new node;

	if(n == NULL){
		return false;
	}
	else{
		n -> item = x;
		n -> next = NULL;

		if(l.head == NULL)
			l.head = n;
		else{
			node *temp = l.head;
		while(temp -> next != NULL)
				temp = temp -> next;		
				
		temp -> next = n;
		}
		l.size++;
		return true;
	}
}

bool insert(subject l, int x, int pos){
	if (pos < 1 || pos > l.size + 1){
		return false;
	}
	else{
		if(pos == 1){
			node *n = new node;
			n -> item = x;
			n -> next = NULL;
			n -> next = l.head;
			l.head = n;
		}
		else{
			node *n = new node;
			if(n == NULL){
				return false;
			}
			n -> item = x;
			n -> next = NULL;
			int c = 1;
			node *tmp = l.head;
			while(c < pos - 1){
				tmp = tmp -> next;
				c++;
			}
			n -> next = tmp -> next;
			tmp -> next = n;
		}
			l.size++;
			return true;
	}
}

bool deleteNode(subject& l,int pos){
	node *n = new node;

	if(pos < 1 ||pos > l.size){
		return false;
	}
	else{
		node *del;
		if(pos == 1){
			del = l.head;
			l.head = l.head -> next;
	}
		else{
			int c = 1;
			node *tmp = l.head;
			while(c < pos - 1){
				tmp = tmp -> next;
				c++;
			}
			del = tmp -> next;
			tmp -> next = del -> next;
		}
			l.size--;
			delete del;
			return true;
	}
}


*Edit: Please use code tags in the future, thanks! :code:



i have tried to change some of the function to linked list idea but i encounter to much error and i dont know anymore how to manage and manipulate things in my student database i hope you can help me because i need to pass it before Aug.10...

please i really really needed your help

This post has been edited by autumn_storm2216: 07 August 2009 - 02:10 AM


Is This A Good Question/Topic? 0
  • +

Replies To: linked list student database

#2 Ancient Dragon  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 81
  • View blog
  • Posts: 679
  • Joined: 19-July 09

Re: linked list student database

Posted 06 August 2009 - 08:46 PM

What is the problem(s) with your program ?
Was This Post Helpful? 0
  • +
  • -

#3 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6039
  • View blog
  • Posts: 23,436
  • Joined: 23-August 08

Re: linked list student database

Posted 07 August 2009 - 05:15 AM

Are you receiving any errors? Does this code not work that way you intended it? When asking for help there are a couple items that are vital in order for someone to properly help you:
  • Post the code you're having problems with
  • Post the exact error you're receiving, if you are receiving one
  • If no error explain what the code is doing versus what you want it to do
  • Post your question in the body of your post, not the description field

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1