When I am running this program having error in reading file compiler show error of access violation please help me out in this matter I have tried << and >> operators for writing and reading but I am using next pointer due to which I cannot use these operator
#include<iostream>
#include<conio.h>
#include<string>
#include<fstream>
using namespace std;
class employee
{ private:
int id;
static int count;
public:
string name;
employee *next;
employee()
{ cout<<"enter name:";
cin>>name;
count++;
id=count;
next=NULL;
}
employee(int x)
{}
void setcount(int x)
{count=x;}
void show()
{cout<<"Name: "<<name<<endl;
cout<<"id: "<<id<<endl;
}
void setname(string s)
{name=s;}
string getname()
{return name;}
int getid()
{return id;}
};
int employee::count=0;
class db
{ employee *list;
public:
db ()
{ list=NULL; }
void hire()
{employee *e=new employee;
employee *temp=list;
if(list==NULL)
{list=e;}
else{
while(temp->next!=NULL)
{ temp=temp->next; }
temp->next=e;
}//else end
cout<<"hired..."<<endl;
}
void displayall()
{ if(list==NULL)
cout<<"NO record..";
else
{ employee *temp=list;
while(temp!=NULL)
{temp->show();
temp=temp->next; }
}//else end
}
void remove()
{ int id;
int found=0;
cout<<"Enter id to be removed...";
cin>>id;
if(list==NULL)
cout<<"no record..";
else
{ employee *temp=list;
employee *pre=list;
while(temp!=NULL)
{ if( temp->getid()==id)
{ found=1;
employee *e=temp;
e->show();
if(temp==list) //first item to b removed
list=list->next;
else
pre->next=temp->next;
delete e; cout<<"record deleted"<<endl;
break;
}
pre=temp;
temp=temp->next;
}//while end
if(found==0)
cout<<"record not found.."<<endl;
}//else end
}
void searchbyname()
{ string name;
int found=0;
cout<<"enter name..";
cin>>name;
if(list==NULL)
cout<<"no record..";
else
{ employee *temp=list;
while(temp!=NULL)
{ if( temp->getname()==name)
{ found=1;
temp->show();}
temp=temp->next;
}//while end
if(found==0)
cout<<"record not found.."<<endl;
}//else end
}
void save()
{ ofstream out;
out.open("eb",ios::out | ios::binary);
if(!out)
cout<<"cannot save..";
else
{ employee *temp=list;
while(temp!=NULL)
{ out.write((char*) temp,sizeof(employee));
temp=temp->next;
}
out.close();
}
}
void retrieve()
{ ifstream in;
in.open("eb",ios::in | ios::binary);
if(!in)
cout<<"cannot retrieve..";
else
{ employee *temp=NULL;
employee e(0);
while(1)
{ in.read((char*) &e,sizeof(employee));
employee *p=new employee(e); //dynamically creating an employee that will have the same value as 'e' and saving its address in p. Every node in link list has different location so p will be different. &e is always same. So actually read node from file in e than create a node dynamically with same contents and address will be save in p.
if(list==NULL)
{list=p;
temp=list;
p->show();}
else{
temp->next=p;
temp=temp->next;
e.show();
if(e.next==NULL)
{ e.setcount(e.getid()); break;}
}
}
in.close();
}
}
};
void main()
{
db obj;
try{ obj.retrieve();
int op;
while(1)
{ cout<<endl<<"Enter 1 to hire ,2 for remove ,3 for displayall, 4 for search by name.. 5 for exit";
cin>>op;
switch(op)
{case 1:
obj.hire(); break;
case 2:
obj.remove(); break;
case 3:
obj.displayall(); break;
case 4:
obj.searchbyname(); break;
}
if(op==5)
break;
}
obj.save();
}
catch(...)
{cout<<"error..";}
getch();
}
You can not read your class data from the file using this code: in.read((char*) &e,sizeof(employee)); Your class contains std::string and pointers, they will be initialized by the values, that point nowhere. Any attempt to use them is an undefined behavior, leading to the AV in your case.
Related
I'm working on this linked list project where I'm supposed to save the employee data in a linked list and apply some operations.
and I am new with c++ so I'll show you my code and please help me with it.
my questions are:
1- whenever I enter a full name the program keeps going in an infinite loop! i used getline() method and still its not working. but if I enterd just the first name it works fine.
2- I don't know how I can display or find the employee information if I used the methods I create it gives me random data.
my code:
main:
#include<iostream>
#include<string>
#include <fstream>
#include "EmployeeList.cpp"
using namespace std;
int main(){
string name;
int choice, id;
double Salary;
EmployeeList* employee = new EmployeeList();
cout<<"***********************************************"<<endl;
cout<<"**********EMPLOYEE MANAGEMENT SYSTEM***********"<<endl;
cout<<"***********************************************\n\n"<<endl;
do{
cout<<" 1--->Press 1 to INSERT EMPLOYMENT:"<<endl;
cout<<" 2--->Press 2 to FIND EMPLOYMENT INFO ID:"<<endl;
cout<<" 3--->Press 3 to DELETE EMPLOYMENT ID:"<<endl;
cout<<" 4--->Press 4 to SEARCH HIGHEST SALARY:"<<endl;
cout<<" 5--->Press 5 to DISPLAY ALL EMOLYMENTS:"<<endl;
cout<<" 6--->Press 6 to REVERSE DISPLAY ALL EMPLOYMENTS:"<<endl;
cout<<" 7--->Press 7 to EXIT:"<<endl;
cout<<"\n Enter Your Choice: ";
cin>>choice;
switch(choice){
case 1:
cout<<endl;
cout<<"Enter name : ";
getline(cin,name);
cout<<"Enter ID : ";
cin>>id;
cout<<"Enter Salary : ";
cin>>Salary;
cout<<endl;
employee ->INSERTEMPLOYMENT(name,id,Salary);
break;
case 2:
cout<<endl;
cout<<"Enter ID : ";
cin>>id;
cout<<endl;
employee->FINDEMPLOYMENTINFOID(id);
break;
case 3:
cout<<endl;
cout<<"Employee ID : ";
cin>>id;
cout<<endl;
employee-> DELETEEMPLOYMENTID(id);
break;
case 4:
cout<<endl;
employee->SEARCHHIGHESTSALARY();
break;
case 5:
cout<<endl;
employee->DISPLAYALLEMPLOYMENTS();
break;
case 6:
cout<<endl;
employee->REVERSEDISPLAYALLEMPLOYMENTS(employee->getHead());
break;
case 7:
exit(0);
default:
cout<<"Invalid choice."<<endl;
break;
}
} while (true);
return 0;
}
Employee:
#include<iostream>
#include<stdlib.h>
using namespace std;
class Employee{
private:
string name;
int ID;
double Salary;
Employee* next;
public:
//Constructor
Employee (string n, int id, double s){
n = name;
id = ID;
s = Salary;
this->next = NULL;
}
//Setters
void setName(string n){
n = name;
}
void setID(int id){
id = ID;
}
void setSalary(double s){
s = Salary;
}
void setNext(Employee* next){
this->next = next;
}
//Getters
string getName(){
return name;
}
int getID(){
return ID;
}
double getSalary(){
return Salary;
}
Employee* getNext(){
return this->next;
}
};
EmployeeList:
#include "Employee.cpp"
using namespace std;
class EmployeeList{
private:
Employee* head;
public:
//Constructor
EmployeeList(){
head = NULL;
}
//getter
Employee* getHead(){
return head;
}
//insert method
void INSERTEMPLOYMENT (string name, int id, double Salary){
Employee* newnode = new Employee(name, id, Salary);
if(head == NULL){
head = newnode;
}
else {
Employee* temp = head;
while(temp->getNext() != NULL){
temp = temp->getNext();
}
temp->setNext(newnode);
}
cout<<"The employee record was added successfully."<<endl;
cout<<"------------------------------------------"<<endl;
}
//info
void FINDEMPLOYMENTINFOID (int id){
Employee *temp = head;
bool status = false;
while(temp != NULL){
if(temp->getID() == id){
status = true;
cout<<"Employee Name = "<<temp->getName()<<endl;
cout<<"Employee ID = "<<temp->getID()<<endl;
cout<<"Employee Salary = "<<temp->getSalary()<<endl;
cout<<"---------------------------------------"<<endl;
}
temp = temp->getNext();
}
if(status == false) {
cout<<"Employee is not found in the list."<<endl;
cout<<"--------------------------------------------"<<endl;
}
}
//display
void DISPLAYALLEMPLOYMENTS (){
Employee* temp = head;
while(temp != NULL){
cout<<"Employee Name = "<<temp->getName()<<endl;
cout<<"Employee ID = "<<temp->getID()<<endl;
cout<<"Employee Salary = "<<temp->getSalary()<<endl;
cout<<"---------------------------------------"<<endl;
temp = temp->getNext();
}
}
};
There are 2 major problems here.
Assignment are not reversible...
In Employee class most assignment are reversed. The constructor is:
//Constructor
Employee (string n, int id, double s){
n = name; // replace the local copy of n with name!!!
id = ID;
s = Salary;
this->next = NULL;
}
It should of course be:
//Constructor
Employee(string n, int id, double s) {
name = n;
ID = id;
Salary= s;
this->next = NULL;
}
This is the reason why your Employee instances only contain garbage values...
Mixing getline and stream extractor is dangerous
When you use a stream extractor to read an integer value, the stream pointer is positioned immediately after the last number, so just before the end of line. If you use getline immediately after that, you will read that newline character only and will get an empty string. A simple trick is to consistently skip over empty lines:
case 1:
cout << endl;
cout << "Enter name : ";
for (;;) {
getline(cin, name);
if (name != "") break;
}
...
That should be enough to fix the immediate problems. But others are still around:
you never test the state of the input streams: if the users types an incorrect number you will not detect it and your program will have a wrong behaviour
EmployeeList being a linked list should behave as a container: it should automatically destroys its elements in its destructor. And because of that and of the rule of 5, it should also have a custom (or deleted) copy constructor and copy assignment operator.
constructor have a special syntax to directly initialize their members instead of first default initialize them and then assign to them. Not only it is slightly more efficient, but it is the only way to initialize const members. For that reason the idiomatic way should be:
//Constructor
Employee (string n, int id, double s)
: name(n), ID(id), Salary(s), next(nullptr) {}
I'm a beginner in coding. I am woring on a mini project to write a car parking system from my college. I used doubly-linked list to insert, search and delete to add, search and remove cars in the system .In this program, everything works fine untill i compile the program to remove a Car.Whenever i enter the car number to remove it from parking the error occurs and the carparking.exe suddenly stops.
This is my program:
#include <iostream>
using namespace std;
struct node
{
int no;
int charge,entry_time,exit_time;
char name[20];
node *prev, *next;
};
class Car
{
private:
node *head;
char ch[20];
public:
Car()
{
head= NULL;
}
void input()
{
cout<<"\nWelcome to Car Parking system\n"<<endl;
cout<<"\n Press 1: To Park a Car"<<endl;
cout<<"\n Press 2: To Search a Car"<<endl;
cout<<"\n Press 3: To Remove of Car"<<endl;
cout<<"\n Press 4: To display the Parking records"<<endl;
cout<<"\n Press 5: Exit the program"<<endl;
return;
}
void add_car()
{
node *newer= new node;
system("cls");
cout<<"Enter the Car number: "<<endl;
cin>>newer->no;
fflush(stdin);
cout<<"Enter the name of driver: "<<endl;
cin.getline(newer->name,20);
fflush(stdin);
cout<<"Enter the entry time of car"<<endl;
cin>>newer->entry_time;
fflush(stdin);
cout<<"Enter the exit time of car"<<endl;
cin>>newer->exit_time;
fflush(stdin);
newer->charge=(newer->exit_time-newer->entry_time)*300;
newer->next =head;
newer->prev =NULL;
if(head!=NULL)
{
head->prev = newer;
}
head= newer;
cout<<"\nThe car is parked successfully"<<endl;
if(head==NULL)
{
cout<<"\n No Car is parked: "<<endl;
}
}
void del_car()
{
if(head==NULL)
{
cout<<"\n No Car is parked "<<endl;
}
else
{
int value;
cout<<"\nEnter the Car number to move"<<endl;
cin>>value;
node *temp=head;
bool flag=false;
if(temp->no== value)
{
head=temp->next;
head->prev=NULL;
flag= true;
delete temp;
if(flag==true)
{
cout<<"\nThe Parking charge is Rs "<<temp->charge<<" only"<<endl;
cout<<"The car is moved out of parking zone"<<endl;
}
}
else
{
while (temp!=NULL)
{
if(temp->no==value)
{
node *p,*q;
if(temp->next==NULL)
{
p=temp->prev;
p->next=NULL;
flag=true;
delete temp;
}
else
{
p=temp->prev;
q=temp->next;
p->next=q;
q->prev=p;
flag=true;
delete temp;
}
}
temp=temp->next;
}
if(flag==false)
{
cout<<"\n\t The car number is not found"<<endl;
}
}
}
}
void display()
{
if(head==NULL)
{
cout<<"No Car is parked"<<endl;
}
else
{
node *temp= head;
while(temp!= NULL)
{
cout<<"\n------------------------------Information----------------------------------------"<<endl;
cout<<"\n\tThe name of the driver is:"<<temp->name<<endl;
cout<<"\n\tThe Car number is: "<<temp->no<<endl;
cout<<"\n\tThe entry time of the car is: "<<temp->entry_time<<endl;
cout<<"\n\tThe exit time of the car is: "<<temp->exit_time<<endl;
temp =temp->next;
}
}
}
void search()
{
if(head==NULL)
{
cout<<"No Car is parked"<<endl;
}
else
{
int value;
cout<<"\nEnter the car number to search"<<endl;
cin>>value;
node *temp=head;
bool flag=false;
if(temp->no=value)
{
cout<<"\n\t----Information of the Car-----"<<endl;
cout<<"\nThe name of the driver is: "<<temp->name<<endl;
cout<<"\nThe Car number is: "<<temp->no<<endl;
cout<<"\nThe entry time is: "<<temp->entry_time<<endl;
cout<<"\nThe Car is still Parked "<<endl;
cout<<"\nThe Parking charge is still pending "<<endl;
return;
}
temp= temp->next;
}
}
};
int main()
{
int n;
string ch;
Car c1;
x2:
c1.input();
cout<<"\n\t----Enter your choice----"<<endl;
cin>>n;
if(n==1)
{
x1:
c1.add_car();
cout<<"\nDo you want to go to Main Menu: Press(y or n)"<<endl;
cin>>ch;
fflush(stdin);
if(ch=="Y" || ch=="y")
{
goto x2;
}
else
{
exit(1);
}
}
else if(n==2)
{
c1.search();
cout<<"\nDo you want to go to Main Menu: Press(y or n)"<<endl;
cin>>ch;
if(ch=="Y"||ch=="y")
{
goto x2;
}
else
{
exit(1);
}
}
else if(n==3)
{
c1.del_car();
cout<<"\nDo you want to go to Main Menu: Press(y or n)"<<endl;
cin>>ch;
if(ch=="Y"||ch=="y")
{
goto x2;
}
else
{
exit(1);
}
}
else if(n==4)
{
c1.display();
cout<<"\nDo you want to go to Main Menu: Press(y or n)"<<endl;
cin>>ch;
if(ch=="Y"||ch=="y")
{
goto x2;
}
else
{
exit(1);
}
}
else if(n==5)
{
exit(1);
}
else
{
cout<<"\n\tChoose correct number"<<endl;
goto x2;
}
return 0;
}
In the function del_car()
if(temp->no== value)
{
head=temp->next;
head->prev=NULL;
}
If there is only car parked, then only head is valid and head->next and head->prev will be NULL. Now,
head = temp->next; // head becomes NULL here
head->prev = NULL; //you are dereferencing a NULL pointer and hence the seg. fault
Just setting head = NULL should do in this condition.
Also, if there are more cars parked, the condition is while(temp!=NULL), but if the car is found, then you have to break the loop so that temp won't be incremented unnecessarily. Add a break; after flag=true;.
I am making a simple word comparison program.
For this, I have a list of words in a text file with one word per line.
Then, I imported all these words into a program using ifstream and then into BST.
Then, I used to insert, create the root, search and traversal functions.
But, I am getting these two simple errors which I don't understand at all/.
Please help!
I used a BST to store all the words from txt file into program and then search among them. I also included the option of adding new words directly to program but not txt file.
I am having the following errors:
"
In function void create()
incompatible types in assignment of 'char' to 'char[100]'
In function void insert()
incompatible types in assignment of 'char' to 'char[100]'
"
#include<iostream>
#include <fstream>
#include <string>
using namespace std;
char x;
char a[100];
int count=0,i=0;
char str;
struct node
{
struct node *left;
struct node *right;
char word[100];
};
struct node *root=NULL;
void create()
{
struct node *newnode;
newnode=(struct node *)malloc(sizeof(struct node));
char str[100];
for(i=0;i<=100;i++) {
str= a[i];
}
strcpy(newnode->word,str);
newnode->left=NULL;
newnode->right=NULL;
root=newnode;
}
void insert()
{
struct node *newnode,*temp,*parent;
newnode=(struct node *)malloc(sizeof(struct node));
char str[100];
for(i=0;i<=100;i++) {
str= a[i];
}
strcpy(newnode->word,str);
newnode->left=NULL;
newnode->right=NULL;
temp=root;
while(temp!=NULL)
{
parent=temp;
if(strcmp(temp->word,str)<0)
temp=temp->left;
else
temp=temp->right;
}
if(strcmp(parent->word,str)<0)
parent->left=newnode;
else
parent->right=newnode;
}
void traverse(struct node *temp)
{
if(temp!=NULL)
{
traverse(temp->right);
cout<<temp->word<<":";
traverse(temp->left);
}
}
void search()
{
struct node *temp;
temp=root;
char ab[100];
cout<<"enter the word to search \n";
cin>>ab;
while(temp!=NULL)
{
if(strcmp(temp->word,ab)==0)
{
cout<<"element found \n";
break;
}
else if(strcmp(temp->word,ab)<0)
{
temp=temp->left;
}
else if(strcmp(temp->word,ab)>0)
{
temp=temp->right;
}
else
{
cout<<"not found";
}
}
}
int main()
{
ifstream inFile;
inFile.open("test.txt");
if(inFile.fail()) {
cerr << "Error opening file"<< endl ;
exit(1);
}
char x;
char a[100];
int count=0,i=0;
char str;
while( !inFile.eof()) {
inFile >> x;
a[i]=x;
count++;
i++;
}
for (i=0;i<100;i++){
cout<< a[i]<<endl;
}
int choice;
while(1)
{
cout<<"***menu***\n1.cerate root node\n2.insert a word\n3.traverse\n4.search\n6.Exit\n";
cout<<"enter your choice \n";
cin>>choice;
switch(choice)
{
case 1:
create();
break;
case 2:
insert();
break;
case 3:
traverse(root);
break;
case 4:
search();
break;
case 6:
exit(0);
}
}
}
#include<iostream>
#include <conio.h>
using namespace std;
class Student_Node{
public:
Student_Node *right,*left;
int ID;
string First_Name;
int Age;
float Test_Score,CGPA;
};
class Student_Tree{
Student_Node *root;
public:
Student_Tree()
{
root=NULL;
}
void take_input();
void build_tree(int id,string name,int age,float score,float cgpa);
void BST_search();
void input_sort(Student_Node *n, Student_Node *r);
void Search (int id);
void searching(int id,Student_Node *r);
void Node_Depth(int id);
void FindHeight();
int Height(Student_Node* root);
};
/////////////////////////////////////////////////////////////////////////
This is my build function. In take_input() i apply a loop to give the user choice to add as many nodes as he wants. In the loop the user gives data which is passed the obj.build_tree(int id,string name,int age,float score,float cgpa); But it adds only one Node and nothing more. When i call my Search (int id) it only outputs the data of first node.
void Student_Tree::build_tree(int id,string name,int age,float score,float
cgpa)
{
Student_Node *n= new Student_Node();
n->ID=id;
n->First_Name=name;
n->Age=age;
n->CGPA=cgpa;
n->Test_Score=score;
if(root==NULL)
{
root=n;
}
else
{
input_sort(n,root);
}
}
void Student_Tree::input_sort(Student_Node *n, Student_Node *r)
{
if(r->ID>=n->ID)
{
if(r->left!=NULL)
{
input_sort(n,r->left);
}
else
{
n=r->left;
}
}
if(r->ID<=n->ID)
{
if(r->right!=NULL)
{
input_sort(n,r->right);
}
else
{
n=r->right;
}
}
}
/////// inout takes input from user, then sends it to the build fucntion
/////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////
void Student_Tree::Search (int id)
{
Student_Node *n=new Student_Node();
n=root;
if(n==NULL)
{
cout<<"Tree is empty"<<endl;
}
else
{
searching(id,root);
}
}
void Student_Tree::searching(int id,Student_Node *r)
{
if(r->ID>=id)
{
if(r->ID==id)
{
cout<<"ID"<<" "<<"Age"<<" "<<"CGPA"<<" "<<"First Name"<<" "<<"Test
Score"<<endl;
cout<<"-------------------------------------------------------------
---------"<<endl;
cout<<r->ID<<" "<<r->Age<<" "<<r->CGPA<<" "<<r->First_Name<<" "<<r->
Test_Score;
}
else
{
searching(id,r->left);
}
}
else if(r->ID<=id)
{
if(r->ID==id)
{
cout<<r->ID;
}
else
{
searching(id,r->right);
}
}
else
{
cout<<"Roll No Not found"<<endl;
}
}
void Student_Tree::FindHeight()
{
Height(root);
}
int Student_Tree::Height(Student_Node *r)
{
if(r==NULL)
{
return 0;
}
else
{
int lb=Height(r->left);
int rb=Height(r->right);
cout<<max(lb,rb)+1;
return max(lb,rb)+1;
}
}
/////////////////////////////////////////////////////////////////////////
void Student_Tree::Node_Depth(int id)
{
int depth = 0;
Student_Node *temp = new Student_Node;
temp = root;
// Run the loop untill temp points to a NULL pointer.
while(temp != NULL)
{
depth++;
if(temp->ID == id)
{
cout<<"\nData found at depth: "<<depth<<endl;
return;
}
// Shift pointer to left child.
else if(temp->ID > id)
temp = temp->left;
// Shift pointer to right child.
else
temp = temp->right;
}
cout<<"\n Data not found";
return;
}
void take_input()
{
Student_Tree obj;
int ID;
string First_Name;
int Age;
float Test_Score,CGPA;
cout<<"How many students data do you want to enter?"<<endl;
int no; cin>>no;
for(int i=0;i<no;i++)
{
cout<<"\t\t\t\tEnter Student Id"<<endl;
cout<<"\t\t\t\t";cin>>ID;
cout<<"\t\t\t\tEnter Student First Name"<<endl;
cout<<"\t\t\t\t";cin>>First_Name;
cout<<"\t\t\t\tEnter Student Age"<<endl;
cout<<"\t\t\t\t";cin>>Age;
cout<<"\t\t\t\tTest Student Score"<<endl;
cout<<"\t\t\t\t";cin>>Test_Score;
cout<<"\t\t\t\tEnter Student CGPA"<<endl;
cout<<"\t\t\t\t";cin>>CGPA;
obj.build_tree(ID,First_Name,Age,Test_Score,CGPA);
}
}
int main()
{
Student_Tree obj;
take_input();
cout<<"Enter node"<<endl;
int n; cin>>n;
obj.Node_Depth(n);
obj.FindHeight();
cout<<"\nEnter id to search"<<endl; int id; cin>>id;
obj.Search(id);
return 0;
}
Your build_tree function calls input_sort to add an additional node. But input_sort never adds anything to anything else. What code do you think actually attaches a second node into the tree? There doesn't seem to be any.
if(root==NULL)
{
root=n;
}
else
{
input_sort(n,root);
}
The first part of this code adds n to the tree if the tree is empty. The second part is expected to add n to the three if the tree is not empty.
void Student_Tree::input_sort(Student_Node *n, Student_Node *r)
{
if(r->ID>=n->ID)
{
if(r->left!=NULL)
{
input_sort(n,r->left);
}
else
{
n=r->left;
We're already hosed right here. This function was called with n being the new node to be added and no other pointer to that node was stored anywhere. If we change the value of n here, we lose the only pointer we had to the node we were supposed to add.
Even if we don't get hosed here, there's no code in input_sort anywhere to actually add a node to the tree, and build_tree is expecting it to.
I am getting this error main.cpp:23:5: error: ‘Department’ does not name a type
/*
* File: main.cpp
* Author: anonymous
*
* Created on May 11, 2015, 9:44 PM
*/
#include <cstdlib>
#include <iostream>
using namespace std;
class College{
public :
string name; //for name of the college
int numOfColleges;
int numDepartments; //number of departments in this college
Department* dep; //this will point to the department in this college
College* next; //the next pointer to point to the next college
College(){
name =" ";
numDepartments=0 ;
dep = NULL;
next=NULL;}
College (string n, int numD ){name=n ;next=NULL;}
void Print(){
cout<<name<<"\n";
}
};
class Department
{
public:
string name;
int numOfStudents;
Department* next;
Department(){name[0] ; numOfStudents=0 ; next=NULL ;}
Department( string n , int numS){ name =" "; n ;numOfStudents = numS ; next=NULL;}
void Print(){cout<<name<<" "<<numOfStudents;}
};
void AddCollege(College *head)
{
string n;
int numD;
cout<<"Enter the name of the College : ";
cin>>n;
cout<<"Enter the number of Departments : ";
cin>>numD;
College* tmp = new College(n,numD);
if(!head)
{
head=tmp;
return;
}
College * t=head;
while(t->next) t=t->next;
t->next=tmp;
cout<<"college added";
}
void DeleteCollege(College*&head)
{
string name;
cout<<"enter name of college:";
cin>>name;
if((!head)||(!head->next && head->name!=name))
{
cout<<"could not find "<<name<<" in the list\n"; return;
}
if(head->next && head->name==name)
{
College *tmp=head;
head=head->next;
delete tmp;
tmp=NULL;
return;
}
College* tmp = head;
College* t;
while(tmp->next)
{
if(tmp->name==name)
{
College* tmp = head;
head = head->next;
delete tmp;
tmp->next=NULL;
}
if(tmp->next->name == name)
{
t = tmp->next;
tmp->next = tmp->next->next;
delete t;
return;
}
tmp=tmp->next;
}
cout<<"could not find "<<name<<" in the list\n";
};
/*Print the list of colleges int the college head*/
void printColleges(College *head)
{
cout<<"ALL Colleges in Database : \n";
College * temp = head;
while(temp!=NULL)
{
temp->Print();
temp=temp->next;
}
}
void AddDepartment(College *head)
{
if(!head)
{
cout<<"NO COLLEGES ADDED!!";
return;
}
string n,Dname;
int numS;
College* tmp = head;
College*tmp2 = tmp;
;
while(tmp)
{
cout<<"->"<<tmp->name;
tmp=tmp->next;
cout<<endl;
}
cout<<"Type a College name to Add a Department inside\n";
cin>>n;
cout<<"Enter Department name:";
cin>>Dname;
cout<<"Enter the number of students :";
cin>>numS;
while(n!=tmp->name)
{
tmp=tmp->next;
}
if(!tmp-> dep)
{
College * tmpD = new Department(Dname,numS);
Department*tmpDD = tmpD;
head=tmp;
return;
}
/*if(tmp->dep)
{
Department *tmp3 = tmp->dep->next;
t=tmp->dep;
dep->next=tmp3;
}*/
};
//void DeleteDepartment(College* head)
//{
//
//
// ;}
void PrintAll(College*head)
{
College * temp = head;
while(temp!=NULL)
{
temp->Print();
temp=temp->next;
}
};
int main(int argc, char** argv) {
int choice;
College*h = NULL;
while(choice!=11){
cout<<"\n\nMenu\n";
cout<<"1: Add a new college in the list \n";
cout<<"2: Delete a college from the list \n";
cout<<"3: Add a department in a college\n";
cout<<"4: Delete a department from a college.\n";
cout<<"5: Print all the colleges along with their departments\n ";
cout<<"6: Delete all the departments of a particular college \n";
cout<<"7: Delete all the colleges from the list.\n";
cout<<"8: Print the total number of students in a college.\n";
cout<<"9: Find and print the college that has the highest number of students. \n";
cout<<"10: Find and print the department that has the highest number of students.\n";
cout<<"EXIT\n";
cin>>choice;
switch(choice)
{
case 1: AddCollege(*&h);
break;
case 2: DeleteCollege(*&h);
break;
case 3: printColleges(*&h);
break;
case 4:
AddDepartment(*&h);
break;
/* case 4:
DeleteDepartment(*&h);
break;
case 5:
PrintAll(*&h);
break;
case 6:
DeleteDepartment(*&h);
break;
case 7:
DeleteAllColleges(*&h);
break;
case 8:
NumOfStudentsInCollege(*&h);
break;
case 9:
HighestNumOfStudentsInCollege(*&h);
break;
case 10:
HighestNumOfStudentsInDep(*&h);
break;
case 11:
cout<<"bye";
break;*/
default:
cout<<"\nInvalid menu choice";
}
}
}
Declare your Department class before it's called in College. (Move your Department declaration above your College declaration):
class Department
{
public:
string name;
int numOfStudents;
Department* next;
Department(){name[0] ; numOfStudents=0 ; next=NULL ;}
Department( string n , int numS){ name =" "; n ;numOfStudents = numS ; next=NULL;}
void Print(){cout<<name<<" "<<numOfStudents;}
};
class College{
public :
string name; //for name of the college
int numOfColleges;
int numDepartments; //number of departments in this college
Department * dep; //this will point to the department in this college
College * next; //the next pointer to point to the next college
College(){
name =" ";
numDepartments=0 ;
dep = NULL;
next=NULL;}
College (string n, int numD ){name=n ;next=NULL;}
void Print(){
cout<<name<<"\n";
}
};